use of com.google.bigtable.admin.v2.TableName in project dataproc-templates by GoogleCloudPlatform.
the class PubSubToBQ method writeToBQ.
public static void writeToBQ(JavaDStream<SparkPubsubMessage> pubSubStream, String outputProjectID, String pubSubBQOutputDataset, String PubSubBQOutputTable, Integer batchSize) {
pubSubStream.foreachRDD(new VoidFunction<JavaRDD<SparkPubsubMessage>>() {
@Override
public void call(JavaRDD<SparkPubsubMessage> sparkPubsubMessageJavaRDD) throws Exception {
sparkPubsubMessageJavaRDD.foreachPartition(new VoidFunction<Iterator<SparkPubsubMessage>>() {
@Override
public void call(Iterator<SparkPubsubMessage> sparkPubsubMessageIterator) throws Exception {
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
Table table = bigquery.getTable(pubSubBQOutputDataset, PubSubBQOutputTable);
TableName parentTable = TableName.of(outputProjectID, pubSubBQOutputDataset, PubSubBQOutputTable);
Schema schema = table.getDefinition().getSchema();
JsonStreamWriter writer = JsonStreamWriter.newBuilder(parentTable.toString(), schema).build();
JSONArray jsonArr = new JSONArray();
while (sparkPubsubMessageIterator.hasNext()) {
SparkPubsubMessage message = sparkPubsubMessageIterator.next();
JSONObject record = new JSONObject(new String(message.getData()));
jsonArr.put(record);
if (jsonArr.length() == batchSize) {
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
AppendRowsResponse response = future.get();
jsonArr = new JSONArray();
}
}
if (jsonArr.length() > 0) {
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
AppendRowsResponse response = future.get();
}
writer.close();
}
});
}
});
}
use of com.google.bigtable.admin.v2.TableName in project gapic-generator-java by googleapis.
the class BaseBigtableDataClientTest method readModifyWriteRowExceptionTest.
@Test
public void readModifyWriteRowExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockBigtable.addException(exception);
try {
TableName tableName = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]");
ByteString rowKey = ByteString.EMPTY;
List<ReadModifyWriteRule> rules = new ArrayList<>();
client.readModifyWriteRow(tableName, rowKey, rules);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.bigtable.admin.v2.TableName in project gapic-generator-java by googleapis.
the class BaseBigtableDataClientTest method readModifyWriteRowTest.
@Test
public void readModifyWriteRowTest() throws Exception {
ReadModifyWriteRowResponse expectedResponse = ReadModifyWriteRowResponse.newBuilder().setRow(Row.newBuilder().build()).build();
mockBigtable.addResponse(expectedResponse);
TableName tableName = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]");
ByteString rowKey = ByteString.EMPTY;
List<ReadModifyWriteRule> rules = new ArrayList<>();
ReadModifyWriteRowResponse actualResponse = client.readModifyWriteRow(tableName, rowKey, rules);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockBigtable.getRequests();
Assert.assertEquals(1, actualRequests.size());
ReadModifyWriteRowRequest actualRequest = ((ReadModifyWriteRowRequest) actualRequests.get(0));
Assert.assertEquals(tableName.toString(), actualRequest.getTableName());
Assert.assertEquals(rowKey, actualRequest.getRowKey());
Assert.assertEquals(rules, actualRequest.getRulesList());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.bigtable.admin.v2.TableName in project gapic-generator-java by googleapis.
the class BaseBigtableDataClientTest method mutateRowExceptionTest3.
@Test
public void mutateRowExceptionTest3() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockBigtable.addException(exception);
try {
TableName tableName = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]");
ByteString rowKey = ByteString.EMPTY;
List<Mutation> mutations = new ArrayList<>();
String appProfileId = "appProfileId704923523";
client.mutateRow(tableName, rowKey, mutations, appProfileId);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.bigtable.admin.v2.TableName in project gapic-generator-java by googleapis.
the class BaseBigtableDataClientTest method mutateRowTest3.
@Test
public void mutateRowTest3() throws Exception {
MutateRowResponse expectedResponse = MutateRowResponse.newBuilder().build();
mockBigtable.addResponse(expectedResponse);
TableName tableName = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]");
ByteString rowKey = ByteString.EMPTY;
List<Mutation> mutations = new ArrayList<>();
String appProfileId = "appProfileId704923523";
MutateRowResponse actualResponse = client.mutateRow(tableName, rowKey, mutations, appProfileId);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockBigtable.getRequests();
Assert.assertEquals(1, actualRequests.size());
MutateRowRequest actualRequest = ((MutateRowRequest) actualRequests.get(0));
Assert.assertEquals(tableName.toString(), actualRequest.getTableName());
Assert.assertEquals(rowKey, actualRequest.getRowKey());
Assert.assertEquals(mutations, actualRequest.getMutationsList());
Assert.assertEquals(appProfileId, actualRequest.getAppProfileId());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations