use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-docs-samples by GoogleCloudPlatform.
the class ReadsTest method beforeClass.
@BeforeClass
public static void beforeClass() throws IOException {
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
instanceId = requireEnv(INSTANCE_ENV);
try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) {
CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME);
adminClient.createTable(createTableRequest);
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
BulkMutation bulkMutation = BulkMutation.create(TABLE_ID).add("phone#4c410523#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190405.003")).add("phone#4c410523#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190405.004")).add("phone#4c410523#20190505", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000")).add("phone#5c10102#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190401.002")).add("phone#5c10102#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000"));
dataClient.bulkMutateRows(bulkMutation);
}
} catch (Exception e) {
System.out.println("Error during beforeClass: \n" + e.toString());
throw (e);
}
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-docs-samples by GoogleCloudPlatform.
the class MemcachedTest method beforeClass.
@BeforeClass
public static void beforeClass() {
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
instanceId = requireEnv(INSTANCE_ENV);
try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) {
CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME);
adminClient.createTable(createTableRequest);
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
String rowkey = "phone#4c410523#20190501";
RowMutation rowMutation = RowMutation.create(TABLE_ID, rowkey).setCell(COLUMN_FAMILY_NAME, "os_build", "PQ2A.190405.003");
dataClient.mutateRow(rowMutation);
}
String[] dockerCommand = (String.format("docker run --name %s -itd --rm --publish 11211:11211 sameersbn/memcached:latest", MEMCACHED_CONTAINER_NAME)).split(" ");
Process process = new ProcessBuilder(dockerCommand).start();
process.waitFor();
} catch (Exception e) {
System.out.println("Error during beforeClass: \n" + e.toString());
}
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project testcontainers-java by testcontainers.
the class BigtableEmulatorContainerTest method testSimple.
// }
@Test
public // testWithEmulatorContainer {
void testSimple() throws IOException, InterruptedException, ExecutionException {
ManagedChannel channel = ManagedChannelBuilder.forTarget(emulator.getEmulatorEndpoint()).usePlaintext().build();
TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
NoCredentialsProvider credentialsProvider = NoCredentialsProvider.create();
try {
createTable(channelProvider, credentialsProvider, "test-table");
BigtableDataClient client = BigtableDataClient.create(BigtableDataSettings.newBuilderForEmulator(emulator.getHost(), emulator.getEmulatorPort()).setProjectId(PROJECT_ID).setInstanceId(INSTANCE_ID).build());
client.mutateRow(RowMutation.create("test-table", "1").setCell("name", "firstName", "Ray"));
Row row = client.readRow("test-table", "1");
List<RowCell> cells = row.getCells("name", "firstName");
assertThat(cells).isNotNull().hasSize(1);
assertThat(cells.get(0).getValue().toStringUtf8()).isEqualTo("Ray");
} finally {
channel.shutdown();
}
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class SampleRowsIT method test.
@Test
public void test() throws InterruptedException, ExecutionException, TimeoutException {
BigtableDataClient client = testEnvRule.env().getDataClient();
String rowPrefix = UUID.randomUUID().toString();
// Create some data so that sample row keys has something to show
List<ApiFuture<?>> futures = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
ApiFuture<Void> future = client.mutateRowAsync(RowMutation.create(testEnvRule.env().getTableId(), rowPrefix + "-" + i).setCell(testEnvRule.env().getFamilyId(), "", "value"));
futures.add(future);
}
ApiFutures.allAsList(futures).get(1, TimeUnit.MINUTES);
ApiFuture<List<KeyOffset>> future = client.sampleRowKeysAsync(testEnvRule.env().getTableId());
List<KeyOffset> results = future.get(1, TimeUnit.MINUTES);
assertThat(results).isNotEmpty();
assertThat(results.get(results.size() - 1).getOffsetBytes()).isGreaterThan(0L);
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class BulkReadIT method testBulkRead.
@Test
public void testBulkRead() throws InterruptedException, ExecutionException {
BigtableDataClient client = testEnvRule.env().getDataClient();
String family = testEnvRule.env().getFamilyId();
String rowPrefix = UUID.randomUUID().toString();
int numRows = 10;
BulkMutation bulkMutation = BulkMutation.create(testEnvRule.env().getTableId());
List<Row> expectedRows = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
bulkMutation.add(RowMutationEntry.create(rowPrefix + "-" + i).setCell(family, "qualifier", 10_000L, "value-" + i));
expectedRows.add(Row.create(ByteString.copyFromUtf8(rowPrefix + "-" + i), ImmutableList.of(RowCell.create(family, ByteString.copyFromUtf8("qualifier"), 10_000L, ImmutableList.<String>of(), ByteString.copyFromUtf8("value-" + i)))));
}
client.bulkMutateRows(bulkMutation);
try (Batcher<ByteString, Row> batcher = client.newBulkReadRowsBatcher(testEnvRule.env().getTableId())) {
List<ApiFuture<Row>> rowFutures = new ArrayList<>(numRows);
for (int rowCount = 0; rowCount < numRows; rowCount++) {
ApiFuture<Row> entryResponse = batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + rowCount));
rowFutures.add(entryResponse);
}
batcher.flush();
List<Row> actualRows = ApiFutures.allAsList(rowFutures).get();
assertThat(actualRows).isEqualTo(expectedRows);
// To verify non-existent and duplicate row keys
rowFutures = new ArrayList<>();
// non-existent row key
rowFutures.add(batcher.add(ByteString.copyFromUtf8(UUID.randomUUID().toString())));
// duplicate row key
rowFutures.add(batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + 0)));
rowFutures.add(batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + 0)));
batcher.flush();
actualRows = ApiFutures.allAsList(rowFutures).get();
assertThat(actualRows.get(0)).isNull();
assertThat(actualRows.get(1)).isEqualTo(expectedRows.get(0));
assertThat(actualRows.get(2)).isEqualTo(expectedRows.get(0));
}
}
Aggregations