App Engine
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
與搭配使用 Cloud Bigtable 和 App Engine 相關的範例。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
Java
如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章。
如要向 Bigtable 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","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,["# App Engine\n\nSamples related to using Cloud Bigtable with App Engine.\n\nExplore further\n---------------\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-----------\n\n### Java\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\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigtable)."]]