Search in sources :

Example 21 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class Reads method readRowRange.

public static void readRowRange(String projectId, String instanceId, String tableId) {
    String start = "phone#4c410523#20190501";
    String end = "phone#4c410523#201906201";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        Query query = Query.create(tableId).range(start, end);
        ServerStream<Row> rows = dataClient.readRows(query);
        for (Row row : rows) {
            printRow(row);
        }
    } catch (IOException e) {
        System.out.println("Unable to initialize service client, as a network error occurred: \n" + e.toString());
    }
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) Row(com.google.cloud.bigtable.data.v2.models.Row) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 22 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class NativeImageBigtableSample method insertData.

public static void insertData(BigtableDataClient client, String tableId, long timestamp, ImmutableMap<String, Long> dataWithLong, ImmutableMap<String, String> dataWithStrings) {
    String rowKey = String.format("phone#%d", timestamp);
    RowMutation rowMutation = RowMutation.create(tableId, rowKey);
    for (Entry<String, Long> longEntry : dataWithLong.entrySet()) {
        rowMutation.setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom(longEntry.getKey().getBytes()), timestamp, longEntry.getValue());
    }
    for (Entry<String, String> stringEntry : dataWithStrings.entrySet()) {
        rowMutation.setCell(COLUMN_FAMILY_NAME, stringEntry.getKey(), timestamp, stringEntry.getValue());
    }
    client.mutateRow(rowMutation);
    System.out.println("Successfully wrote row: " + rowKey);
}
Also used : RowMutation(com.google.cloud.bigtable.data.v2.models.RowMutation) ByteString(com.google.protobuf.ByteString)

Example 23 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class NativeImageBigtableSample method readData.

static void readData(BigtableDataClient client, String tableId) {
    Query query = Query.create(tableId).prefix("");
    ServerStream<Row> rows = client.readRows(query);
    System.out.println("Reading phone data in table:");
    for (Row row : rows) {
        System.out.println("Key: " + row.getKey().toStringUtf8());
        for (RowCell cell : row.getCells()) {
            System.out.printf("\t%s: %s @%s\n", cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
        }
        System.out.println();
    }
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) RowCell(com.google.cloud.bigtable.data.v2.models.RowCell) Row(com.google.cloud.bigtable.data.v2.models.Row)

Example 24 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class NativeImageBigtableSample method main.

/**
 * Entrypoint to the BigTable sample application.
 */
public static void main(String[] args) throws IOException {
    String projectId = ServiceOptions.getDefaultProjectId();
    BigtableTableAdminSettings adminClientSettings = BigtableTableAdminSettings.newBuilder().setInstanceId(INSTANCE_NAME).setProjectId(projectId).build();
    BigtableDataSettings clientSettings = BigtableDataSettings.newBuilder().setInstanceId(INSTANCE_NAME).setProjectId(projectId).build();
    BigtableInstanceAdminSettings instanceAdminSettings = BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId).build();
    BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminClientSettings);
    BigtableDataClient standardClient = BigtableDataClient.create(clientSettings);
    BigtableInstanceAdminClient instanceAdminClient = BigtableInstanceAdminClient.create(instanceAdminSettings);
    if (!instanceAdminClient.exists(INSTANCE_NAME)) {
        instanceAdminClient.createInstance(CreateInstanceRequest.of(INSTANCE_NAME).addCluster("cluster", "us-central1-f", 3, StorageType.SSD).setType(Instance.Type.PRODUCTION).addLabel("example", "instance_admin"));
    }
    String tableName = TABLE_NAME + UUID.randomUUID().toString().replace("-", "");
    createTable(adminClient, tableName);
    // Add data into table
    ImmutableMap<String, Long> dataWithLong = ImmutableMap.of("connected_cell", 1L, "connected_wifi", 1L);
    ImmutableMap<String, String> dataWithStrings = ImmutableMap.of("os_build", "PQ2A.190405.003");
    long timestamp = System.currentTimeMillis() * 1000;
    insertData(standardClient, tableName, timestamp, dataWithLong, dataWithStrings);
    readData(standardClient, tableName);
    // Clean up
    deleteTable(adminClient, tableName);
}
Also used : BigtableTableAdminSettings(com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings) BigtableInstanceAdminClient(com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient) ByteString(com.google.protobuf.ByteString) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) BigtableDataSettings(com.google.cloud.bigtable.data.v2.BigtableDataSettings) BigtableInstanceAdminSettings(com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings)

Example 25 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class ConfigureConnectionPool method configureConnectionPool.

public static void configureConnectionPool(String projectId, String instanceId) {
    // String projectId = "my-project-id";
    // String instanceId = "my-instance-id";
    BigtableDataSettings.Builder settingsBuilder = BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId);
    settingsBuilder.stubSettings().setTransportChannelProvider(EnhancedBigtableStubSettings.defaultGrpcTransportProviderBuilder().setPoolSize(10).build());
    BigtableDataSettings settings = settingsBuilder.build();
    try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
        InstantiatingGrpcChannelProvider provider = (InstantiatingGrpcChannelProvider) settings.getStubSettings().getTransportChannelProvider();
        int poolSize = provider.toBuilder().getPoolSize();
        System.out.println(String.format("Connected with pool size of %d", poolSize));
    } catch (IOException e) {
        System.out.println("Error during ConfigureConnectionPool: \n" + e.toString());
    }
}
Also used : InstantiatingGrpcChannelProvider(com.google.api.gax.grpc.InstantiatingGrpcChannelProvider) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) BigtableDataSettings(com.google.cloud.bigtable.data.v2.BigtableDataSettings)

Aggregations

BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)27 Row (com.google.cloud.bigtable.data.v2.models.Row)18 IOException (java.io.IOException)13 Query (com.google.cloud.bigtable.data.v2.models.Query)8 BulkMutation (com.google.cloud.bigtable.data.v2.models.BulkMutation)7 ByteString (com.google.protobuf.ByteString)7 Test (org.junit.Test)6 BigtableTableAdminClient (com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)4 BigtableDataSettings (com.google.cloud.bigtable.data.v2.BigtableDataSettings)4 CreateTableRequest (com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)3 RowCell (com.google.cloud.bigtable.data.v2.models.RowCell)3 RowMutation (com.google.cloud.bigtable.data.v2.models.RowMutation)3 RowMutationEntry (com.google.cloud.bigtable.data.v2.models.RowMutationEntry)3 BeforeClass (org.junit.BeforeClass)3 ApiFuture (com.google.api.core.ApiFuture)2 Filters (com.google.cloud.bigtable.data.v2.models.Filters)2 ReadModifyWriteRow (com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)2 ArrayList (java.util.ArrayList)2 BatcherImpl (com.google.api.gax.batching.BatcherImpl)1 FlowControlEventStats (com.google.api.gax.batching.FlowControlEventStats)1