JAVA_SDK.md 2.9 KB
Newer Older
B
barrierye 已提交
1 2 3
# Paddle Serving Client Java SDK

([简体中文](JAVA_SDK_CN.md)|English)
B
barrierye 已提交
4

B
barrierye 已提交
5 6 7 8
Paddle Serving provides Java SDK,which supports predict on the Client side with Java language. This document shows how to use the Java SDK.

## Getting started

B
barrierye 已提交
9

B
barrierye 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
### Prerequisites

```
- Java 8 or higher
- Apache Maven
```

The following table shows compatibilities between Paddle Serving Server and Java SDK.

| Paddle Serving Server version | Java SDK version |
| :---------------------------: | :--------------: |
|             0.3.2             |      0.0.1       |

### Install Java SDK

B
barrierye 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
You can download jar and install it to the local Maven repository:

```shell
wget https://paddle-serving.bj.bcebos.com/jar/paddle-serving-sdk-java-0.0.1.jar
mvn install:install-file -Dfile=$PWD/paddle-serving-sdk-java-0.0.1.jar -DgroupId=io.paddle.serving.client -DartifactId=paddle-serving-sdk-java -Dversion=0.0.1 -Dpackaging=jar
```

Or compile from the source code and install it to the local Maven repository:

```shell
cd Serving/java
mvn compile
mvn install
```

### Maven configure
B
barrierye 已提交
41

B
fix doc  
barrierye 已提交
42
```text
B
barrierye 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
 <dependency>
     <groupId>io.paddle.serving.client</groupId>
     <artifactId>paddle-serving-sdk-java</artifactId>
     <version>0.0.1</version>
 </dependency>
```



## Example

Here we will show how to use Java SDK for Boston house price prediction. Please refer to [examples](../java/examples) folder for more examples.

### Get model

```shell
wget --no-check-certificate https://paddle-serving.bj.bcebos.com/uci_housing.tar.gz
tar -xzf uci_housing.tar.gz
```

### Start Python Server

```shell
python -m paddle_serving_server.serve --model uci_housing_model --port 9393 --use_multilang 
```

#### Client side code example

```java
import io.paddle.serving.client.*;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import java.util.*;

public class PaddleServingClientExample {
    public static void main( String[] args ) {
        float[] data = {0.0137f, -0.1136f, 0.2553f, -0.0692f,
            0.0582f, -0.0727f, -0.1583f, -0.0584f,
            0.6283f, 0.4919f, 0.1856f, 0.0795f, -0.0332f};
        INDArray npdata = Nd4j.createFromArray(data);
        HashMap<String, INDArray> feed_data
            = new HashMap<String, INDArray>() {{
                put("x", npdata);
            }};
        List<String> fetch = Arrays.asList("price");

        Client client = new Client();
        String target = "localhost:9393";
        boolean succ = client.connect(target);
        if (succ != true) {
            System.out.println("connect failed.");
            return ;
        }

        Map<String, INDArray> fetch_map = client.predict(feed_data, fetch);
        if (fetch_map == null) {
            System.out.println("predict failed.");
            return ;
        }

        for (Map.Entry<String, INDArray> e : fetch_map.entrySet()) {
            System.out.println("Key = " + e.getKey() + ", Value = " + e.getValue());
        }
        return ;
    }
}
```