Search in sources :

Example 11 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class TableRowToStorageApiProtoTest method testNullRepeatedDescriptorFromTableSchema.

@Test
public void testNullRepeatedDescriptorFromTableSchema() throws Exception {
    TableRow repeatedRow = new TableRow().set("repeated1", null).set("repeated2", null).set("repeatednof1", null).set("repeatednof2", null);
    Descriptor descriptor = TableRowToStorageApiProto.getDescriptorFromTableSchema(REPEATED_MESSAGE_SCHEMA);
    DynamicMessage msg = TableRowToStorageApiProto.messageFromTableRow(descriptor, repeatedRow);
    Map<String, FieldDescriptor> fieldDescriptors = descriptor.getFields().stream().collect(Collectors.toMap(FieldDescriptor::getName, Functions.identity()));
    List<DynamicMessage> repeated1 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeated1"));
    assertTrue(repeated1.isEmpty());
    List<DynamicMessage> repeated2 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeated2"));
    assertTrue(repeated2.isEmpty());
    List<DynamicMessage> repeatednof1 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeatednof1"));
    assertTrue(repeatednof1.isEmpty());
    List<DynamicMessage> repeatednof2 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeatednof2"));
    assertTrue(repeatednof2.isEmpty());
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) DynamicMessage(com.google.protobuf.DynamicMessage) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

Example 12 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project druid by druid-io.

the class ProtobufReader method parseInputRows.

@Override
protected List<InputRow> parseInputRows(DynamicMessage intermediateRow) throws ParseException, JsonProcessingException {
    Map<String, Object> record;
    if (flattenSpec == null || JSONPathSpec.DEFAULT.equals(flattenSpec)) {
        try {
            record = CollectionUtils.mapKeys(intermediateRow.getAllFields(), k -> k.getJsonName());
        } catch (Exception ex) {
            throw new ParseException(null, ex, "Protobuf message could not be parsed");
        }
    } else {
        try {
            String json = JsonFormat.printer().print(intermediateRow);
            record = recordFlattener.flatten(OBJECT_MAPPER.readValue(json, JsonNode.class));
        } catch (InvalidProtocolBufferException e) {
            throw new ParseException(null, e, "Protobuf message could not be parsed");
        }
    }
    return Collections.singletonList(MapInputRowParser.parse(inputRowSchema, record));
}
Also used : DynamicMessage(com.google.protobuf.DynamicMessage) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ObjectFlattener(org.apache.druid.java.util.common.parsers.ObjectFlattener) CollectionUtils(org.apache.druid.utils.CollectionUtils) InputRowSchema(org.apache.druid.data.input.InputRowSchema) Iterators(com.google.common.collect.Iterators) ByteBuffer(java.nio.ByteBuffer) JSONPathSpec(org.apache.druid.java.util.common.parsers.JSONPathSpec) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) CloseableIterator(org.apache.druid.java.util.common.parsers.CloseableIterator) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) MapInputRowParser(org.apache.druid.data.input.impl.MapInputRowParser) JSONFlattenerMaker(org.apache.druid.java.util.common.parsers.JSONFlattenerMaker) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) IOUtils(org.apache.commons.io.IOUtils) InputRow(org.apache.druid.data.input.InputRow) List(java.util.List) IntermediateRowParsingReader(org.apache.druid.data.input.IntermediateRowParsingReader) CloseableIterators(org.apache.druid.java.util.common.CloseableIterators) JsonFormat(com.google.protobuf.util.JsonFormat) ObjectFlatteners(org.apache.druid.java.util.common.parsers.ObjectFlatteners) InputEntity(org.apache.druid.data.input.InputEntity) Collections(java.util.Collections) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 13 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project druid by druid-io.

the class SchemaRegistryBasedProtobufBytesDecoder method parse.

@Override
public DynamicMessage parse(ByteBuffer bytes) {
    // ignore first \0 byte
    bytes.get();
    // extract schema registry id
    int id = bytes.getInt();
    // ignore \0 byte before PB message
    bytes.get();
    int length = bytes.limit() - 2 - 4;
    Descriptors.Descriptor descriptor;
    try {
        ProtobufSchema schema = (ProtobufSchema) registry.getSchemaById(id);
        descriptor = schema.toDescriptor();
    } catch (RestClientException e) {
        LOGGER.error(e.getMessage());
        throw new ParseException(null, e, "Fail to get protobuf schema because of can not connect to registry or failed http request!");
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
        throw new ParseException(null, e, "Fail to get protobuf schema because of invalid schema!");
    }
    try {
        byte[] rawMessage = new byte[length];
        bytes.get(rawMessage, 0, length);
        DynamicMessage message = DynamicMessage.parseFrom(descriptor, rawMessage);
        return message;
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        throw new ParseException(null, e, "Fail to decode protobuf message!");
    }
}
Also used : RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) Descriptors(com.google.protobuf.Descriptors) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) DynamicMessage(com.google.protobuf.DynamicMessage) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)

Example 14 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project tesla by linking12.

the class DynamicGrpcClient method doRemoteCall.

@Override
public String doRemoteCall(final FilterRpcDO rpcDo, final String jsonInput) {
    try {
        final String serviceName = rpcDo.getServiceName();
        final String methodName = rpcDo.getMethodName();
        final String group = rpcDo.getServiceGroup();
        final String version = rpcDo.getServiceVersion();
        Pair<Descriptor, Descriptor> inOutType = ProtobufUtil.resolveServiceInputOutputType(rpcDo);
        Descriptor inPutType = inOutType.getLeft();
        Descriptor outPutType = inOutType.getRight();
        MethodDescriptor<DynamicMessage, DynamicMessage> methodDesc = this.createGrpcMethodDescriptor(serviceName, methodName, inPutType, outPutType);
        DynamicMessage message = this.createGrpcDynamicMessage(inPutType, jsonInput);
        Message response = (Message) genricService.$invoke(serviceName, group, version, methodName, methodDesc, message);
        return JSON2PROTOBUF.printToString(response);
    } catch (IOException e) {
        throw new RpcServiceException(String.format("json covert to DynamicMessage failed! the json is :%s, the protobuf type is: %s", jsonInput), e);
    } catch (Throwable e) {
        throw new RpcFrameworkException(String.format("service definition is wrong,please check the proto file you update,service is %s, method is %s", rpcDo.getServiceName(), rpcDo.getMethodName()), e);
    }
}
Also used : RpcFrameworkException(com.quancheng.saluki.core.grpc.exception.RpcFrameworkException) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) Descriptor(com.google.protobuf.Descriptors.Descriptor) MethodDescriptor(io.grpc.MethodDescriptor) RpcServiceException(com.quancheng.saluki.core.grpc.exception.RpcServiceException) DynamicMessage(com.google.protobuf.DynamicMessage) IOException(java.io.IOException)

Example 15 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project BIMserver by opensourceBIM.

the class Handler method run.

@Override
public void run() {
    running = true;
    try {
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        String token = dis.readUTF();
        ReflectiveRpcChannel reflectiveRpcChannel = new ReflectiveRpcChannel(ServiceInterface.class, serviceFactory.get(token, AccessMethod.INTERNAL).get(ServiceInterface.class), protocolBuffersMetaData, servicesMap);
        while (running) {
            String serviceName = dis.readUTF();
            String methodName = dis.readUTF();
            MethodDescriptorContainer methodDescriptorContainer;
            try {
                methodDescriptorContainer = protocolBuffersMetaData.getMethod(serviceName, methodName);
                Builder newBuilder = DynamicMessage.newBuilder(methodDescriptorContainer.getInputDescriptor());
                newBuilder.mergeDelimitedFrom(dis);
                DynamicMessage request = newBuilder.build();
                Message response = reflectiveRpcChannel.callBlockingMethod(methodDescriptorContainer, request);
                response.writeDelimitedTo(socket.getOutputStream());
            } catch (ServiceNotFoundException e) {
                // TODO should return a usable pb message for the user
                LOGGER.error("", e);
            } catch (ServiceMethodNotFoundException e) {
                // TODO should return a usable pb message for the user
                LOGGER.error("", e);
            }
        }
    } catch (IOException e) {
        LOGGER.error("", e);
    } catch (UserException e) {
        LOGGER.error("", e);
    }
    socketNotificationsClient.notifyDisconnect();
}
Also used : ReflectiveRpcChannel(org.bimserver.shared.pb.ReflectiveRpcChannel) MethodDescriptorContainer(org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) ServiceNotFoundException(org.bimserver.shared.pb.ServiceNotFoundException) ServiceInterface(org.bimserver.shared.interfaces.ServiceInterface) Builder(com.google.protobuf.DynamicMessage.Builder) DynamicMessage(com.google.protobuf.DynamicMessage) IOException(java.io.IOException) UserException(org.bimserver.shared.exceptions.UserException) DataInputStream(java.io.DataInputStream) ServiceMethodNotFoundException(org.bimserver.shared.pb.ServiceMethodNotFoundException)

Aggregations

DynamicMessage (com.google.protobuf.DynamicMessage)29 Test (org.junit.Test)12 ByteString (com.google.protobuf.ByteString)11 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)11 Descriptor (com.google.protobuf.Descriptors.Descriptor)9 IOException (java.io.IOException)6 List (java.util.List)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)5 Message (com.google.protobuf.Message)5 Map (java.util.Map)4 TableRow (com.google.api.services.bigquery.model.TableRow)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 Builder (com.google.protobuf.DynamicMessage.Builder)3 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 DataHandler (javax.activation.DataHandler)3 Nullable (javax.annotation.Nullable)3 Descriptors (com.google.protobuf.Descriptors)2 JsonFormat (com.google.protobuf.util.JsonFormat)2