Skip to content

Commit ae88f82

Browse files
Hbase client for Bigtable write samples (GoogleCloudPlatform#1445)
* Setup mvn package and copy in existing samples and tests * Fix style issues and ran tests * Writing quickstart + test for Bigtable * Updating pom with current versions * Adding default values for the system propertiesˆ * surefire instead of failsafe * setup table for testing using cbt tool, retriggering test * Removing unnecessary code from quickstart test that was causing failures * cleaning up quickstart * Changing test variables to use GOOGLE_CLOUD_PROJECT and the instance environment variable * HBase write samples and test cleanups * rename to folder to snippets * Add copyright to pom.xml * Cleaning up some of the existing files * POM touchups
1 parent a723c47 commit ae88f82

File tree

11 files changed

+445
-51
lines changed

11 files changed

+445
-51
lines changed

bigtable/hbase/snippets/pom.xml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.12</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.google.cloud.bigtable</groupId>
42+
<artifactId>bigtable-hbase-1.x</artifactId>
43+
<version>1.8.0</version>
44+
</dependency>
45+
</dependencies>
46+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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_hbase]
20+
21+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import org.apache.hadoop.hbase.TableName;
25+
import org.apache.hadoop.hbase.client.Connection;
26+
import org.apache.hadoop.hbase.client.Put;
27+
import org.apache.hadoop.hbase.client.Table;
28+
import org.apache.hadoop.hbase.util.Bytes;
29+
30+
public class WriteBatch {
31+
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");
32+
33+
public static void writeBatch(String projectId, String instanceId, String tableId) {
34+
// String projectId = "my-project-id";
35+
// String instanceId = "my-instance-id";
36+
// String tableId = "mobile-time-series";
37+
38+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
39+
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
40+
long timestamp = System.currentTimeMillis();
41+
42+
List<Put> puts = new ArrayList<Put>();
43+
puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190501")));
44+
puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190502")));
45+
46+
puts.get(0)
47+
.addColumn(
48+
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
49+
puts.get(0)
50+
.addColumn(
51+
COLUMN_FAMILY_NAME,
52+
Bytes.toBytes("os_build"),
53+
timestamp,
54+
Bytes.toBytes("12155.0.0-rc1"));
55+
56+
puts.get(1)
57+
.addColumn(
58+
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
59+
puts.get(1)
60+
.addColumn(
61+
COLUMN_FAMILY_NAME,
62+
Bytes.toBytes("os_build"),
63+
timestamp,
64+
Bytes.toBytes("12145.0.0-rc6"));
65+
66+
table.put(puts);
67+
68+
System.out.print("Successfully wrote 2 rows");
69+
} catch (Exception e) {
70+
System.out.println("Error during WriteBatch: \n" + e.toString());
71+
}
72+
}
73+
}
74+
75+
// [END bigtable_writes_batch_hbase]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_hbase]
20+
21+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
22+
import org.apache.hadoop.hbase.TableName;
23+
import org.apache.hadoop.hbase.client.Connection;
24+
import org.apache.hadoop.hbase.client.Put;
25+
import org.apache.hadoop.hbase.client.RowMutations;
26+
import org.apache.hadoop.hbase.client.Table;
27+
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
28+
import org.apache.hadoop.hbase.util.Bytes;
29+
30+
public class WriteConditionally {
31+
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");
32+
33+
public static void writeConditionally(String projectId, String instanceId, String tableId) {
34+
// String projectId = "my-project-id";
35+
// String instanceId = "my-instance-id";
36+
// String tableId = "mobile-time-series";
37+
38+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
39+
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
40+
long timestamp = System.currentTimeMillis();
41+
42+
String rowKey = "phone#4c410523#20190501";
43+
RowMutations mutations = new RowMutations(Bytes.toBytes(rowKey));
44+
45+
Put put = new Put(Bytes.toBytes(rowKey));
46+
put.addColumn(
47+
COLUMN_FAMILY_NAME, Bytes.toBytes("os_name"), timestamp, Bytes.toBytes("android"));
48+
mutations.add(put);
49+
50+
table.checkAndMutate(
51+
Bytes.toBytes(rowKey),
52+
COLUMN_FAMILY_NAME,
53+
Bytes.toBytes("os_build"),
54+
CompareOp.GREATER_OR_EQUAL,
55+
Bytes.toBytes("PQ2A.190405"),
56+
mutations);
57+
58+
System.out.print("Successfully updated row's os_name");
59+
60+
} catch (Exception e) {
61+
System.out.println("Error during WriteConditionally: \n" + e.toString());
62+
e.printStackTrace();
63+
}
64+
}
65+
}
66+
67+
// [END bigtable_writes_conditional_hbase]
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_hbase]
20+
21+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
22+
import org.apache.hadoop.hbase.TableName;
23+
import org.apache.hadoop.hbase.client.Connection;
24+
import org.apache.hadoop.hbase.client.Table;
25+
import org.apache.hadoop.hbase.util.Bytes;
26+
27+
public class WriteIncrement {
28+
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");
29+
30+
public static void writeIncrement(String projectId, String instanceId, String tableId) {
31+
// String projectId = "my-project-id";
32+
// String instanceId = "my-instance-id";
33+
// String tableId = "mobile-time-series";
34+
35+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
36+
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
37+
38+
String rowKey = "phone#4c410523#20190501";
39+
40+
table.incrementColumnValue(
41+
Bytes.toBytes(rowKey), COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), -1);
42+
43+
System.out.printf("Successfully updated row %s", rowKey);
44+
45+
} catch (Exception e) {
46+
System.out.println("Error during WriteIncrement: \n" + e.toString());
47+
}
48+
}
49+
}
50+
51+
// [END bigtable_writes_increment_hbase]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_simple_hbase]
20+
21+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
22+
import org.apache.hadoop.hbase.TableName;
23+
import org.apache.hadoop.hbase.client.Connection;
24+
import org.apache.hadoop.hbase.client.Put;
25+
import org.apache.hadoop.hbase.client.Table;
26+
import org.apache.hadoop.hbase.util.Bytes;
27+
28+
public class WriteSimple {
29+
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");
30+
31+
public static void writeSimple(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 (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
37+
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
38+
long timestamp = System.currentTimeMillis();
39+
40+
String rowKey = "phone#4c410523#20190501";
41+
42+
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));
47+
put.addColumn(
48+
COLUMN_FAMILY_NAME,
49+
Bytes.toBytes("os_build"),
50+
timestamp,
51+
Bytes.toBytes("PQ2A.190405.003"));
52+
table.put(put);
53+
54+
System.out.printf("Successfully wrote row %s", rowKey);
55+
56+
} catch (Exception e) {
57+
System.out.println("Error during WriteSimple: \n" + e.toString());
58+
}
59+
}
60+
}
61+
62+
// [END bigtable_writes_simple_hbase]

0 commit comments

Comments
 (0)