Search in sources :

Example 46 with TableName

use of com.google.bigtable.v2.TableName in project java-bigtable by googleapis.

the class BaseBigtableTableAdminClientTest method generateConsistencyTokenTest.

@Test
public void generateConsistencyTokenTest() throws Exception {
    GenerateConsistencyTokenResponse expectedResponse = GenerateConsistencyTokenResponse.newBuilder().setConsistencyToken("consistencyToken-1985152319").build();
    mockBigtableTableAdmin.addResponse(expectedResponse);
    TableName name = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]");
    GenerateConsistencyTokenResponse actualResponse = client.generateConsistencyToken(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockBigtableTableAdmin.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GenerateConsistencyTokenRequest actualRequest = ((GenerateConsistencyTokenRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : TableName(com.google.bigtable.admin.v2.TableName) AbstractMessage(com.google.protobuf.AbstractMessage) GenerateConsistencyTokenRequest(com.google.bigtable.admin.v2.GenerateConsistencyTokenRequest) GenerateConsistencyTokenResponse(com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse) Test(org.junit.Test)

Example 47 with TableName

use of com.google.bigtable.v2.TableName in project java-bigtable by googleapis.

the class BaseBigtableTableAdminClientTest method snapshotTableExceptionTest2.

@Test
public void snapshotTableExceptionTest2() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockBigtableTableAdmin.addException(exception);
    try {
        TableName name = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]");
        String cluster = "cluster872092154";
        String snapshotId = "snapshotId-1113817601";
        String description = "description-1724546052";
        client.snapshotTableAsync(name, cluster, snapshotId, description).get();
        Assert.fail("No exception raised");
    } catch (ExecutionException e) {
        Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
        InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
        Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
    }
}
Also used : TableName(com.google.bigtable.admin.v2.TableName) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 48 with TableName

use of com.google.bigtable.v2.TableName in project java-bigtable by googleapis.

the class BaseBigtableTableAdminClientTest method deleteTableExceptionTest.

@Test
public void deleteTableExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockBigtableTableAdmin.addException(exception);
    try {
        TableName name = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]");
        client.deleteTable(name);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : TableName(com.google.bigtable.admin.v2.TableName) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 49 with TableName

use of com.google.bigtable.v2.TableName in project beam by apache.

the class BigtableWriteIT method getTableData.

/**
 * Helper function to get a table's data.
 */
private List<KV<ByteString, ByteString>> getTableData(String tableName) throws IOException {
    // Add empty range to avoid TARGET_NOT_SET error
    RowRange range = RowRange.newBuilder().setStartKeyClosed(ByteString.EMPTY).setEndKeyOpen(ByteString.EMPTY).build();
    RowSet rowSet = RowSet.newBuilder().addRowRanges(range).build();
    ReadRowsRequest.Builder readRowsRequestBuilder = ReadRowsRequest.newBuilder().setTableName(tableName).setRows(rowSet);
    ResultScanner<Row> scanner = session.getDataClient().readRows(readRowsRequestBuilder.build());
    Row currentRow;
    List<KV<ByteString, ByteString>> tableData = new ArrayList<>();
    while ((currentRow = scanner.next()) != null) {
        ByteString key = currentRow.getKey();
        ByteString value = currentRow.getFamilies(0).getColumns(0).getCells(0).getValue();
        tableData.add(KV.of(key, value));
    }
    scanner.close();
    return tableData;
}
Also used : RowRange(com.google.bigtable.v2.RowRange) ByteString(com.google.protobuf.ByteString) RowSet(com.google.bigtable.v2.RowSet) ArrayList(java.util.ArrayList) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) Row(com.google.bigtable.v2.Row) KV(org.apache.beam.sdk.values.KV)

Example 50 with TableName

use of com.google.bigtable.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();
                }
            });
        }
    });
}
Also used : JSONArray(com.google.cloud.spark.bigquery.repackaged.org.json.JSONArray) AppendRowsResponse(com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse) JavaRDD(org.apache.spark.api.java.JavaRDD) TableName(com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.storage.v1beta2.TableName) SparkPubsubMessage(org.apache.spark.streaming.pubsub.SparkPubsubMessage) JSONObject(com.google.cloud.spark.bigquery.repackaged.org.json.JSONObject) VoidFunction(org.apache.spark.api.java.function.VoidFunction) Iterator(java.util.Iterator) JsonStreamWriter(com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter)

Aggregations

ByteString (com.google.protobuf.ByteString)48 Test (org.junit.Test)46 ArrayList (java.util.ArrayList)39 Mutation (com.google.bigtable.v2.Mutation)25 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)19 AbstractMessage (com.google.protobuf.AbstractMessage)19 StatusRuntimeException (io.grpc.StatusRuntimeException)19 TableName (com.google.bigtable.v2.TableName)18 TableName (com.google.bigtable.admin.v2.TableName)17 CheckAndMutateRowResponse (com.google.bigtable.v2.CheckAndMutateRowResponse)12 ReadModifyWriteRule (com.google.bigtable.v2.ReadModifyWriteRule)12 RowFilter (com.google.bigtable.v2.RowFilter)12 BaseBigtableDataClient (com.google.cloud.bigtable.data.v2.BaseBigtableDataClient)12 JSONArray (org.json.JSONArray)9 JSONObject (org.json.JSONObject)9 CheckAndMutateRowRequest (com.google.bigtable.v2.CheckAndMutateRowRequest)8 MutateRowResponse (com.google.bigtable.v2.MutateRowResponse)8 ReadModifyWriteRowResponse (com.google.bigtable.v2.ReadModifyWriteRowResponse)8 ExecutionException (java.util.concurrent.ExecutionException)8 TableName (com.google.cloud.bigquery.storage.v1.TableName)7