Skip to content

Commit

Permalink
Hbase client for Bigtable write samples (#1445)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
billyjacobson authored Jun 18, 2019
1 parent a723c47 commit ae88f82
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 51 deletions.
46 changes: 46 additions & 0 deletions bigtable/hbase/snippets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2019 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.bigtable</groupId>
<artifactId>docs-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<name>docs-samples</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
<artifactId>bigtable-hbase-1.x</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigtable;

// [START bigtable_writes_batch_hbase]

import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class WriteBatch {
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");

public static void writeBatch(String projectId, String instanceId, String tableId) {
// String projectId = "my-project-id";
// String instanceId = "my-instance-id";
// String tableId = "mobile-time-series";

try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
long timestamp = System.currentTimeMillis();

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

puts.get(0)
.addColumn(
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
puts.get(0)
.addColumn(
COLUMN_FAMILY_NAME,
Bytes.toBytes("os_build"),
timestamp,
Bytes.toBytes("12155.0.0-rc1"));

puts.get(1)
.addColumn(
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
puts.get(1)
.addColumn(
COLUMN_FAMILY_NAME,
Bytes.toBytes("os_build"),
timestamp,
Bytes.toBytes("12145.0.0-rc6"));

table.put(puts);

System.out.print("Successfully wrote 2 rows");
} catch (Exception e) {
System.out.println("Error during WriteBatch: \n" + e.toString());
}
}
}

// [END bigtable_writes_batch_hbase]
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigtable;

// [START bigtable_writes_conditional_hbase]

import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.RowMutations;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;

public class WriteConditionally {
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");

public static void writeConditionally(String projectId, String instanceId, String tableId) {
// String projectId = "my-project-id";
// String instanceId = "my-instance-id";
// String tableId = "mobile-time-series";

try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
long timestamp = System.currentTimeMillis();

String rowKey = "phone#4c410523#20190501";
RowMutations mutations = new RowMutations(Bytes.toBytes(rowKey));

Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(
COLUMN_FAMILY_NAME, Bytes.toBytes("os_name"), timestamp, Bytes.toBytes("android"));
mutations.add(put);

table.checkAndMutate(
Bytes.toBytes(rowKey),
COLUMN_FAMILY_NAME,
Bytes.toBytes("os_build"),
CompareOp.GREATER_OR_EQUAL,
Bytes.toBytes("PQ2A.190405"),
mutations);

System.out.print("Successfully updated row's os_name");

} catch (Exception e) {
System.out.println("Error during WriteConditionally: \n" + e.toString());
e.printStackTrace();
}
}
}

// [END bigtable_writes_conditional_hbase]
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigtable;

// [START bigtable_writes_increment_hbase]

import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class WriteIncrement {
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");

public static void writeIncrement(String projectId, String instanceId, String tableId) {
// String projectId = "my-project-id";
// String instanceId = "my-instance-id";
// String tableId = "mobile-time-series";

try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));

String rowKey = "phone#4c410523#20190501";

table.incrementColumnValue(
Bytes.toBytes(rowKey), COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), -1);

System.out.printf("Successfully updated row %s", rowKey);

} catch (Exception e) {
System.out.println("Error during WriteIncrement: \n" + e.toString());
}
}
}

// [END bigtable_writes_increment_hbase]
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigtable;

// [START bigtable_writes_simple_hbase]

import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class WriteSimple {
private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");

public static void writeSimple(String projectId, String instanceId, String tableId) {
// String projectId = "my-project-id";
// String instanceId = "my-instance-id";
// String tableId = "mobile-time-series";

try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
long timestamp = System.currentTimeMillis();

String rowKey = "phone#4c410523#20190501";

Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), timestamp, Bytes.toBytes(1));
put.addColumn(
COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1));
put.addColumn(
COLUMN_FAMILY_NAME,
Bytes.toBytes("os_build"),
timestamp,
Bytes.toBytes("PQ2A.190405.003"));
table.put(put);

System.out.printf("Successfully wrote row %s", rowKey);

} catch (Exception e) {
System.out.println("Error during WriteSimple: \n" + e.toString());
}
}
}

// [END bigtable_writes_simple_hbase]
Loading

0 comments on commit ae88f82

Please sign in to comment.