Search in sources :

Example 1 with BigQueryConverters

use of com.google.cloud.teleport.v2.transforms.BigQueryConverters in project DataflowTemplates by GoogleCloudPlatform.

the class StreamingDataGeneratorWriteToBigQuery method expand.

@Override
public PDone expand(PCollection<byte[]> fakeMessages) {
    WriteResult writeResults = fakeMessages.apply("Write Json messsages", BigQueryIO.<byte[]>write().to(getPipelineOptions().getOutputTableSpec()).withMethod(Method.STREAMING_INSERTS).ignoreInsertIds().withCreateDisposition(CreateDisposition.CREATE_NEVER).withWriteDisposition(WriteDisposition.valueOf(getPipelineOptions().getWriteDisposition())).withFailedInsertRetryPolicy(InsertRetryPolicy.retryTransientErrors()).withExtendedErrorInfo().withFormatFunction((message) -> {
        TableRow row = null;
        try {
            row = TableRowJsonCoder.of().decode(new ByteArrayInputStream(message), Coder.Context.OUTER);
        } catch (IOException e) {
            throw new RuntimeException("Failed converting to TableRow with an error:" + e.getMessage());
        }
        return row;
    }));
    // Write errors to Dead Letter table
    writeResults.getFailedInsertsWithErr().apply("Convert to FailSafe Element", MapElements.into(FAILSAFE_ELEMENT_CODER.getEncodedTypeDescriptor()).via(BigQueryConverters::wrapBigQueryInsertError)).setCoder(FAILSAFE_ELEMENT_CODER).apply("Write Failed Records", ErrorConverters.WriteStringMessageErrors.newBuilder().setErrorRecordsTable(MoreObjects.firstNonNull(getPipelineOptions().getOutputDeadletterTable(), getPipelineOptions().getOutputTableSpec() + DEFAULT_DEADLETTER_TABLE_SUFFIX)).setErrorRecordsTableSchema(// i.e schema in above method
    SchemaUtils.DEADLETTER_SCHEMA).build());
    return PDone.in(fakeMessages.getPipeline());
}
Also used : Method(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.Method) MapElements(org.apache.beam.sdk.transforms.MapElements) PDone(org.apache.beam.sdk.values.PDone) BigQueryIO(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO) InsertRetryPolicy(org.apache.beam.sdk.io.gcp.bigquery.InsertRetryPolicy) MoreObjects(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.MoreObjects) Coder(org.apache.beam.sdk.coders.Coder) IOException(java.io.IOException) CreateDisposition(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.CreateDisposition) WriteResult(org.apache.beam.sdk.io.gcp.bigquery.WriteResult) PCollection(org.apache.beam.sdk.values.PCollection) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) PTransform(org.apache.beam.sdk.transforms.PTransform) SchemaUtils(com.google.cloud.teleport.v2.utils.SchemaUtils) ByteArrayInputStream(java.io.ByteArrayInputStream) FailsafeElementCoder(com.google.cloud.teleport.v2.coders.FailsafeElementCoder) WriteDisposition(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.WriteDisposition) TableRow(com.google.api.services.bigquery.model.TableRow) AutoValue(com.google.auto.value.AutoValue) StreamingDataGenerator(com.google.cloud.teleport.v2.templates.StreamingDataGenerator) TableRowJsonCoder(org.apache.beam.sdk.io.gcp.bigquery.TableRowJsonCoder) WriteResult(org.apache.beam.sdk.io.gcp.bigquery.WriteResult) ByteArrayInputStream(java.io.ByteArrayInputStream) TableRow(com.google.api.services.bigquery.model.TableRow) IOException(java.io.IOException)

Aggregations

TableRow (com.google.api.services.bigquery.model.TableRow)1 AutoValue (com.google.auto.value.AutoValue)1 FailsafeElementCoder (com.google.cloud.teleport.v2.coders.FailsafeElementCoder)1 StreamingDataGenerator (com.google.cloud.teleport.v2.templates.StreamingDataGenerator)1 SchemaUtils (com.google.cloud.teleport.v2.utils.SchemaUtils)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Coder (org.apache.beam.sdk.coders.Coder)1 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)1 BigQueryIO (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO)1 CreateDisposition (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.CreateDisposition)1 Method (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.Method)1 WriteDisposition (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.WriteDisposition)1 InsertRetryPolicy (org.apache.beam.sdk.io.gcp.bigquery.InsertRetryPolicy)1 TableRowJsonCoder (org.apache.beam.sdk.io.gcp.bigquery.TableRowJsonCoder)1 WriteResult (org.apache.beam.sdk.io.gcp.bigquery.WriteResult)1 MapElements (org.apache.beam.sdk.transforms.MapElements)1 PTransform (org.apache.beam.sdk.transforms.PTransform)1 PCollection (org.apache.beam.sdk.values.PCollection)1 PDone (org.apache.beam.sdk.values.PDone)1