Skip to content

Commit 562c8d2

Browse files
Connecting to bigtable (#296)
* First draft of moving connecting to Cloud Bigtable code samples to github * adding test for the helper with the configuration * reformat code with google-java-format * Writing tests for the bigtable helpers * Add region tag for creating connection object * Editing projectID for readme * Enabling app profile id * Formatting code * Refining connecting examples by putting in same class, renaming the maven project, and reformating the code * Rename package and fix tests * Upgrade to Java Hbase client for Cloud Bigtable 1.5 * Adding bigtable.version to properties * Adding additional example with/without app profiles * Remove confusing comment about app profiles * Connecting to Bigtable in one line
0 parents  commit 562c8d2

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2018 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.cloud.bigtable.connect;
18+
19+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
20+
import com.google.cloud.bigtable.hbase.BigtableOptionsFactory;
21+
import java.io.IOException;
22+
import org.apache.hadoop.conf.Configuration;
23+
import org.apache.hadoop.hbase.HBaseConfiguration;
24+
import org.apache.hadoop.hbase.client.Connection;
25+
import org.apache.hadoop.hbase.client.ConnectionFactory;
26+
27+
public class BigtableConnect {
28+
29+
public static String projectId;
30+
public static String instanceId;
31+
public static String appProfileId = "default";
32+
public static Connection connection = null;
33+
34+
public static void main(String... args) {
35+
projectId = args[0]; // my-gcp-project-id
36+
instanceId = args[1]; // my-bigtable-instance-id
37+
38+
if (args.length > 2) {
39+
appProfileId = args[2]; // my-bigtable-app-profile-id or default if not provided.
40+
}
41+
}
42+
43+
// [START bigtable_connect]
44+
public static void connect() throws IOException {
45+
connection = BigtableConfiguration.connect(projectId, instanceId);
46+
}
47+
// [END bigtable_connect]
48+
49+
// [START bigtable_connect_app_profile]
50+
public static void connectWithAppProfile() throws IOException {
51+
connection = BigtableConfiguration.connect(projectId, instanceId, appProfileId);
52+
}
53+
// [END bigtable_connect_app_profile]
54+
55+
// [START bigtable_connect_with_configuration]
56+
public static void connectWithConfiguration() throws IOException {
57+
// Define the HBase configuration with the projectID, instanceID, and optional appProfileID
58+
// from resources/hbase_site.xml
59+
Configuration config = HBaseConfiguration.create();
60+
connection = ConnectionFactory.createConnection(config);
61+
}
62+
// [END bigtable_connect_with_configuration]
63+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!--Define the projectID, instanceID, and appProfileID by using system properties
2+
e.g. -Dbigtable.projectID=test-project -Dbigtable.instanceID=test-instance
3+
-->
4+
<!--[START bigtable_hbase_site_xml]-->
5+
<configuration>
6+
<property>
7+
<name>hbase.client.connection.impl</name>
8+
<value>com.google.cloud.bigtable.hbase1_x.BigtableConnection</value>
9+
</property>
10+
<property>
11+
<name>google.bigtable.project.id</name>
12+
<value>${bigtable.projectID}</value>
13+
</property>
14+
<property>
15+
<name>google.bigtable.instance.id</name>
16+
<value>${bigtable.instanceID}</value>
17+
</property>
18+
<!--
19+
Include the following property if you are using app profiles.
20+
If you do not include the following property, the connection uses the
21+
default app profile.
22+
-->
23+
<property>
24+
<name>google.bigtable.app.profile.id</name>
25+
<value>${bigtable.appProfileID || "default"}</value>
26+
</property>
27+
</configuration>
28+
<!--[END bigtable_hbase_site_xml]-->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2018 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.cloud.bigtable.connect;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
25+
26+
@RunWith(JUnit4.class)
27+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
28+
public class BigtableConnectTest {
29+
30+
// provide your project id as an env var
31+
private final String projectId = System.getProperty("bigtable.projectID");
32+
private final String instanceId = System.getProperty("bigtable.instanceID");
33+
BigtableConnect helper;
34+
35+
@Before
36+
public void prepare() throws Exception {
37+
helper = new BigtableConnect();
38+
helper.main(projectId, instanceId);
39+
}
40+
41+
@Test
42+
public void connection() throws Exception {
43+
helper.connect();
44+
45+
assertThat(helper.connection.toString()).contains("project=" + projectId);
46+
assertThat(helper.connection.toString()).contains("instance=" + instanceId);
47+
}
48+
49+
@Test
50+
public void connectionWithAppProfile() throws Exception {
51+
helper.connectWithAppProfile();
52+
53+
assertThat(helper.connection.toString()).contains("project=" + projectId);
54+
assertThat(helper.connection.toString()).contains("instance=" + instanceId);
55+
}
56+
57+
@Test
58+
public void connectionWithConfiguration() throws Exception {
59+
helper.connectWithConfiguration();
60+
61+
assertThat(helper.connection.toString()).contains("project=" + projectId);
62+
assertThat(helper.connection.toString()).contains("instance=" + instanceId);
63+
}
64+
}

0 commit comments

Comments
 (0)