-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSampleCode.java
More file actions
46 lines (41 loc) · 1.57 KB
/
SampleCode.java
File metadata and controls
46 lines (41 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import com.dato.deploy.PredictiveServiceClient;
import org.json.simple.JSONObject;
import com.dato.deploy.PredictiveServiceClientResponse;
import com.dato.deploy.PredictiveServiceClientException;
/*
* A sample Java main application that connects to a running
* Dato Predictive Services. This example assumes an "add" function
* already deployed in the predictive service. The schema of "add" function
* in Python is as follow:
*
* def add(a,b):
* return a + b
*
* Replace "end_point" and "api_key" to the corresponding values in your
* predictive service. To run this Java main application using maven:
*
* >>> mvn compile
* >>> mvn exec:java -Dexec.mainClass="SampleCode"
*/
public class SampleCode {
public static String end_point = "http://localhost:9005";
public static String api_key = "api_key";
public static void main(String args[]) {
PredictiveServiceClient client = new PredictiveServiceClient(end_point,api_key,false);
System.out.println("schema version:" + client.getSchema());
JSONObject request = new JSONObject();
request.put("a", 10);
request.put("b", 2);
PredictiveServiceClientResponse response = client.query("add",request);
if (response.getStatusCode() == 200) {
// successfully connected
JSONObject results = response.getResult();
System.out.println(results.toString());
System.exit(0);
} else {
throw new PredictiveServiceClientException(
"Error connecting to service: response: " +
response.getErrorMessage());
}
}
}