Search in sources :

Example 1 with StreamAppendClient

use of org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StreamAppendClient in project beam by apache.

the class FakeDatasetService method getStreamAppendClient.

@Override
public StreamAppendClient getStreamAppendClient(String streamName, Descriptor descriptor) {
    return new StreamAppendClient() {

        private Descriptor protoDescriptor;

        {
            this.protoDescriptor = descriptor;
        }

        @Override
        public ApiFuture<AppendRowsResponse> appendRows(long offset, ProtoRows rows) throws Exception {
            synchronized (FakeDatasetService.class) {
                Stream stream = writeStreams.get(streamName);
                if (stream == null) {
                    throw new RuntimeException("No such stream: " + streamName);
                }
                List<TableRow> tableRows = Lists.newArrayListWithExpectedSize(rows.getSerializedRowsCount());
                for (ByteString bytes : rows.getSerializedRowsList()) {
                    DynamicMessage msg = DynamicMessage.parseFrom(protoDescriptor, bytes);
                    if (msg.getUnknownFields() != null && !msg.getUnknownFields().asMap().isEmpty()) {
                        throw new RuntimeException("Unknown fields set in append! " + msg.getUnknownFields());
                    }
                    tableRows.add(TableRowToStorageApiProto.tableRowFromMessage(DynamicMessage.parseFrom(protoDescriptor, bytes)));
                }
                stream.appendRows(offset, tableRows);
            }
            return ApiFutures.immediateFuture(AppendRowsResponse.newBuilder().build());
        }

        @Override
        public void close() throws Exception {
        }

        @Override
        public void pin() {
        }

        @Override
        public void unpin() throws Exception {
        }
    };
}
Also used : ProtoRows(com.google.cloud.bigquery.storage.v1.ProtoRows) StreamAppendClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StreamAppendClient) ByteString(com.google.protobuf.ByteString) TableRow(com.google.api.services.bigquery.model.TableRow) AppendRowsResponse(com.google.cloud.bigquery.storage.v1.AppendRowsResponse) Descriptor(com.google.protobuf.Descriptors.Descriptor) WriteStream(com.google.cloud.bigquery.storage.v1.WriteStream) DynamicMessage(com.google.protobuf.DynamicMessage)

Aggregations

TableRow (com.google.api.services.bigquery.model.TableRow)1 AppendRowsResponse (com.google.cloud.bigquery.storage.v1.AppendRowsResponse)1 ProtoRows (com.google.cloud.bigquery.storage.v1.ProtoRows)1 WriteStream (com.google.cloud.bigquery.storage.v1.WriteStream)1 ByteString (com.google.protobuf.ByteString)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 StreamAppendClient (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StreamAppendClient)1