Skip to content

Commit a15074f

Browse files
Bigtable Write samples (GoogleCloudPlatform#1513)
* Bigtable Writes samples for Java Client * Adding increment comment * Correcting bytes written in previous tests
1 parent 109a86b commit a15074f

File tree

10 files changed

+420
-61
lines changed

10 files changed

+420
-61
lines changed

bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteBatch.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,21 @@ public static void writeBatch(String projectId, String instanceId, String tableI
3838
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
3939
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
4040
long timestamp = System.currentTimeMillis();
41+
byte[] one = new byte[] {0, 0, 0, 0, 0, 0, 0, 1};
4142

4243
List<Put> puts = new ArrayList<Put>();
4344
puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190501")));
4445
puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190502")));
4546

46-
puts.get(0)
47-
.addColumn(
48-
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
47+
puts.get(0).addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, one);
4948
puts.get(0)
5049
.addColumn(
5150
COLUMN_FAMILY_NAME,
5251
Bytes.toBytes("os_build"),
5352
timestamp,
5453
Bytes.toBytes("12155.0.0-rc1"));
5554

56-
puts.get(1)
57-
.addColumn(
58-
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
55+
puts.get(1).addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, one);
5956
puts.get(1)
6057
.addColumn(
6158
COLUMN_FAMILY_NAME,

bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteSimple.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ public static void writeSimple(String projectId, String instanceId, String table
3636
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
3737
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
3838
long timestamp = System.currentTimeMillis();
39+
byte[] one = new byte[] {0, 0, 0, 0, 0, 0, 0, 1};
3940

4041
String rowKey = "phone#4c410523#20190501";
41-
4242
Put put = new Put(Bytes.toBytes(rowKey));
43-
put.addColumn(
44-
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), timestamp, Bytes.toBytes(1));
45-
put.addColumn(
46-
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
43+
44+
put.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), timestamp, one);
45+
put.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, one);
4746
put.addColumn(
4847
COLUMN_FAMILY_NAME,
4948
Bytes.toBytes("os_build"),

bigtable/hbase/snippets/src/test/java/com/example/bigtable/WritesTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,31 @@ public static void afterClass() {
8989
}
9090

9191
@Test
92-
public void testWriteSimple() {
92+
public void test1_WriteSimple() {
9393
WriteSimple.writeSimple(projectId, instanceId, TABLE_ID);
9494

9595
String output = bout.toString();
9696
assertThat(output, CoreMatchers.containsString("Successfully wrote row"));
9797
}
9898

9999
@Test
100-
public void testWriteBatch() {
100+
public void test2_WriteBatch() {
101101
WriteBatch.writeBatch(projectId, instanceId, TABLE_ID);
102102

103103
String output = bout.toString();
104104
assertThat(output, CoreMatchers.containsString("Successfully wrote 2 rows"));
105105
}
106106

107107
@Test
108-
public void testWriteConditionally() {
108+
public void test3_WriteConditionally() {
109109
WriteConditionally.writeConditionally(projectId, instanceId, TABLE_ID);
110110

111111
String output = bout.toString();
112112
assertThat(output, CoreMatchers.containsString("Successfully updated row's os_name"));
113113
}
114114

115115
@Test
116-
public void testWriteIncrement() {
116+
public void test4_WriteIncrement() {
117117
WriteIncrement.writeIncrement(projectId, instanceId, TABLE_ID);
118118

119119
String output = bout.toString();

bigtable/pom.xml

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,4 @@
5555
<version>0.97.0</version>
5656
</dependency>
5757
</dependencies>
58-
59-
<build>
60-
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
61-
<plugins>
62-
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
63-
<plugin>
64-
<artifactId>maven-clean-plugin</artifactId>
65-
<version>3.1.0</version>
66-
</plugin>
67-
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
68-
<plugin>
69-
<artifactId>maven-resources-plugin</artifactId>
70-
<version>3.0.2</version>
71-
</plugin>
72-
<plugin>
73-
<artifactId>maven-compiler-plugin</artifactId>
74-
<version>3.8.0</version>
75-
</plugin>
76-
<plugin>
77-
<artifactId>maven-surefire-plugin</artifactId>
78-
<version>2.22.1</version>
79-
</plugin>
80-
<plugin>
81-
<artifactId>maven-jar-plugin</artifactId>
82-
<version>3.0.2</version>
83-
</plugin>
84-
<plugin>
85-
<artifactId>maven-install-plugin</artifactId>
86-
<version>2.5.2</version>
87-
</plugin>
88-
<plugin>
89-
<artifactId>maven-deploy-plugin</artifactId>
90-
<version>2.8.2</version>
91-
</plugin>
92-
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
93-
<plugin>
94-
<artifactId>maven-site-plugin</artifactId>
95-
<version>3.7.1</version>
96-
</plugin>
97-
<plugin>
98-
<artifactId>maven-project-info-reports-plugin</artifactId>
99-
<version>3.0.0</version>
100-
</plugin>
101-
</plugins>
102-
</pluginManagement>
103-
</build>
10458
</project>

bigtable/snippets/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google Inc.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<groupId>com.example.bigtable</groupId>
23+
<artifactId>docs-samples</artifactId>
24+
<version>1.0-SNAPSHOT</version>
25+
<name>docs-samples</name>
26+
27+
<properties>
28+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29+
<maven.compiler.source>1.8</maven.compiler.source>
30+
<maven.compiler.target>1.8</maven.compiler.target>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>junit</groupId>
36+
<artifactId>junit</artifactId>
37+
<version>4.13-beta-3</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-bigtable -->
41+
<dependency>
42+
<groupId>com.google.cloud</groupId>
43+
<artifactId>google-cloud-bigtable</artifactId>
44+
<version>0.97.0</version>
45+
</dependency>
46+
</dependencies>
47+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
// [START bigtable_writes_batch]
20+
21+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
22+
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
23+
import com.google.cloud.bigtable.data.v2.models.Mutation;
24+
import com.google.protobuf.ByteString;
25+
26+
public class WriteBatch {
27+
private static final String COLUMN_FAMILY_NAME = "stats_summary";
28+
29+
public static void writeBatch(String projectId, String instanceId, String tableId) {
30+
// String projectId = "my-project-id";
31+
// String instanceId = "my-instance-id";
32+
// String tableId = "mobile-time-series";
33+
34+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
35+
long timestamp = System.currentTimeMillis() * 1000;
36+
ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1});
37+
38+
BulkMutation bulkMutation =
39+
BulkMutation.create(tableId)
40+
.add(
41+
"tablet#a0b81f74#20190501",
42+
Mutation.create()
43+
.setCell(
44+
COLUMN_FAMILY_NAME,
45+
ByteString.copyFrom("connected_wifi".getBytes()),
46+
timestamp,
47+
one)
48+
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1"))
49+
.add(
50+
"tablet#a0b81f74#20190502",
51+
Mutation.create()
52+
.setCell(
53+
COLUMN_FAMILY_NAME,
54+
ByteString.copyFrom("connected_wifi".getBytes()),
55+
timestamp,
56+
one)
57+
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6"));
58+
59+
dataClient.bulkMutateRows(bulkMutation);
60+
61+
System.out.print("Successfully wrote 2 rows");
62+
} catch (Exception e) {
63+
System.out.println("Error during WriteBatch: \n" + e.toString());
64+
}
65+
}
66+
}
67+
68+
// [END bigtable_writes_batch]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
// [START bigtable_writes_conditional]
20+
21+
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
22+
23+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
24+
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
25+
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
26+
import com.google.cloud.bigtable.data.v2.models.Mutation;
27+
28+
public class WriteConditionally {
29+
private static final String COLUMN_FAMILY_NAME = "stats_summary";
30+
31+
public static void writeConditionally(String projectId, String instanceId, String tableId) {
32+
// String projectId = "my-project-id";
33+
// String instanceId = "my-instance-id";
34+
// String tableId = "mobile-time-series";
35+
36+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
37+
long timestamp = System.currentTimeMillis() * 1000;
38+
39+
String rowKey = "phone#4c410523#20190501";
40+
41+
Mutation mutation =
42+
Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android");
43+
44+
Filter filter =
45+
FILTERS
46+
.chain()
47+
.filter(FILTERS.family().exactMatch(COLUMN_FAMILY_NAME))
48+
.filter(FILTERS.qualifier().exactMatch("os_build"))
49+
.filter(FILTERS.value().regex("PQ2A\\..*"));
50+
51+
ConditionalRowMutation conditionalRowMutation =
52+
ConditionalRowMutation.create(tableId, rowKey).condition(filter).then(mutation);
53+
54+
boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);
55+
56+
System.out.printf("Successfully updated row's os_name: %b", success);
57+
58+
} catch (Exception e) {
59+
System.out.println("Error during WriteConditionally: \n" + e.toString());
60+
e.printStackTrace();
61+
}
62+
}
63+
}
64+
65+
// [END bigtable_writes_conditional]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
// [START bigtable_writes_increment]
20+
21+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
22+
import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow;
23+
import com.google.cloud.bigtable.data.v2.models.Row;
24+
import java.nio.charset.Charset;
25+
26+
public class WriteIncrement {
27+
private static final String COLUMN_FAMILY_NAME = "stats_summary";
28+
29+
public static void writeIncrement(String projectId, String instanceId, String tableId) {
30+
// String projectId = "my-project-id";
31+
// String instanceId = "my-instance-id";
32+
// String tableId = "mobile-time-series";
33+
34+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
35+
// Get an existing row that has a cell with an incrementable value. A value can be incremented
36+
// if it is encoded as a 64-bit big-endian signed integer.
37+
String rowKey = "phone#4c410523#20190501";
38+
ReadModifyWriteRow mutation =
39+
ReadModifyWriteRow.create(tableId, rowKey)
40+
.increment(COLUMN_FAMILY_NAME, "connected_cell", -1);
41+
Row success = dataClient.readModifyWriteRow(mutation);
42+
43+
System.out.printf(
44+
"Successfully updated row %s", success.getKey().toString(Charset.defaultCharset()));
45+
} catch (Exception e) {
46+
System.out.println("Error during WriteIncrement: \n" + e.toString());
47+
}
48+
}
49+
}
50+
51+
// [END bigtable_writes_increment]

0 commit comments

Comments
 (0)