ahmedabu98 commented on code in PR #27866:
URL: https://github.com/apache/beam/pull/27866#discussion_r1286368137
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIOWriteTest.java:
##########
@@ -2980,6 +2986,99 @@ public void
testBatchStorageWriteWithMultipleAppendsPerStream() throws Exception
containsInAnyOrder(Iterables.toArray(rows, TableRow.class)));
}
+ @Test
+ public void testWriteProtos() throws Exception {
+ BigQueryIO.Write.Method method =
+ useStreaming
+ ? (useStorageApi
+ ? (useStorageApiApproximate
+ ? Method.STORAGE_API_AT_LEAST_ONCE
+ : Method.STORAGE_WRITE_API)
+ : Method.STREAMING_INSERTS)
+ : useStorageApi ? Method.STORAGE_WRITE_API : Method.FILE_LOADS;
+ Function<Integer, Proto3SchemaMessages.Primitive> getPrimitive =
+ (Integer i) ->
+ Proto3SchemaMessages.Primitive.newBuilder()
+ .setPrimitiveDouble(i)
+ .setPrimitiveFloat(i)
+ .setPrimitiveInt32(i)
+ .setPrimitiveInt64(i)
+ .setPrimitiveUint32(i)
+ .setPrimitiveUint64(i)
+ .setPrimitiveSint32(i)
+ .setPrimitiveSint64(i)
+ .setPrimitiveFixed32(i)
+ .setPrimitiveFixed64(i)
+ .setPrimitiveSint32(i)
+ .setPrimitiveSint64(i)
+ .setPrimitiveBool(true)
+ .setPrimitiveString(Integer.toString(i))
+ .setPrimitiveBytes(
+
ByteString.copyFrom(Integer.toString(i).getBytes(StandardCharsets.UTF_8)))
+ .build();
+ Function<Integer, TableRow> getPrimitiveRow =
+ (Integer i) ->
+ new TableRow()
+ .set("primitive_double", Double.valueOf(i).toString())
+ .set("primitive_float", Float.valueOf(i).toString())
+ .set("primitive_int32", i.toString())
+ .set("primitive_int64", i.toString())
+ .set("primitive_uint32", i.toString())
+ .set("primitive_uint64", i.toString())
+ .set("primitive_sint32", i.toString())
+ .set("primitive_sint64", i.toString())
+ .set("primitive_fixed32", i.toString())
+ .set("primitive_fixed64", i.toString())
+ .set("primitive_sint32", i.toString())
+ .set("primitive_sint64", i.toString())
+ .set("primitive_bool", "true")
+ .set("primitive_string", i.toString())
+ .set(
+ "primitive_bytes",
+ BaseEncoding.base64()
+ .encode(
+
ByteString.copyFrom(i.toString().getBytes(StandardCharsets.UTF_8))
+ .toByteArray()));
+
+ List<Proto3SchemaMessages.Primitive> nestedItems =
+ Lists.newArrayList(getPrimitive.apply(1), getPrimitive.apply(2),
getPrimitive.apply(3));
+
+ Iterable<Proto3SchemaMessages.Nested> items =
+ nestedItems.stream()
+ .map(
+ p ->
+ Proto3SchemaMessages.Nested.newBuilder()
+ .setNested(p)
+ .addAllNestedList(Lists.newArrayList(p, p, p))
+ .build())
+ .collect(Collectors.toList());
+
+ List<TableRow> expectedNestedTableRows =
+ Lists.newArrayList(
+ getPrimitiveRow.apply(1), getPrimitiveRow.apply(2),
getPrimitiveRow.apply(3));
+ Iterable<TableRow> expectedItems =
+ expectedNestedTableRows.stream()
+ .map(
+ p ->
+ new TableRow().set("nested", p).set("nested_list",
Lists.newArrayList(p, p, p)))
+ .collect(Collectors.toList());
+
+ BigQueryIO.Write<Proto3SchemaMessages.Nested> write =
+ BigQueryIO.writeProtos(Proto3SchemaMessages.Nested.class)
+ .to("dataset-id.table-id")
+ .withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED)
+ .withMethod(method)
+ .withoutValidation()
+ .withTestServices(fakeBqServices);
+
+ p.apply(Create.of(items)).apply("WriteToBQ", write);
+ p.run();
+
+ assertThat(
+ fakeDatasetService.getAllRows("project-id", "dataset-id", "table-id"),
+ containsInAnyOrder(Iterables.toArray(expectedItems, TableRow.class)));
+ }
Review Comment:
This looks good! We also need a test for using an input schema that contains
non-primitives
##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIOWriteTest.java:
##########
@@ -1090,13 +1094,15 @@ public void runTestWriteAvro(boolean schemaFromView)
throws Exception {
p.run();
+ System.err.println(
+ "ALLROWS " + fakeDatasetService.getAllRows("project-id", "dataset-id",
"table-id"));
Review Comment:
cleanup
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]