Bigtable quickstart (HBase)
Stay organized with collections
Save and categorize content based on your preferences.
Quickstart for Cloud Bigtable which shows connecting to an instance and reading a row from a table.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis quickstart demonstrates how to connect to a Cloud Bigtable instance using the native HBase API.\u003c/p\u003e\n"],["\u003cp\u003eThe code sample shows how to read a specific row from an existing table within the Bigtable instance.\u003c/p\u003e\n"],["\u003cp\u003eThe example requires setting up Application Default Credentials for authentication with Bigtable.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code is written in Java and utilizes the Bigtable client library.\u003c/p\u003e\n"],["\u003cp\u003eTo find code samples for other Google Cloud products, the Google Cloud sample browser is recommended for searching and filtering.\u003c/p\u003e\n"]]],[],null,["Quickstart for Cloud Bigtable which shows connecting to an instance and reading a row from a table.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Bigtable client libraries](/bigtable/docs/reference/libraries)\n\nCode sample \n\nJava\n\n\nTo learn how to install and use the client library for Bigtable, see\n[Bigtable client libraries](/bigtable/docs/reference/libraries).\n\n\nTo authenticate to Bigtable, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package com.example.cloud.bigtable.quickstart;\n\n import com.google.cloud.bigtable.hbase.BigtableConfiguration;\n\n import java.io.IOException;\n import org.apache.hadoop.hbase.TableName;\n import org.apache.hadoop.hbase.client.Connection;\n import org.apache.hadoop.hbase.client.Get;\n import org.apache.hadoop.hbase.client.Result;\n import org.apache.hadoop.hbase.client.Table;\n import org.apache.hadoop.hbase.util.Bytes;\n\n /**\n * A quickstart application that shows connecting to a Cloud Bigtable instance\n * using the native HBase API to read a row from a table.\n */\n public class Quickstart {\n\n public static void main(String... args) {\n\n String projectId = args[0]; // my-gcp-project-id\n String instanceId = args[1]; // my-bigtable-instance-id\n String tableId = args[2]; // my-bigtable-table-id\n\n // Create a connection to the Cloud Bigtable instance.\n // Use try-with-resources to make sure the connection is closed correctly\n try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {\n\n System.out.println(\"--- Connection established with Bigtable Instance ---\");\n // Create a connection to the table that already exists\n // Use try-with-resources to make sure the connection to the table is closed correctly\n try (Table table = connection.getTable(TableName.valueOf(tableId))) {\n\n // Read a row\n String rowKey = \"r1\";\n System.out.printf(\"--- Reading for row-key: %s for provided table: %s ---\\n\",\n rowKey, tableId);\n\n // Retrieve the result\n Result result = table.get(new Get(Bytes.toBytes(rowKey)));\n\n // Convert row data to string\n String rowValue = Bytes.toString(result.value());\n\n System.out.printf(\"Scanned value for Row r1: %s \\n\", rowValue);\n\n System.out.println(\" --- Finished reading row --- \");\n\n } catch (IOException e) {\n // handle exception while connecting to a table\n throw e;\n }\n } catch (IOException e) {\n System.err.println(\"Exception while running quickstart: \" + e.getMessage());\n e.printStackTrace();\n }\n }\n }\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigtable)."]]