首页 > 代码库 > grpc 创建者模式的使用
grpc 创建者模式的使用
复杂对象创建模式样例:
RequestInstance2.RequestInstance.Builder builder = RequestInstance2.RequestInstance.newBuilder();
for(int i=0;i<features.size();i++){
String feature = features.get(i);
String[] arr = feature.split(",");
if(arr==null)
continue;
RequestInstance2.Feature.Builder featuresBuilder = RequestInstance2.RequestInstance.newBuilder()
.getCommonFeature().toBuilder().addFeaturesBuilder(0);
for (int j = 0; j < arr.length; j++) {
featuresBuilder.setName(String.valueOf(j + 1)).setSValue(arr[j]);
}
builder.addInstanceFeature(i, RequestInstance2.RequestInstance.newBuilder()
.getCommonFeature().toBuilder().addFeatures(featuresBuilder.build()));
}
RequestInstance2.RequestInstance instance = builder.build();
byte[] bytes = instance.toByteArray();
ByteString bs = ByteString.copyFrom(bytes);
InputHead inputHead = InputHead.getDefaultInstance().toBuilder().setVersion(20).setBodySize(bs.size()).addAppKeys(0).setAppKeys(0, 85001).build();
Request request = Request.getDefaultInstance().toBuilder().setHead(inputHead).setBody(bs).build();
Response response = null;
try {
response = blockingStub.getPredictorValue(request);
} catch (StatusRuntimeException e) {
logger.error("RPC failed: "+ e.getStatus());
return null;
}
double value = http://www.mamicode.com/0d;
List<Double> doubles = new ArrayList<>();
if(response!=null){
ByteString tempBytes = response.getBody();
try {
pino_resp_pb.Resp resp = pino_resp_pb.Resp.parseFrom(tempBytes);
if(resp.getOutputCount()>0){
pino_resp_pb.Output output = resp.getOutput(0);
pino_resp_pb.Matrix matrix = output.getMatrix();
if(matrix!=null&&matrix.getDoubleValCount()>0){
for(int i=0;i<matrix.getDoubleValCount();i++){
value = http://www.mamicode.com/matrix.getDoubleVal(i);
doubles.add(value);
}
}
}
} catch (InvalidProtocolBufferException e) {
logger.error(e.getMessage(),e);
}
}
grpc 创建者模式的使用