Search in sources :

Example 1 with FailsafeJsonToTableRow

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

the class BigQueryConvertersTest method testFailsafeJsonToTableRowValidInput.

/**
 * Tests the {@link BigQueryConverters.FailsafeJsonToTableRow} transform with good input.
 */
@Test
@Category(NeedsRunner.class)
public void testFailsafeJsonToTableRowValidInput() {
    // Test input
    final String payload = "{\"ticker\": \"GOOGL\", \"price\": 1006.94}";
    final Map<String, String> attributes = ImmutableMap.of("id", "0xDb12", "type", "stock");
    final PubsubMessage message = new PubsubMessage(payload.getBytes(), attributes);
    final FailsafeElement<PubsubMessage, String> input = FailsafeElement.of(message, payload);
    // Expected Output
    TableRow expectedRow = new TableRow().set("ticker", "GOOGL").set("price", 1006.94);
    // Register the coder for the pipeline. This prevents having to invoke .setCoder() on
    // many transforms.
    FailsafeElementCoder<PubsubMessage, String> coder = FailsafeElementCoder.of(PubsubMessageWithAttributesCoder.of(), StringUtf8Coder.of());
    CoderRegistry coderRegistry = pipeline.getCoderRegistry();
    coderRegistry.registerCoderForType(coder.getEncodedTypeDescriptor(), coder);
    // Build the pipeline
    PCollectionTuple output = pipeline.apply("CreateInput", Create.of(input).withCoder(coder)).apply("JsonToTableRow", FailsafeJsonToTableRow.<PubsubMessage>newBuilder().setSuccessTag(TABLE_ROW_TAG).setFailureTag(FAILSAFE_ELM_TAG).build());
    // Assert
    PAssert.that(output.get(TABLE_ROW_TAG)).containsInAnyOrder(expectedRow);
    PAssert.that(output.get(FAILSAFE_ELM_TAG)).empty();
    // Execute the test
    pipeline.run();
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) FailsafeJsonToTableRow(com.google.cloud.teleport.v2.transforms.BigQueryConverters.FailsafeJsonToTableRow) TableRow(com.google.api.services.bigquery.model.TableRow) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) PubsubMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

TableRow (com.google.api.services.bigquery.model.TableRow)1 FailsafeJsonToTableRow (com.google.cloud.teleport.v2.transforms.BigQueryConverters.FailsafeJsonToTableRow)1 CoderRegistry (org.apache.beam.sdk.coders.CoderRegistry)1 PubsubMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage)1 PCollectionTuple (org.apache.beam.sdk.values.PCollectionTuple)1 Test (org.junit.Test)1 Category (org.junit.experimental.categories.Category)1