-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bigtable quickstart
- Loading branch information
0 parents
commit 25f01ed
Showing
2 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
bigtable/hbase/snippets/src/main/java/com/example/cloud/bigtable/quickstart/Quickstart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2018 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. | ||
*/ | ||
|
||
// [START bigtable_quickstart] | ||
|
||
package com.example.cloud.bigtable.quickstart; | ||
|
||
import com.google.cloud.bigtable.hbase.BigtableConfiguration; | ||
|
||
import java.io.IOException; | ||
import org.apache.hadoop.hbase.TableName; | ||
import org.apache.hadoop.hbase.client.Connection; | ||
import org.apache.hadoop.hbase.client.Get; | ||
import org.apache.hadoop.hbase.client.Result; | ||
import org.apache.hadoop.hbase.client.Table; | ||
import org.apache.hadoop.hbase.util.Bytes; | ||
|
||
/** | ||
* A quickstart application that shows connecting to a Cloud Bigtable instance | ||
* using the native HBase API to read a row from a table. | ||
*/ | ||
public class Quickstart { | ||
|
||
public static void main(String... args) { | ||
|
||
String projectId = args[0]; // my-gcp-project-id | ||
String instanceId = args[1]; // my-bigtable-instance-id | ||
String tableId = args[2]; // my-bigtable-table-id | ||
|
||
// Create a connection to the Cloud Bigtable instance. | ||
// Use try-with-resources to make sure the connection is closed correctly | ||
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { | ||
|
||
// Create a connection to the table that already exists | ||
// Use try-with-resources to make sure the connection to the table is closed correctly | ||
try (Table table = connection.getTable(TableName.valueOf(tableId))) { | ||
|
||
// Read a row | ||
String rowKey = "r1"; | ||
|
||
// Retrieve the result | ||
Result result = table.get(new Get(Bytes.toBytes(rowKey))); | ||
|
||
// Convert row data to string | ||
String rowValue = Bytes.toString(result.value()); | ||
|
||
System.out.printf("Row r1: %s", rowValue); | ||
|
||
} catch (IOException e) { | ||
// handle exception while connecting to a table | ||
throw e; | ||
} | ||
} catch (IOException e) { | ||
System.err.println("Exception while running quickstart: " + e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
// [END bigtable_quickstart] |
99 changes: 99 additions & 0 deletions
99
...e/hbase/snippets/src/test/java/com/example/cloud/bigtable/quickstart/it/QuickstartIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2018 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.cloud.bigtable.quickstart.it; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.example.cloud.bigtable.quickstart.Quickstart; | ||
import com.google.cloud.bigtable.hbase.BigtableConfiguration; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import org.apache.hadoop.hbase.HColumnDescriptor; | ||
import org.apache.hadoop.hbase.HTableDescriptor; | ||
import org.apache.hadoop.hbase.TableName; | ||
import org.apache.hadoop.hbase.client.Admin; | ||
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; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Cloud Bigtable Quickstart integration tests | ||
*/ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class QuickstartIT { | ||
|
||
private final String instanceId = System.getProperty("bigtable.test.instance"); | ||
private final String tableId = formatForTest("my-table"); | ||
private final String columnFamilyName = "my-column-family"; | ||
private final String columnName = "my-column"; | ||
private final String data = "my-data"; | ||
|
||
// provide your project id as an env var | ||
private final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
|
||
private String formatForTest(String name) { | ||
return name + "-" + UUID.randomUUID().toString().substring(0, 20); | ||
} | ||
|
||
@Before | ||
public void prepare() throws Exception { | ||
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { | ||
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableId)); | ||
descriptor.addFamily(new HColumnDescriptor(columnFamilyName)); | ||
try (Admin admin = connection.getAdmin()) { | ||
admin.createTable(descriptor); | ||
// Retrieve the table we just created so we can do some reads and writes | ||
try (Table table = connection.getTable(TableName.valueOf(tableId))) { | ||
|
||
String rowKey = "r1"; | ||
|
||
Put put = new Put(Bytes.toBytes(rowKey)); | ||
put.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName), | ||
Bytes.toBytes(data)); | ||
table.put(put); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void quickStart() throws Exception { | ||
PrintStream stdOut = System.out; | ||
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
PrintStream out = new PrintStream(bout); | ||
System.setOut(out); | ||
Quickstart.main(projectId, instanceId, tableId); | ||
System.setOut(stdOut); | ||
assertTrue(bout.toString().contains(data)); | ||
} | ||
|
||
@After | ||
public void cleanup() throws Exception { | ||
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { | ||
connection.getAdmin().deleteTable(TableName.valueOf(tableId)); | ||
} | ||
} | ||
} |