|
| 1 | +/* |
| 2 | + * Copyright 2019 Google Inc. |
| 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 | +import com.google.api.gax.rpc.ServerStream; |
| 18 | +import com.google.cloud.bigtable.data.v2.BigtableDataClient; |
| 19 | +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; |
| 20 | +import com.google.cloud.bigtable.data.v2.models.BulkMutation; |
| 21 | +import com.google.cloud.bigtable.data.v2.models.Mutation; |
| 22 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 23 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 24 | +import com.google.cloud.bigtable.data.v2.models.RowCell; |
| 25 | +import com.google.cloud.bigtable.data.v2.models.RowMutation; |
| 26 | +import com.google.protobuf.ByteString; |
| 27 | + |
| 28 | +public class CassandraMigrationCodelab { |
| 29 | + private BigtableDataClient dataClient; |
| 30 | + private String tableId; |
| 31 | + private final String COLUMN_FAMILY_NAME = "stats_summary"; |
| 32 | + |
| 33 | + public CassandraMigrationCodelab(String projectId, String instanceId, String tableId) { |
| 34 | + this.tableId = tableId; |
| 35 | + BigtableDataSettings settings = |
| 36 | + BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build(); |
| 37 | + |
| 38 | + try { |
| 39 | + dataClient = BigtableDataClient.create(settings); |
| 40 | + } catch (Exception e) { |
| 41 | + System.out.println("Error during data client connection: \n" + e.toString()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public void run() { |
| 46 | + write(); |
| 47 | + writeBatch(); |
| 48 | + update(); |
| 49 | + update2(); |
| 50 | + get(); |
| 51 | + scan(); |
| 52 | + delete(); |
| 53 | + deleteMultiple(); |
| 54 | + } |
| 55 | + |
| 56 | + public void write() { |
| 57 | + try { |
| 58 | + System.currentTimeMillis(); |
| 59 | + long timestamp = (long) 1556712000 * 1000; // Timestamp of June 1, 2019 12:00 |
| 60 | + |
| 61 | + String rowKey = "phone#4c410523#20190501"; |
| 62 | + ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1}); |
| 63 | + |
| 64 | + RowMutation rowMutation = |
| 65 | + RowMutation.create(tableId, rowKey) |
| 66 | + .setCell( |
| 67 | + COLUMN_FAMILY_NAME, |
| 68 | + ByteString.copyFrom("connected_cell".getBytes()), |
| 69 | + timestamp, |
| 70 | + one) |
| 71 | + .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003"); |
| 72 | + |
| 73 | + dataClient.mutateRow(rowMutation); |
| 74 | + } catch (Exception e) { |
| 75 | + System.out.println("Error during Write: \n" + e.toString()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void writeBatch() { |
| 80 | + try { |
| 81 | + long timestamp = (long) 1556712000 * 1000; // Timestamp of June 1, 2019 12:00 |
| 82 | + |
| 83 | + BulkMutation bulkMutation = |
| 84 | + BulkMutation.create(tableId) |
| 85 | + .add( |
| 86 | + "tablet#a0b81f74#20190501", |
| 87 | + Mutation.create() |
| 88 | + .setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "chromeos") |
| 89 | + .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")) |
| 90 | + .add( |
| 91 | + "tablet#a0b81f74#20190502", |
| 92 | + Mutation.create() |
| 93 | + .setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "chromeos") |
| 94 | + .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6")); |
| 95 | + |
| 96 | + dataClient.bulkMutateRows(bulkMutation); |
| 97 | + } catch (Exception e) { |
| 98 | + System.out.println("Error during WriteBatch: \n" + e.toString()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void update() { |
| 103 | + try { |
| 104 | + long timestamp = (long) 1556713800 * 1000; // Timestamp of June 1, 2019 12:30 |
| 105 | + |
| 106 | + String rowKey = "phone#4c410523#20190501"; |
| 107 | + |
| 108 | + RowMutation rowMutation = |
| 109 | + RowMutation.create(tableId, rowKey) |
| 110 | + .setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android"); |
| 111 | + |
| 112 | + dataClient.mutateRow(rowMutation); |
| 113 | + } catch (Exception e) { |
| 114 | + System.out.println("Error during update: \n" + e.toString()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public void update2() { |
| 119 | + |
| 120 | + try { |
| 121 | + long timestamp = (long) 1556713800 * 1000; // Timestamp of June 1, 2019 12:30 |
| 122 | + |
| 123 | + String rowKey = "phone#4c410523#20190501"; |
| 124 | + |
| 125 | + ByteString zero = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}); |
| 126 | + |
| 127 | + RowMutation rowMutation = |
| 128 | + RowMutation.create(tableId, rowKey) |
| 129 | + .setCell( |
| 130 | + COLUMN_FAMILY_NAME, |
| 131 | + ByteString.copyFrom("connected_cell".getBytes()), |
| 132 | + timestamp, |
| 133 | + zero); |
| 134 | + |
| 135 | + dataClient.mutateRow(rowMutation); |
| 136 | + } catch (Exception e) { |
| 137 | + System.out.println("Error during update2: \n" + e.toString()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public void get() { |
| 142 | + try { |
| 143 | + String rowKey = "phone#4c410523#20190501"; |
| 144 | + |
| 145 | + Row row = dataClient.readRow(tableId, rowKey); |
| 146 | + for (RowCell cell : row.getCells()) { |
| 147 | + |
| 148 | + System.out.printf( |
| 149 | + "Family: %s Qualifier: %s Value: %s Timestamp: %s%n", |
| 150 | + cell.getFamily(), |
| 151 | + cell.getQualifier().toStringUtf8(), |
| 152 | + cell.getValue().toStringUtf8(), |
| 153 | + cell.getTimestamp()); |
| 154 | + } |
| 155 | + } catch (Exception e) { |
| 156 | + System.out.println("Error during lookup: \n" + e.toString()); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public void scan() { |
| 161 | + try { |
| 162 | + Query query = Query.create(tableId).range("tablet#a0b81f74#201905", "tablet#a0b81f74#201906"); |
| 163 | + ServerStream<Row> rowStream = dataClient.readRows(query); |
| 164 | + for (Row row : rowStream) { |
| 165 | + System.out.println("Row Key: " + row.getKey().toStringUtf8()); |
| 166 | + for (RowCell cell : row.getCells()) { |
| 167 | + |
| 168 | + System.out.printf( |
| 169 | + "Family: %s Qualifier: %s Value: %s Timestamp: %s%n", |
| 170 | + cell.getFamily(), |
| 171 | + cell.getQualifier().toStringUtf8(), |
| 172 | + cell.getValue().toStringUtf8(), |
| 173 | + cell.getTimestamp()); |
| 174 | + } |
| 175 | + } |
| 176 | + } catch (Exception e) { |
| 177 | + System.out.println("Error during scan: \n" + e.toString()); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public void delete() { |
| 182 | + try { |
| 183 | + String rowKey = "phone#4c410523#20190501"; |
| 184 | + |
| 185 | + RowMutation mutation = RowMutation.create(tableId, rowKey).deleteRow(); |
| 186 | + |
| 187 | + dataClient.mutateRow(mutation); |
| 188 | + } catch (Exception e) { |
| 189 | + System.out.println("Error during Delete: \n" + e.toString()); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + public void deleteMultiple() { |
| 194 | + try { |
| 195 | + Query query = Query.create(tableId).prefix("tablet#a0b81f7"); |
| 196 | + ServerStream<Row> rowStream = dataClient.readRows(query); |
| 197 | + BulkMutation bulkMutation = BulkMutation.create(tableId); |
| 198 | + for (Row row : rowStream) { |
| 199 | + bulkMutation.add(row.getKey(), Mutation.create().deleteRow()); |
| 200 | + } |
| 201 | + |
| 202 | + dataClient.bulkMutateRows(bulkMutation); |
| 203 | + } catch (Exception e) { |
| 204 | + System.out.println("Error during DeleteMultiple: \n" + e.toString()); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments