|
| 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 static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; |
| 28 | + |
| 29 | +import com.google.api.gax.rpc.ServerStream; |
| 30 | +import com.google.cloud.bigtable.data.v2.BigtableDataClient; |
| 31 | +import com.google.cloud.bigtable.data.v2.models.Filters; |
| 32 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 33 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 34 | +import com.google.cloud.bigtable.data.v2.models.RowCell; |
| 35 | +import com.google.cloud.bigtable.data.v2.models.RowMutation; |
| 36 | +import com.google.protobuf.ByteString; |
| 37 | +import java.io.IOException; |
| 38 | + |
| 39 | +public class Reads { |
| 40 | + // [END bigtable_reads_row] |
| 41 | + // [END bigtable_reads_row_partial] |
| 42 | + // [END bigtable_reads_rows] |
| 43 | + // [END bigtable_reads_row_range] |
| 44 | + // [END bigtable_reads_row_ranges] |
| 45 | + // [END bigtable_reads_prefix] |
| 46 | + // [END bigtable_reads_filter] |
| 47 | + |
| 48 | + // [START bigtable_reads_row] |
| 49 | + public static void readRow() { |
| 50 | + // TODO(developer): Replace these variables before running the sample. |
| 51 | + String projectId = "my-project-id"; |
| 52 | + String instanceId = "my-instance-id"; |
| 53 | + String tableId = "mobile-time-series"; |
| 54 | + readRow(projectId, instanceId, tableId); |
| 55 | + } |
| 56 | + |
| 57 | + public static void readRow(String projectId, String instanceId, String tableId) { |
| 58 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 59 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 60 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 61 | + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { |
| 62 | + String rowkey = "phone#4c410523#20190501"; |
| 63 | + |
| 64 | + Row row = dataClient.readRow(tableId, rowkey); |
| 65 | + printRow(row); |
| 66 | + |
| 67 | + } catch (IOException e) { |
| 68 | + System.out.println( |
| 69 | + "Unable to intailize service client, as a network error occured: \n" + e.toString()); |
| 70 | + } |
| 71 | + } |
| 72 | + // [END bigtable_reads_row] |
| 73 | + |
| 74 | + // [START bigtable_reads_row_partial] |
| 75 | + public static void readRowPartial() { |
| 76 | + // TODO(developer): Replace these variables before running the sample. |
| 77 | + String projectId = "my-project-id"; |
| 78 | + String instanceId = "my-instance-id"; |
| 79 | + String tableId = "mobile-time-series"; |
| 80 | + readRowPartial(projectId, instanceId, tableId); |
| 81 | + } |
| 82 | + |
| 83 | + public static void readRowPartial(String projectId, String instanceId, String tableId) { |
| 84 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 85 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 86 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 87 | + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { |
| 88 | + String rowkey = "phone#4c410523#20190501"; |
| 89 | + Filters.Filter filter = |
| 90 | + FILTERS |
| 91 | + .chain() |
| 92 | + .filter(FILTERS.family().exactMatch("stats_summary")) |
| 93 | + .filter(FILTERS.qualifier().exactMatch("os_build")); |
| 94 | + |
| 95 | + Row row = dataClient.readRow(tableId, rowkey, filter); |
| 96 | + printRow(row); |
| 97 | + |
| 98 | + } catch (IOException e) { |
| 99 | + System.out.println( |
| 100 | + "Unable to intailize service client, as a network error occured: \n" + e.toString()); |
| 101 | + } |
| 102 | + } |
| 103 | + // [END bigtable_reads_row_partial] |
| 104 | + |
| 105 | + // [START bigtable_reads_rows] |
| 106 | + public static void readRows() { |
| 107 | + // TODO(developer): Replace these variables before running the sample. |
| 108 | + String projectId = "my-project-id"; |
| 109 | + String instanceId = "my-instance-id"; |
| 110 | + String tableId = "mobile-time-series"; |
| 111 | + readRows(projectId, instanceId, tableId); |
| 112 | + } |
| 113 | + |
| 114 | + public static void readRows(String projectId, String instanceId, String tableId) { |
| 115 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 116 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 117 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 118 | + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { |
| 119 | + Query query = |
| 120 | + Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502"); |
| 121 | + ServerStream<Row> rows = dataClient.readRows(query); |
| 122 | + for (Row row : rows) { |
| 123 | + printRow(row); |
| 124 | + } |
| 125 | + } catch (IOException e) { |
| 126 | + System.out.println( |
| 127 | + "Unable to intailize service client, as a network error occured: \n" + e.toString()); |
| 128 | + } |
| 129 | + } |
| 130 | + // [END bigtable_reads_rows] |
| 131 | + |
| 132 | + // [START bigtable_reads_row_range] |
| 133 | + public static void readRowRange() { |
| 134 | + // TODO(developer): Replace these variables before running the sample. |
| 135 | + String projectId = "my-project-id"; |
| 136 | + String instanceId = "my-instance-id"; |
| 137 | + String tableId = "mobile-time-series"; |
| 138 | + readRowRange(projectId, instanceId, tableId); |
| 139 | + } |
| 140 | + |
| 141 | + public static void readRowRange(String projectId, String instanceId, String tableId) { |
| 142 | + String start = "phone#4c410523#20190501"; |
| 143 | + String end = "phone#4c410523#201906201"; |
| 144 | + |
| 145 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 146 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 147 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 148 | + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { |
| 149 | + Query query = Query.create(tableId).range(start, end); |
| 150 | + ServerStream<Row> rows = dataClient.readRows(query); |
| 151 | + for (Row row : rows) { |
| 152 | + printRow(row); |
| 153 | + } |
| 154 | + } catch (IOException e) { |
| 155 | + System.out.println( |
| 156 | + "Unable to intailize service client, as a network error occured: \n" + e.toString()); |
| 157 | + } |
| 158 | + } |
| 159 | + // [END bigtable_reads_row_range] |
| 160 | + |
| 161 | + // [START bigtable_reads_row_ranges] |
| 162 | + public static void readRowRanges() { |
| 163 | + // TODO(developer): Replace these variables before running the sample. |
| 164 | + String projectId = "my-project-id"; |
| 165 | + String instanceId = "my-instance-id"; |
| 166 | + String tableId = "mobile-time-series"; |
| 167 | + readRowRanges(projectId, instanceId, tableId); |
| 168 | + } |
| 169 | + |
| 170 | + public static void readRowRanges(String projectId, String instanceId, String tableId) { |
| 171 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 172 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 173 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 174 | + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { |
| 175 | + Query query = |
| 176 | + Query.create(tableId) |
| 177 | + .range("phone#4c410523#20190501", "phone#4c410523#20190601") |
| 178 | + .range("phone#5c10102#20190501", "phone#5c10102#20190601"); |
| 179 | + ServerStream<Row> rows = dataClient.readRows(query); |
| 180 | + for (Row row : rows) { |
| 181 | + printRow(row); |
| 182 | + } |
| 183 | + } catch (IOException e) { |
| 184 | + System.out.println( |
| 185 | + "Unable to intailize service client, as a network error occured: \n" + e.toString()); |
| 186 | + } |
| 187 | + } |
| 188 | + // [END bigtable_reads_row_ranges] |
| 189 | + |
| 190 | + // [START bigtable_reads_prefix] |
| 191 | + public static void readPrefix() { |
| 192 | + // TODO(developer): Replace these variables before running the sample. |
| 193 | + String projectId = "my-project-id"; |
| 194 | + String instanceId = "my-instance-id"; |
| 195 | + String tableId = "mobile-time-series"; |
| 196 | + readPrefix(projectId, instanceId, tableId); |
| 197 | + } |
| 198 | + |
| 199 | + public static void readPrefix(String projectId, String instanceId, String tableId) { |
| 200 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 201 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 202 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 203 | + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { |
| 204 | + Query query = Query.create(tableId).prefix("phone"); |
| 205 | + ServerStream<Row> rows = dataClient.readRows(query); |
| 206 | + for (Row row : rows) { |
| 207 | + printRow(row); |
| 208 | + } |
| 209 | + } catch (IOException e) { |
| 210 | + System.out.println( |
| 211 | + "Unable to intailize service client, as a network error occured: \n" + e.toString()); |
| 212 | + } |
| 213 | + } |
| 214 | + // [END bigtable_reads_prefix] |
| 215 | + |
| 216 | + // [START bigtable_reads_filter] |
| 217 | + public static void readFilter() { |
| 218 | + // TODO(developer): Replace these variables before running the sample. |
| 219 | + String projectId = "my-project-id"; |
| 220 | + String instanceId = "my-instance-id"; |
| 221 | + String tableId = "mobile-time-series"; |
| 222 | + readFilter(projectId, instanceId, tableId); |
| 223 | + } |
| 224 | + |
| 225 | + public static void readFilter(String projectId, String instanceId, String tableId) { |
| 226 | + Filters.Filter filter = FILTERS.value().regex("PQ2A.*"); |
| 227 | + |
| 228 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 229 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 230 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 231 | + try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { |
| 232 | + Query query = Query.create(tableId).filter(filter); |
| 233 | + ServerStream<Row> rows = dataClient.readRows(query); |
| 234 | + for (Row row : rows) { |
| 235 | + printRow(row); |
| 236 | + } |
| 237 | + } catch (IOException e) { |
| 238 | + System.out.println( |
| 239 | + "Unable to intailize service client, as a network error occured: \n" + e.toString()); |
| 240 | + } |
| 241 | + } |
| 242 | + // [END bigtable_reads_filter] |
| 243 | + |
| 244 | + // [START bigtable_reads_row] |
| 245 | + // [START bigtable_reads_row_partial] |
| 246 | + // [START bigtable_reads_rows] |
| 247 | + // [START bigtable_reads_row_range] |
| 248 | + // [START bigtable_reads_row_ranges] |
| 249 | + // [START bigtable_reads_prefix] |
| 250 | + // [START bigtable_reads_filter] |
| 251 | + private static void printRow(Row row) { |
| 252 | + System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8()); |
| 253 | + String colFamily = ""; |
| 254 | + for (RowCell cell : row.getCells()) { |
| 255 | + if (!cell.getFamily().equals(colFamily)) { |
| 256 | + colFamily = cell.getFamily(); |
| 257 | + System.out.printf("Column Family %s%n", colFamily); |
| 258 | + } |
| 259 | + System.out.printf( |
| 260 | + "\t%s: %s @%s%n", |
| 261 | + cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp()); |
| 262 | + } |
| 263 | + System.out.println(); |
| 264 | + } |
| 265 | +} |
| 266 | +// [END bigtable_reads_row] |
| 267 | +// [END bigtable_reads_row_partial] |
| 268 | +// [END bigtable_reads_rows] |
| 269 | +// [END bigtable_reads_row_range] |
| 270 | +// [END bigtable_reads_row_ranges] |
| 271 | +// [END bigtable_reads_prefix] |
| 272 | +// [END bigtable_reads_filter] |
0 commit comments