|
| 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_writes_batch_hbase] |
| 20 | + |
| 21 | +import com.google.cloud.bigtable.hbase.BigtableConfiguration; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import org.apache.hadoop.hbase.TableName; |
| 25 | +import org.apache.hadoop.hbase.client.Connection; |
| 26 | +import org.apache.hadoop.hbase.client.Put; |
| 27 | +import org.apache.hadoop.hbase.client.Table; |
| 28 | +import org.apache.hadoop.hbase.util.Bytes; |
| 29 | + |
| 30 | +public class WriteBatch { |
| 31 | + private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary"); |
| 32 | + |
| 33 | + public static void writeBatch(String projectId, String instanceId, String tableId) { |
| 34 | + // String projectId = "my-project-id"; |
| 35 | + // String instanceId = "my-instance-id"; |
| 36 | + // String tableId = "mobile-time-series"; |
| 37 | + |
| 38 | + try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { |
| 39 | + Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId))); |
| 40 | + long timestamp = System.currentTimeMillis(); |
| 41 | + |
| 42 | + List<Put> puts = new ArrayList<Put>(); |
| 43 | + puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190501"))); |
| 44 | + puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190502"))); |
| 45 | + |
| 46 | + puts.get(0) |
| 47 | + .addColumn( |
| 48 | + COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1)); |
| 49 | + puts.get(0) |
| 50 | + .addColumn( |
| 51 | + COLUMN_FAMILY_NAME, |
| 52 | + Bytes.toBytes("os_build"), |
| 53 | + timestamp, |
| 54 | + Bytes.toBytes("12155.0.0-rc1")); |
| 55 | + |
| 56 | + puts.get(1) |
| 57 | + .addColumn( |
| 58 | + COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, Bytes.toBytes(1)); |
| 59 | + puts.get(1) |
| 60 | + .addColumn( |
| 61 | + COLUMN_FAMILY_NAME, |
| 62 | + Bytes.toBytes("os_build"), |
| 63 | + timestamp, |
| 64 | + Bytes.toBytes("12145.0.0-rc6")); |
| 65 | + |
| 66 | + table.put(puts); |
| 67 | + |
| 68 | + System.out.print("Successfully wrote 2 rows"); |
| 69 | + } catch (Exception e) { |
| 70 | + System.out.println("Error during WriteBatch: \n" + e.toString()); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// [END bigtable_writes_batch_hbase] |
0 commit comments