Search in sources :

Example 1 with StreamWriter

use of com.google.cloud.bigquery.storage.v1.StreamWriter in project java-bigquerystorage by googleapis.

the class ParallelWriteCommittedStream method writeToStream.

private void writeToStream(BigQueryWriteClient client, WriteStream writeStream, long deadlineMillis) throws Throwable {
    LOG.info("Start writing to new stream:" + writeStream.getName());
    synchronized (this) {
        inflightCount = 0;
        successCount = 0;
        failureCount = 0;
        error = null;
        lastMetricsTimeMillis = System.currentTimeMillis();
        lastMetricsSuccessCount = 0;
        lastMetricsFailureCount = 0;
    }
    Descriptor descriptor = BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(writeStream.getTableSchema());
    ProtoSchema protoSchema = ProtoSchemaConverter.convert(descriptor);
    try (StreamWriter writer = StreamWriter.newBuilder(writeStream.getName()).setWriterSchema(protoSchema).setTraceId("SAMPLE:parallel_append").build()) {
        while (System.currentTimeMillis() < deadlineMillis) {
            synchronized (this) {
                if (error != null) {
                    // Stop writing once we get an error.
                    throw error;
                }
            }
            ApiFuture<AppendRowsResponse> future = writer.append(createAppendRows(descriptor), -1);
            synchronized (this) {
                inflightCount++;
            }
            ApiFutures.addCallback(future, new AppendCompleteCallback(this), MoreExecutors.directExecutor());
        }
    }
}
Also used : ProtoSchema(com.google.cloud.bigquery.storage.v1.ProtoSchema) StreamWriter(com.google.cloud.bigquery.storage.v1.StreamWriter) AppendRowsResponse(com.google.cloud.bigquery.storage.v1.AppendRowsResponse) Descriptor(com.google.protobuf.Descriptors.Descriptor) BQTableSchemaToProtoDescriptor(com.google.cloud.bigquery.storage.v1.BQTableSchemaToProtoDescriptor)

Aggregations

AppendRowsResponse (com.google.cloud.bigquery.storage.v1.AppendRowsResponse)1 BQTableSchemaToProtoDescriptor (com.google.cloud.bigquery.storage.v1.BQTableSchemaToProtoDescriptor)1 ProtoSchema (com.google.cloud.bigquery.storage.v1.ProtoSchema)1 StreamWriter (com.google.cloud.bigquery.storage.v1.StreamWriter)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1