Skip to content

Commit a9001a8

Browse files
authored
docs: Expand hello world snippet to show how to access specific cells (#516)
* samples: Expand hello world snippet to show how to access specific cells by family & qualifier * samples: Expand hello world snippet to show how to access specific cells by family & qualifier
1 parent 4dd7638 commit a9001a8

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

samples/snippets/src/main/java/com/m/examples/bigtable/HelloWorld.java

+45-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
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

1717
package com.m.examples.bigtable;
1818

@@ -29,6 +29,8 @@
2929
import com.google.cloud.bigtable.data.v2.models.RowCell;
3030
import com.google.cloud.bigtable.data.v2.models.RowMutation;
3131
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
3234

3335
// [END bigtable_hw_imports_veneer]
3436

@@ -48,7 +50,8 @@
4850
public 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("\nWriting 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("\nReading 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("\nReading 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("\nReading 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
}

samples/snippets/src/test/java/com/m/examples/bigtable/HelloWorldTest.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@
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

1717
package com.m.examples.bigtable;
1818

19+
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
2022
import static org.junit.Assert.assertTrue;
2123

2224
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
2325
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
2426
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
2527
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2628
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
27-
import com.google.cloud.bigtable.data.v2.models.Row;
2829
import java.io.IOException;
2930
import java.util.Random;
3031
import java.util.concurrent.TimeUnit;
3132
import java.util.regex.Matcher;
3233
import java.util.regex.Pattern;
3334
import org.junit.After;
3435
import org.junit.AfterClass;
35-
import org.junit.AssumptionViolatedException;
3636
import org.junit.Before;
3737
import org.junit.BeforeClass;
3838
import org.junit.Test;
@@ -86,10 +86,11 @@ public void setup() throws IOException {
8686
}
8787

8888
@After
89-
public void after() {
89+
public void teardown() {
9090
if (adminClient.exists(tableId)) {
9191
adminClient.deleteTable(tableId);
9292
}
93+
helloWorld.close();
9394
}
9495

9596
@Test
@@ -103,18 +104,26 @@ public void testCreateAndDeleteTable() throws IOException {
103104
// Deletes a table.
104105
testHelloWorld.deleteTable();
105106
assertTrue(!adminClient.exists(testTable));
107+
testHelloWorld.close();
106108
}
107109

108110
@Test
109111
public void testWriteToTable() {
110112
// Writes to a table.
113+
assertNull(dataClient.readRow(tableId, "rowKey0"));
111114
helloWorld.writeToTable();
112-
Row row = dataClient.readRow(tableId, "rowKey0");
113-
assertNotNull(row);
115+
assertNotNull(dataClient.readRow(tableId, "rowKey0"));
114116
}
115117

116-
// TODO: add test for helloWorld.readSingleRow()
117-
// TODO: add test for helloWorld.readTable()
118+
@Test
119+
public void testReads() {
120+
assertEquals(0, helloWorld.readTable().size());
121+
helloWorld.writeToTable();
122+
123+
assertEquals(2, helloWorld.readSingleRow().getCells().size());
124+
assertEquals(1, helloWorld.readSpecificCells().size());
125+
assertEquals(3, helloWorld.readTable().size());
126+
}
118127

119128
@Test
120129
public void testRunDoesNotFail() throws Exception {

0 commit comments

Comments
 (0)