1
1
package samples ;
2
2
3
- import java .io .InputStreamReader ;
3
+ import java .io .* ;
4
4
5
5
import org .apache .commons .httpclient .HttpClient ;
6
6
import org .apache .commons .httpclient .methods .GetMethod ;
7
+ import org .apache .commons .httpclient .methods .PostMethod ;
8
+ import org .apache .commons .httpclient .methods .multipart .*;
7
9
8
- import com .google .gson .Gson ;
10
+ import water .util .Utils ;
11
+
12
+ import com .google .gson .*;
13
+ import com .google .gson .internal .Streams ;
14
+ import com .google .gson .stream .JsonWriter ;
9
15
10
16
/**
11
- * Invokes an H2O functionality through the Web API.
17
+ * Invokes H2O functionality through the Web API.
12
18
*/
13
19
public class WebAPI {
20
+ static final String URL = "http://127.0.0.1:54321" ;
21
+ static final File JSON_FILE = new File (Utils .tmp (), "model.json" );
22
+
14
23
public static void main (String [] args ) throws Exception {
24
+ listJobs ();
25
+ exportModel ();
26
+ importModel ();
27
+ }
28
+
29
+ /**
30
+ * Lists jobs currently running.
31
+ */
32
+ static void listJobs () throws Exception {
15
33
HttpClient client = new HttpClient ();
16
- GetMethod get = new GetMethod ("http://127.0.0.1:54321 /Jobs.json" );
34
+ GetMethod get = new GetMethod (URL + " /Jobs.json" );
17
35
int status = client .executeMethod (get );
18
36
if ( status != 200 )
19
37
throw new Exception (get .getStatusText ());
@@ -36,4 +54,46 @@ public static class Job {
36
54
String end_time ;
37
55
String exception ;
38
56
}
57
+
58
+ /**
59
+ * Exports a model to a JSON file.
60
+ */
61
+ static void exportModel () throws Exception {
62
+ HttpClient client = new HttpClient ();
63
+ GetMethod get = new GetMethod (URL + "/2/ExportModel.json?model=MyInitialNeuralNet" );
64
+ int status = client .executeMethod (get );
65
+ if ( status != 200 )
66
+ throw new Exception (get .getStatusText ());
67
+ JsonObject response = (JsonObject ) new JsonParser ().parse (new InputStreamReader (get .getResponseBodyAsStream ()));
68
+ JsonElement model = response .get ("model" );
69
+ JsonWriter writer = new JsonWriter (new FileWriter (JSON_FILE ));
70
+ writer .setLenient (true );
71
+ writer .setIndent (" " );
72
+ Streams .write (model , writer );
73
+ writer .close ();
74
+ get .releaseConnection ();
75
+ }
76
+
77
+ /**
78
+ * Imports a model from a JSON file.
79
+ */
80
+ public static void importModel () throws Exception {
81
+ // Upload file to H2O
82
+ HttpClient client = new HttpClient ();
83
+ PostMethod post = new PostMethod (URL + "/Upload.json?key=" + JSON_FILE .getName ());
84
+ Part [] parts = { new FilePart (JSON_FILE .getName (), JSON_FILE ) };
85
+ post .setRequestEntity (new MultipartRequestEntity (parts , post .getParams ()));
86
+ if ( 200 != client .executeMethod (post ) )
87
+ throw new RuntimeException ("Request failed: " + post .getStatusLine ());
88
+ post .releaseConnection ();
89
+
90
+ // Parse the key into a model
91
+ GetMethod get = new GetMethod (URL + "/2/ImportModel.json?" //
92
+ + "destination_key=MyImportedNeuralNet&" //
93
+ + "type=NeuralNetModel&" //
94
+ + "json=" + JSON_FILE .getName ());
95
+ if ( 200 != client .executeMethod (get ) )
96
+ throw new RuntimeException ("Request failed: " + get .getStatusLine ());
97
+ get .releaseConnection ();
98
+ }
39
99
}
0 commit comments