1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
15- */
15+ */
1616
1717package com .m .examples .bigtable ;
1818
2929import com .google .cloud .bigtable .data .v2 .models .RowCell ;
3030import com .google .cloud .bigtable .data .v2 .models .RowMutation ;
3131import java .io .IOException ;
32+ import java .util .ArrayList ;
33+ import java .util .List ;
3234
3335// [END bigtable_hw_imports_veneer]
3436
4850public class HelloWorld {
4951
5052 private static final String COLUMN_FAMILY = "cf1" ;
51- private static final String COLUMN_QUALIFIER = "greeting" ;
53+ private static final String COLUMN_QUALIFIER_GREETING = "greeting" ;
54+ private static final String COLUMN_QUALIFIER_NAME = "name" ;
5255 private static final String ROW_KEY_PREFIX = "rowKey" ;
5356 private final String tableId ;
5457 private final BigtableDataClient dataClient ;
@@ -94,8 +97,13 @@ public void run() throws Exception {
9497 createTable ();
9598 writeToTable ();
9699 readSingleRow ();
100+ readSpecificCells ();
97101 readTable ();
98102 deleteTable ();
103+ close ();
104+ }
105+
106+ public void close () {
99107 dataClient .close ();
100108 adminClient .close ();
101109 }
@@ -119,13 +127,15 @@ public void writeToTable() {
119127 // [START bigtable_hw_write_rows_veneer]
120128 try {
121129 System .out .println ("\n Writing some greetings to the table" );
122- String [] greetings = {"Hello World!" , "Hello Bigtable!" , "Hello Java!" };
123- for (int i = 0 ; i < greetings .length ; i ++) {
130+ String [] names = {"World" , "Bigtable" , "Java" };
131+ for (int i = 0 ; i < names .length ; i ++) {
132+ String greeting = "Hello " + names [i ] + "!" ;
124133 RowMutation rowMutation =
125134 RowMutation .create (tableId , ROW_KEY_PREFIX + i )
126- .setCell (COLUMN_FAMILY , COLUMN_QUALIFIER , greetings [i ]);
135+ .setCell (COLUMN_FAMILY , COLUMN_QUALIFIER_NAME , names [i ])
136+ .setCell (COLUMN_FAMILY , COLUMN_QUALIFIER_GREETING , greeting );
127137 dataClient .mutateRow (rowMutation );
128- System .out .println (greetings [ i ] );
138+ System .out .println (greeting );
129139 }
130140 } catch (NotFoundException e ) {
131141 System .err .println ("Failed to write to non-existent table: " + e .getMessage ());
@@ -134,7 +144,7 @@ public void writeToTable() {
134144 }
135145
136146 /** Demonstrates how to read a single row from a table. */
137- public void readSingleRow () {
147+ public Row readSingleRow () {
138148 // [START bigtable_hw_get_by_key_veneer]
139149 try {
140150 System .out .println ("\n Reading a single row by row key" );
@@ -145,29 +155,56 @@ public void readSingleRow() {
145155 "Family: %s Qualifier: %s Value: %s%n" ,
146156 cell .getFamily (), cell .getQualifier ().toStringUtf8 (), cell .getValue ().toStringUtf8 ());
147157 }
158+ return row ;
159+ } catch (NotFoundException e ) {
160+ System .err .println ("Failed to read from a non-existent table: " + e .getMessage ());
161+ return null ;
162+ }
163+ // [END bigtable_hw_get_by_key_veneer]
164+ }
165+
166+ /** Demonstrates how to access specific cells by family and qualifier. */
167+ public List <RowCell > readSpecificCells () {
168+ // [START bigtable_hw_get_by_key_veneer]
169+ try {
170+ System .out .println ("\n Reading specific cells by family and qualifier" );
171+ Row row = dataClient .readRow (tableId , ROW_KEY_PREFIX + 0 );
172+ System .out .println ("Row: " + row .getKey ().toStringUtf8 ());
173+ List <RowCell > cells = row .getCells (COLUMN_FAMILY , COLUMN_QUALIFIER_NAME );
174+ for (RowCell cell : cells ) {
175+ System .out .printf (
176+ "Family: %s Qualifier: %s Value: %s%n" ,
177+ cell .getFamily (), cell .getQualifier ().toStringUtf8 (), cell .getValue ().toStringUtf8 ());
178+ }
179+ return cells ;
148180 } catch (NotFoundException e ) {
149181 System .err .println ("Failed to read from a non-existent table: " + e .getMessage ());
182+ return null ;
150183 }
151184 // [END bigtable_hw_get_by_key_veneer]
152185 }
153186
154187 /** Demonstrates how to read an entire table. */
155- public void readTable () {
188+ public List < Row > readTable () {
156189 // [START bigtable_hw_scan_all_veneer]
157190 try {
158191 System .out .println ("\n Reading the entire table" );
159192 Query query = Query .create (tableId );
160193 ServerStream <Row > rowStream = dataClient .readRows (query );
194+ List <Row > tableRows = new ArrayList <>();
161195 for (Row r : rowStream ) {
162196 System .out .println ("Row Key: " + r .getKey ().toStringUtf8 ());
197+ tableRows .add (r );
163198 for (RowCell cell : r .getCells ()) {
164199 System .out .printf (
165200 "Family: %s Qualifier: %s Value: %s%n" ,
166201 cell .getFamily (), cell .getQualifier ().toStringUtf8 (), cell .getValue ().toStringUtf8 ());
167202 }
168203 }
204+ return tableRows ;
169205 } catch (NotFoundException e ) {
170206 System .err .println ("Failed to read a non-existent table: " + e .getMessage ());
207+ return null ;
171208 }
172209 // [END bigtable_hw_scan_all_veneer]
173210 }
0 commit comments