Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Contoh: Lingkungan standar Java App Engine
Contoh ini adalah aplikasi App Engine, yang ditulis dalam
Java, yang menulis beberapa ucapan "halo dunia" ke tabel Bigtable
dan membacanya kembali. Aplikasi berjalan di Google Cloud dalam
lingkungan standar App Engine. Aplikasi
menggunakan runtime Java 8. Kode untuk aplikasi ini ada di repositori GitHub GoogleCloudPlatform/java-docs-samples, di direktori appengine-java8/bigtable.
BigtableHelper menyediakan metode untuk membuat koneksi ke Bigtable. Class ini juga menyimpan koneksi dalam cache dan menyediakan metode yang
akan mengambil koneksi yang di-cache jika ada. Membuat koneksi adalah
operasi yang relatif mahal, jadi sebagai praktik terbaik, Anda harus selalu membuat
satu koneksi dan menggunakannya kembali.
publicstaticvoidconnect()throwsIOException{if(PROJECT_ID==null||INSTANCE_ID==null){if(sc!=null){sc.log("environment variables BIGTABLE_PROJECT, and BIGTABLE_INSTANCE need to be defined.");}return;}connection=BigtableConfiguration.connect(PROJECT_ID,INSTANCE_ID);}/** * Get the shared connection to Cloud Bigtable. * * @return the connection */publicstaticConnectiongetConnection(){if(connection==null){try{connect();}catch(IOExceptione){if(sc!=null){sc.log("connect ",e);}}}if(connection==null){if(sc!=null){sc.log("BigtableHelper-No Connection");}}returnconnection;}
BigtableHelloWorld
BigtableHelloWorld digunakan untuk menulis serangkaian ucapan ke
Bigtable, membaca ucapan, lalu menampilkannya. Class ini
mendapatkan koneksi Bigtable dari BigtableHelper, menggunakan
koneksi untuk mendapatkan objek Table, yang memungkinkan Anda membaca dan menulis nilai,
lalu menggunakan objek Table untuk menulis ke dan membaca dari tabel.
/** * A minimal application that connects to Cloud Bigtable using the native HBase API and performs * some basic operations. */publicclassBigtableHelloWorld{// Refer to table metadata names by byte array in the HBase APIprivatestaticfinalbyte[]TABLE_NAME=Bytes.toBytes("Hello-Bigtable");privatestaticfinalbyte[]COLUMN_FAMILY_NAME=Bytes.toBytes("cf1");privatestaticfinalbyte[]COLUMN_NAME=Bytes.toBytes("greeting");// Write some friendly greetings to Cloud BigtableprivatestaticfinalString[]GREETINGS={"Hello World!","Hello Cloud Bigtable!","Hello HBase!"};/** * Create a table -- first time only. * * @param connection to Bigtable * @return the status */publicstaticStringcreate(Connectionconnection){try{// The admin API lets us create, manage and delete tablesAdminadmin=connection.getAdmin();// Create a table with a single column familyHTableDescriptordescriptor=newHTableDescriptor(TableName.valueOf(TABLE_NAME));descriptor.addFamily(newHColumnDescriptor(COLUMN_FAMILY_NAME));admin.createTable(descriptor);}catch(IOExceptione){return"Table exists.";}return"Create table "+Bytes.toString(TABLE_NAME);}/** Connects to Cloud Bigtable, runs some basic operations and prints the results. */publicstaticStringdoHelloWorld(){StringBuilderresult=newStringBuilder();// Create the Bigtable connection, use try-with-resources to make sure it gets closedConnectionconnection=BigtableHelper.getConnection();result.append(create(connection));result.append("<br><br>");try(Tabletable=connection.getTable(TableName.valueOf(TABLE_NAME))){// Retrieve the table we just created so we can do some reads and writes// Write some rows to the tableresult.append("Write some greetings to the table<br>");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));result.append("Get a single greeting by row key<br>");result.append(" ");result.append(rowKey);result.append("= ");result.append(greeting);result.append("<br>");// Now scan across all rows.Scanscan=newScan();result.append("Scan for all greetings:");ResultScannerscanner=table.getScanner(scan);for(Resultrow:scanner){byte[]valueBytes=row.getValue(COLUMN_FAMILY_NAME,COLUMN_NAME);result.append(" ");result.append(Bytes.toString(valueBytes));result.append("<br>");}}catch(IOExceptione){result.append("Exception while running HelloWorld: "+e.getMessage()+"<br>");result.append(e.toString());returnresult.toString();}returnresult.toString();}}
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-03-06 UTC."],[[["This Java application demonstrates how to interact with a Bigtable database from the Google App Engine standard environment using the Java 8 runtime."],["The `BigtableHelper` class manages the connection to Bigtable, caching it for reuse to improve performance."],["The `BigtableHelloWorld` class handles writing greetings to a Bigtable table, reading them back, and displaying them."],["The code utilizes the HBase API to create a table, write rows with column family, and scan/read the values from the table."],["The example is intended to showcase the basic operations with Bigtable, and recommends referring to the documentation on schema-design for better performance in production applications."]]],[]]