12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
- */
15
+ */
16
16
17
17
package com .m .examples .bigtable ;
18
18
29
29
import com .google .cloud .bigtable .data .v2 .models .RowCell ;
30
30
import com .google .cloud .bigtable .data .v2 .models .RowMutation ;
31
31
import java .io .IOException ;
32
+ import java .util .ArrayList ;
33
+ import java .util .List ;
32
34
33
35
// [END bigtable_hw_imports_veneer]
34
36
48
50
public class HelloWorld {
49
51
50
52
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" ;
52
55
private static final String ROW_KEY_PREFIX = "rowKey" ;
53
56
private final String tableId ;
54
57
private final BigtableDataClient dataClient ;
@@ -94,8 +97,13 @@ public void run() throws Exception {
94
97
createTable ();
95
98
writeToTable ();
96
99
readSingleRow ();
100
+ readSpecificCells ();
97
101
readTable ();
98
102
deleteTable ();
103
+ close ();
104
+ }
105
+
106
+ public void close () {
99
107
dataClient .close ();
100
108
adminClient .close ();
101
109
}
@@ -119,13 +127,15 @@ public void writeToTable() {
119
127
// [START bigtable_hw_write_rows_veneer]
120
128
try {
121
129
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 ] + "!" ;
124
133
RowMutation rowMutation =
125
134
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 );
127
137
dataClient .mutateRow (rowMutation );
128
- System .out .println (greetings [ i ] );
138
+ System .out .println (greeting );
129
139
}
130
140
} catch (NotFoundException e ) {
131
141
System .err .println ("Failed to write to non-existent table: " + e .getMessage ());
@@ -134,7 +144,7 @@ public void writeToTable() {
134
144
}
135
145
136
146
/** Demonstrates how to read a single row from a table. */
137
- public void readSingleRow () {
147
+ public Row readSingleRow () {
138
148
// [START bigtable_hw_get_by_key_veneer]
139
149
try {
140
150
System .out .println ("\n Reading a single row by row key" );
@@ -145,29 +155,56 @@ public void readSingleRow() {
145
155
"Family: %s Qualifier: %s Value: %s%n" ,
146
156
cell .getFamily (), cell .getQualifier ().toStringUtf8 (), cell .getValue ().toStringUtf8 ());
147
157
}
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 ;
148
180
} catch (NotFoundException e ) {
149
181
System .err .println ("Failed to read from a non-existent table: " + e .getMessage ());
182
+ return null ;
150
183
}
151
184
// [END bigtable_hw_get_by_key_veneer]
152
185
}
153
186
154
187
/** Demonstrates how to read an entire table. */
155
- public void readTable () {
188
+ public List < Row > readTable () {
156
189
// [START bigtable_hw_scan_all_veneer]
157
190
try {
158
191
System .out .println ("\n Reading the entire table" );
159
192
Query query = Query .create (tableId );
160
193
ServerStream <Row > rowStream = dataClient .readRows (query );
194
+ List <Row > tableRows = new ArrayList <>();
161
195
for (Row r : rowStream ) {
162
196
System .out .println ("Row Key: " + r .getKey ().toStringUtf8 ());
197
+ tableRows .add (r );
163
198
for (RowCell cell : r .getCells ()) {
164
199
System .out .printf (
165
200
"Family: %s Qualifier: %s Value: %s%n" ,
166
201
cell .getFamily (), cell .getQualifier ().toStringUtf8 (), cell .getValue ().toStringUtf8 ());
167
202
}
168
203
}
204
+ return tableRows ;
169
205
} catch (NotFoundException e ) {
170
206
System .err .println ("Failed to read a non-existent table: " + e .getMessage ());
207
+ return null ;
171
208
}
172
209
// [END bigtable_hw_scan_all_veneer]
173
210
}
0 commit comments