// Create the Bigtable connection, use try-with-resources to make sure it gets closedtry(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){// The admin API lets us create, manage and delete tablesAdminadmin=connection.getAdmin();
テーブルの作成
Admin API を使用して、テーブルを作成します。
// Create a table with a single column familyHTableDescriptordescriptor=newHTableDescriptor(TableName.valueOf(TABLE_NAME));descriptor.addFamily(newHColumnDescriptor(COLUMN_FAMILY_NAME));System.out.println("HelloWorld: Create table "+descriptor.getNameAsString());admin.createTable(descriptor);
// Retrieve the table we just created so we can do some reads and writesTabletable=connection.getTable(TableName.valueOf(TABLE_NAME));// Write some rows to the tableSystem.out.println("HelloWorld: Write some greetings to the table");for(inti=0;i < GREETINGS.length;i++){// Each row has a unique row key.//// Note: This example uses sequential numeric IDs for simplicity, but// this can result in poor performance in a production application.// Since rows are stored in sorted order by key, sequential keys can// result in poor distribution of operations across nodes.//// For more information about how to design a Bigtable schema for the// best performance, see the documentation://// https://cloud.google.com/bigtable/docs/schema-designStringrowKey="greeting"+i;// Put a single row into the table. We could also pass a list of Puts to write a batch.Putput=newPut(Bytes.toBytes(rowKey));put.addColumn(COLUMN_FAMILY_NAME,COLUMN_NAME,Bytes.toBytes(GREETINGS[i]));table.put(put);}
キーによる行の読み取り
キーを使用して行を直接取得します。
// Get the first greeting by row keyStringrowKey="greeting0";ResultgetResult=table.get(newGet(Bytes.toBytes(rowKey)));Stringgreeting=Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME,COLUMN_NAME));System.out.println("Get a single greeting by row key");System.out.printf("\t%s = %s\n",rowKey,greeting);
すべてのテーブル行のスキャン
Scan クラスを使用して、行の範囲を取得します。
// Now scan across all rows.Scanscan=newScan();System.out.println("HelloWorld: Scan for all greetings:");ResultScannerscanner=table.getScanner(scan);for(Resultrow:scanner){byte[]valueBytes=row.getValue(COLUMN_FAMILY_NAME,COLUMN_NAME);System.out.println('\t'+Bytes.toString(valueBytes));}
テーブルの削除
Admin API を使用して、テーブルを削除します。
// Clean up by disabling and then deleting the tableSystem.out.println("HelloWorld: Delete the table");admin.disableTable(table.getName());admin.deleteTable(table.getName());
すべてを組み合わせる
コメントなしの例を以下に示します。
packagecom.example.bigtable;importcom.google.cloud.bigtable.hbase.BigtableConfiguration;importjava.io.IOException;importjava.util.UUID;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.Admin;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.client.Table;importorg.apache.hadoop.hbase.util.Bytes;publicclassHelloWorld{privatestaticfinalbyte[]TABLE_NAME=Bytes.toBytes("Hello-Bigtable-"+UUID.randomUUID().toString().substring(0,19));privatestaticfinalbyte[]COLUMN_FAMILY_NAME=Bytes.toBytes("cf1");privatestaticfinalbyte[]COLUMN_NAME=Bytes.toBytes("greeting");privatestaticfinalString[]GREETINGS={"Hello World!","Hello Cloud Bigtable!","Hello HBase!"};protectedstaticvoiddoHelloWorld(StringprojectId,StringinstanceId){try(Connectionconnection=BigtableConfiguration.connect(projectId,instanceId)){Adminadmin=connection.getAdmin();try{HTableDescriptordescriptor=newHTableDescriptor(TableName.valueOf(TABLE_NAME));descriptor.addFamily(newHColumnDescriptor(COLUMN_FAMILY_NAME));System.out.println("HelloWorld: Create table "+descriptor.getNameAsString());admin.createTable(descriptor);Tabletable=connection.getTable(TableName.valueOf(TABLE_NAME));System.out.println("HelloWorld: Write some greetings to the table");for(inti=0;i < GREETINGS.length;i++){StringrowKey="greeting"+i;Putput=newPut(Bytes.toBytes(rowKey));put.addColumn(COLUMN_FAMILY_NAME,COLUMN_NAME,Bytes.toBytes(GREETINGS[i]));table.put(put);}StringrowKey="greeting0";ResultgetResult=table.get(newGet(Bytes.toBytes(rowKey)));Stringgreeting=Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME,COLUMN_NAME));System.out.println("Get a single greeting by row key");System.out.printf("\t%s = %s\n",rowKey,greeting);Scanscan=newScan();System.out.println("HelloWorld: Scan for all greetings:");ResultScannerscanner=table.getScanner(scan);for(Resultrow:scanner){byte[]valueBytes=row.getValue(COLUMN_FAMILY_NAME,COLUMN_NAME);System.out.println('\t'+Bytes.toString(valueBytes));}System.out.println("HelloWorld: Delete the table");admin.disableTable(table.getName());admin.deleteTable(table.getName());}catch(IOExceptione){if(admin.tableExists(TableName.valueOf(TABLE_NAME))){System.out.println("HelloWorld: Cleaning up table");admin.disableTable(TableName.valueOf(TABLE_NAME));admin.deleteTable(TableName.valueOf(TABLE_NAME));}throwe;}}catch(IOExceptione){System.err.println("Exception while running HelloWorld: "+e.getMessage());e.printStackTrace();}}publicstaticvoidmain(String[]args){StringprojectId=requiredProperty("bigtable.projectID");StringinstanceId=requiredProperty("bigtable.instanceID");doHelloWorld(projectId,instanceId);}privatestaticStringrequiredProperty(Stringprop){Stringvalue=System.getProperty(prop);if(value==null){thrownewIllegalArgumentException("Missing required system property: "+prop);}returnvalue;}}
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-03-06 UTC。"],[[["This \"hello world\" application demonstrates the use of the Bigtable HBase client library for Java to interact with a Bigtable instance."],["The application showcases how to connect to a Bigtable instance, create a table, write data to it, and read the data back, all through the HBase APIs."],["Authentication is set up using the gcloud CLI and Application Default Credentials, and instructions for installation and initialization are provided."],["The application demonstrates creating a table with a single column family, writing rows to it, reading a specific row, and scanning all rows."],["The sample illustrates how to delete the table after operations are completed, using the Admin API."]]],[]]