App Engine
Stay organized with collections
Save and categorize content based on your preferences.
Samples related to using Cloud Bigtable with App Engine.
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 page provides a Java code sample demonstrating basic interactions with Cloud Bigtable using the native HBase API within an App Engine environment.\u003c/p\u003e\n"],["\u003cp\u003eThe code sample includes creating a table, writing data (greetings) to the table with unique row keys, and retrieving data by row key and through a scan of all rows.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication for Bigtable can be set up using Application Default Credentials, which is covered in the documentation provided.\u003c/p\u003e\n"],["\u003cp\u003eThe code utilizes \u003ccode\u003eConnection\u003c/code\u003e and \u003ccode\u003eTable\u003c/code\u003e objects, and shows how to use \u003ccode\u003ePut\u003c/code\u003e and \u003ccode\u003eGet\u003c/code\u003e methods to write and read data, respectively.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation link provides further details for using Java App Engine standard environment with Cloud Bigtable, and also a link to where the client libraries can be found.\u003c/p\u003e\n"]]],[],null,["Samples related to using Cloud Bigtable with App Engine.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Example: Java App Engine standard environment](/bigtable/docs/samples-java-appengine-standard)\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 /**\n * A minimal application that connects to Cloud Bigtable using the native HBase API and performs\n * some basic operations.\n */\n public class BigtableHelloWorld {\n\n // Refer to table metadata names by byte array in the HBase API\n private static final byte[] TABLE_NAME = Bytes.toBytes(\"Hello-Bigtable\");\n private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes(\"cf1\");\n private static final byte[] COLUMN_NAME = Bytes.toBytes(\"greeting\");\n\n // Write some friendly greetings to Cloud Bigtable\n private static final String[] GREETINGS = {\n \"Hello World!\", \"Hello Cloud Bigtable!\", \"Hello HBase!\"\n };\n\n /**\n * Create a table -- first time only.\n *\n * @param connection to Bigtable\n * @return the status\n */\n public static String create(Connection connection) {\n try {\n // The admin API lets us create, manage and delete tables\n Admin admin = connection.getAdmin();\n\n // Create a table with a single column family\n HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));\n descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));\n\n admin.createTable(descriptor);\n } catch (IOException e) {\n return \"Table exists.\";\n }\n return \"Create table \" + Bytes.toString(TABLE_NAME);\n }\n\n /** Connects to Cloud Bigtable, runs some basic operations and prints the results. */\n public static String doHelloWorld() {\n\n StringBuilder result = new StringBuilder();\n\n // Create the Bigtable connection, use try-with-resources to make sure it gets closed\n Connection connection = BigtableHelper.getConnection();\n result.append(create(connection));\n result.append(\"\u003cbr\u003e\u003cbr\u003e\");\n try (Table table = connection.getTable(TableName.valueOf(TABLE_NAME))) {\n\n // Retrieve the table we just created so we can do some reads and writes\n\n // Write some rows to the table\n result.append(\"Write some greetings to the table\u003cbr\u003e\");\n for (int i = 0; i \u003c GREETINGS.length; i++) {\n // Each row has a unique row key.\n //\n // Note: This example uses sequential numeric IDs for simplicity, but\n // this can result in poor performance in a production application.\n // Since rows are stored in sorted order by key, sequential keys can\n // result in poor distribution of operations across nodes.\n //\n // For more information about how to design a Bigtable schema for the\n // best performance, see the documentation:\n //\n // https://cloud.google.com/bigtable/docs/schema-design\n String rowKey = \"greeting\" + i;\n\n // Put a single row into the table. We could also pass a list of Puts to write a batch.\n Put put = new Put(Bytes.toBytes(rowKey));\n put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, Bytes.toBytes(GREETINGS[i]));\n table.put(put);\n }\n\n // Get the first greeting by row key\n String rowKey = \"greeting0\";\n Result getResult = table.get(new Get(Bytes.toBytes(rowKey)));\n String greeting = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));\n result.append(\"Get a single greeting by row key\u003cbr\u003e\");\n\n result.append(\" \");\n result.append(rowKey);\n result.append(\"= \");\n result.append(greeting);\n result.append(\"\u003cbr\u003e\");\n\n // Now scan across all rows.\n Scan scan = new Scan();\n\n result.append(\"Scan for all greetings:\");\n ResultScanner scanner = table.getScanner(scan);\n for (Result row : scanner) {\n byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);\n result.append(\" \");\n result.append(Bytes.toString(valueBytes));\n result.append(\"\u003cbr\u003e\");\n }\n\n } catch (IOException e) {\n result.append(\"Exception while running HelloWorld: \" + e.getMessage() + \"\u003cbr\u003e\");\n result.append(e.toString());\n return result.toString();\n }\n\n return result.toString();\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)."]]