App Engine
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
App Engine에서의 Cloud Bigtable 사용과 관련된 샘플입니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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)."]]