Skip to content

Commit f81a9a5

Browse files
billyjacobsonlesv
andauthored
bigtable hbase: read and filter snippets (#1947)
* Read and filter snippets for Hbase Bigtable Reads done Filters WIP, issues with Pass and ColRange. Some nuances to sort out between HBase and other clients still * Filter snippets * Remove unused region tags Co-authored-by: Les Vogel <[email protected]>
1 parent f1def2a commit f81a9a5

File tree

7 files changed

+1670
-10
lines changed

7 files changed

+1670
-10
lines changed

bigtable/hbase/snippets/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,17 @@
4242
<artifactId>bigtable-hbase-1.x</artifactId>
4343
<version>1.12.1</version>
4444
</dependency>
45+
<dependency>
46+
<groupId>com.google.truth</groupId>
47+
<artifactId>truth</artifactId>
48+
<version>1.0</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-bigtable -->
52+
<dependency>
53+
<groupId>com.google.cloud</groupId>
54+
<artifactId>google-cloud-bigtable</artifactId>
55+
<version>1.8.0</version>
56+
</dependency>
4557
</dependencies>
4658
</project>

bigtable/hbase/snippets/src/main/java/com/example/bigtable/Filters.java

Lines changed: 430 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
// [START bigtable_reads_row]
20+
// [START bigtable_reads_row_partial]
21+
// [START bigtable_reads_rows]
22+
// [START bigtable_reads_row_range]
23+
// [START bigtable_reads_row_ranges]
24+
// [START bigtable_reads_prefix]
25+
// [START bigtable_reads_filter]
26+
27+
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
28+
import java.io.IOException;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
import org.apache.hadoop.hbase.Cell;
32+
import org.apache.hadoop.hbase.TableName;
33+
import org.apache.hadoop.hbase.client.Connection;
34+
import org.apache.hadoop.hbase.client.Get;
35+
import org.apache.hadoop.hbase.client.Result;
36+
import org.apache.hadoop.hbase.client.ResultScanner;
37+
import org.apache.hadoop.hbase.client.Scan;
38+
import org.apache.hadoop.hbase.client.Table;
39+
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
40+
import org.apache.hadoop.hbase.filter.Filter;
41+
import org.apache.hadoop.hbase.filter.MultiRowRangeFilter;
42+
import org.apache.hadoop.hbase.filter.MultiRowRangeFilter.RowRange;
43+
import org.apache.hadoop.hbase.filter.RegexStringComparator;
44+
import org.apache.hadoop.hbase.filter.ValueFilter;
45+
import org.apache.hadoop.hbase.util.Bytes;
46+
47+
public class Reads {
48+
// [END bigtable_reads_row]
49+
// [END bigtable_reads_row_partial]
50+
// [END bigtable_reads_rows]
51+
// [END bigtable_reads_row_range]
52+
// [END bigtable_reads_row_ranges]
53+
// [END bigtable_reads_prefix]
54+
// [END bigtable_reads_filter]
55+
56+
// [START bigtable_reads_row]
57+
public static void readRow() {
58+
// TODO(developer): Replace these variables before running the sample.
59+
String projectId = "my-project-id";
60+
String instanceId = "my-instance-id";
61+
String tableId = "mobile-time-series";
62+
readRow(projectId, instanceId, tableId);
63+
}
64+
65+
public static void readRow(String projectId, String instanceId, String tableId) {
66+
// Initialize client that will be used to send requests. This client only needs to be created
67+
// once, and can be reused for multiple requests. After completing all of your requests, call
68+
// the "close" method on the client to safely clean up any remaining background resources.
69+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
70+
Table table = connection.getTable(TableName.valueOf(tableId));
71+
72+
byte[] rowkey = Bytes.toBytes("phone#4c410523#20190501");
73+
74+
Result row = table.get(new Get(rowkey));
75+
printRow(row);
76+
77+
} catch (IOException e) {
78+
System.out.println(
79+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
80+
}
81+
}
82+
// [START bigtable_reads_row_partial]
83+
public static void readRowPartial() {
84+
// TODO(developer): Replace these variables before running the sample.
85+
String projectId = "my-project-id";
86+
String instanceId = "my-instance-id";
87+
String tableId = "mobile-time-series";
88+
readRowPartial(projectId, instanceId, tableId);
89+
}
90+
91+
public static void readRowPartial(String projectId, String instanceId, String tableId) {
92+
// Initialize client that will be used to send requests. This client only needs to be created
93+
// once, and can be reused for multiple requests. After completing all of your requests, call
94+
// the "close" method on the client to safely clean up any remaining background resources.
95+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
96+
Table table = connection.getTable(TableName.valueOf(tableId));
97+
byte[] rowkey = Bytes.toBytes("phone#4c410523#20190501");
98+
99+
Result row =
100+
table.get(
101+
new Get(rowkey).addColumn(Bytes.toBytes("stats_summary"), Bytes.toBytes("os_build")));
102+
printRow(row);
103+
104+
} catch (IOException e) {
105+
System.out.println(
106+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
107+
}
108+
}
109+
// [END bigtable_reads_row_partial]
110+
111+
// [START bigtable_reads_rows]
112+
public static void readRows() {
113+
// TODO(developer): Replace these variables before running the sample.
114+
String projectId = "my-project-id";
115+
String instanceId = "my-instance-id";
116+
String tableId = "mobile-time-series";
117+
readRows(projectId, instanceId, tableId);
118+
}
119+
120+
public static void readRows(String projectId, String instanceId, String tableId) {
121+
// Initialize client that will be used to send requests. This client only needs to be created
122+
// once, and can be reused for multiple requests. After completing all of your requests, call
123+
// the "close" method on the client to safely clean up any remaining background resources.
124+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
125+
Table table = connection.getTable(TableName.valueOf(tableId));
126+
List<Get> queryRowList = new ArrayList<Get>();
127+
queryRowList.add(new Get(Bytes.toBytes("phone#4c410523#20190501")));
128+
queryRowList.add(new Get(Bytes.toBytes("phone#4c410523#20190502")));
129+
130+
Result[] rows = table.get(queryRowList);
131+
132+
for (Result row : rows) {
133+
printRow(row);
134+
}
135+
} catch (IOException e) {
136+
System.out.println(
137+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
138+
}
139+
}
140+
// [END bigtable_reads_rows]
141+
142+
// [START bigtable_reads_row_range]
143+
public static void readRowRange() {
144+
// TODO(developer): Replace these variables before running the sample.
145+
String projectId = "my-project-id";
146+
String instanceId = "my-instance-id";
147+
String tableId = "mobile-time-series";
148+
readRowRange(projectId, instanceId, tableId);
149+
}
150+
151+
public static void readRowRange(String projectId, String instanceId, String tableId) {
152+
// Initialize client that will be used to send requests. This client only needs to be created
153+
// once, and can be reused for multiple requests. After completing all of your requests, call
154+
// the "close" method on the client to safely clean up any remaining background resources.
155+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
156+
Table table = connection.getTable(TableName.valueOf(tableId));
157+
158+
Scan rangeQuery =
159+
new Scan()
160+
.withStartRow(Bytes.toBytes("phone#4c410523#20190501"))
161+
.withStopRow(Bytes.toBytes("phone#4c410523#201906201"));
162+
163+
ResultScanner rows = table.getScanner(rangeQuery);
164+
165+
for (Result row : rows) {
166+
printRow(row);
167+
}
168+
169+
} catch (IOException e) {
170+
System.out.println(
171+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
172+
}
173+
}
174+
// [END bigtable_reads_row_range]
175+
176+
// [START bigtable_reads_row_ranges]
177+
public static void readRowRanges() {
178+
// TODO(developer): Replace these variables before running the sample.
179+
String projectId = "my-project-id";
180+
String instanceId = "my-instance-id";
181+
String tableId = "mobile-time-series";
182+
readRowRanges(projectId, instanceId, tableId);
183+
}
184+
185+
public static void readRowRanges(String projectId, String instanceId, String tableId) {
186+
// Initialize client that will be used to send requests. This client only needs to be created
187+
// once, and can be reused for multiple requests. After completing all of your requests, call
188+
// the "close" method on the client to safely clean up any remaining background resources.
189+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
190+
Table table = connection.getTable(TableName.valueOf(tableId));
191+
List<RowRange> ranges = new ArrayList<>();
192+
193+
ranges.add(
194+
new RowRange(
195+
Bytes.toBytes("phone#4c410523#20190501"),
196+
true,
197+
Bytes.toBytes("phone#4c410523#20190601"),
198+
false));
199+
ranges.add(
200+
new RowRange(
201+
Bytes.toBytes("phone#5c10102#20190501"),
202+
true,
203+
Bytes.toBytes("phone#5c10102#20190601"),
204+
false));
205+
Filter filter = new MultiRowRangeFilter(ranges);
206+
Scan scan = new Scan().setFilter(filter);
207+
208+
ResultScanner rows = table.getScanner(scan);
209+
210+
for (Result row : rows) {
211+
printRow(row);
212+
}
213+
} catch (IOException e) {
214+
System.out.println(
215+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
216+
}
217+
}
218+
// [END bigtable_reads_row_ranges]
219+
220+
// [START bigtable_reads_prefix]
221+
public static void readPrefix() {
222+
// TODO(developer): Replace these variables before running the sample.
223+
String projectId = "my-project-id";
224+
String instanceId = "my-instance-id";
225+
String tableId = "mobile-time-series";
226+
readPrefix(projectId, instanceId, tableId);
227+
}
228+
229+
public static void readPrefix(String projectId, String instanceId, String tableId) {
230+
// Initialize client that will be used to send requests. This client only needs to be created
231+
// once, and can be reused for multiple requests. After completing all of your requests, call
232+
// the "close" method on the client to safely clean up any remaining background resources.
233+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
234+
Table table = connection.getTable(TableName.valueOf(tableId));
235+
Scan prefixScan = new Scan().setRowPrefixFilter(Bytes.toBytes("phone"));
236+
ResultScanner rows = table.getScanner(prefixScan);
237+
238+
for (Result row : rows) {
239+
printRow(row);
240+
}
241+
} catch (IOException e) {
242+
System.out.println(
243+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
244+
}
245+
}
246+
// [END bigtable_reads_prefix]
247+
248+
// [START bigtable_reads_filter]
249+
public static void readFilter() {
250+
// TODO(developer): Replace these variables before running the sample.
251+
String projectId = "my-project-id";
252+
String instanceId = "my-instance-id";
253+
String tableId = "mobile-time-series";
254+
readFilter(projectId, instanceId, tableId);
255+
}
256+
257+
public static void readFilter(String projectId, String instanceId, String tableId) {
258+
// Initialize client that will be used to send requests. This client only needs to be created
259+
// once, and can be reused for multiple requests. After completing all of your requests, call
260+
// the "close" method on the client to safely clean up any remaining background resources.
261+
try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
262+
Table table = connection.getTable(TableName.valueOf(tableId));
263+
264+
ValueFilter valueFilter =
265+
new ValueFilter(CompareOp.EQUAL, new RegexStringComparator("PQ2A.*"));
266+
Scan scan = new Scan().setFilter(valueFilter);
267+
268+
ResultScanner rows = table.getScanner(scan);
269+
270+
for (Result row : rows) {
271+
printRow(row);
272+
}
273+
} catch (IOException e) {
274+
System.out.println(
275+
"Unable to initialize service client, as a network error occurred: \n" + e.toString());
276+
}
277+
}
278+
// [END bigtable_reads_filter]
279+
280+
// [START bigtable_reads_row]
281+
// [START bigtable_reads_row_partial]
282+
// [START bigtable_reads_rows]
283+
// [START bigtable_reads_row_range]
284+
// [START bigtable_reads_row_ranges]
285+
// [START bigtable_reads_prefix]
286+
// [START bigtable_reads_filter]
287+
private static void printRow(Result row) {
288+
System.out.printf("Reading data for %s%n", Bytes.toString(row.rawCells()[0].getRowArray()));
289+
String colFamily = "";
290+
for (Cell cell : row.rawCells()) {
291+
String currentFamily = Bytes.toString(cell.getFamilyArray());
292+
if (!currentFamily.equals(colFamily)) {
293+
colFamily = currentFamily;
294+
System.out.printf("Column Family %s%n", colFamily);
295+
}
296+
System.out.printf(
297+
"\t%s: %s @%s%n",
298+
Bytes.toString(cell.getQualifierArray()),
299+
Bytes.toString(cell.getValueArray()),
300+
cell.getTimestamp());
301+
}
302+
System.out.println();
303+
}
304+
}
305+
// [END bigtable_reads_row]
306+
// [END bigtable_reads_row_partial]
307+
// [END bigtable_reads_rows]
308+
// [END bigtable_reads_row_range]
309+
// [END bigtable_reads_row_ranges]
310+
// [END bigtable_reads_prefix]
311+
// [END bigtable_reads_filter]

0 commit comments

Comments
 (0)