Java Code Examples for com.google.cloud.bigtable.hbase.BigtableConfiguration#connect()
The following examples show how to use
com.google.cloud.bigtable.hbase.BigtableConfiguration#connect() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigtableTarget.java From datacollector with Apache License 2.0 | 6 votes |
private Connection connectToBigtable(List<ConfigIssue> issues) { Connection conn; try { conn = BigtableConfiguration.connect(conf.bigtableProjectID, conf.bigtableInstanceID); } catch (Exception ex) { LOG.error(Errors.BIGTABLE_01.getMessage(), conf.bigtableProjectID, ex.toString(), ex); issues.add(getContext().createConfigIssue(Groups.BIGTABLE.name(), CONFIG, Errors.BIGTABLE_01, conf.bigtableProjectID, ex.toString() )); return null; } return conn; }
Example 2
Source File: Reads.java From java-docs-samples with Apache License 2.0 | 6 votes |
public static void readRow(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Table table = connection.getTable(TableName.valueOf(tableId)); byte[] rowkey = Bytes.toBytes("phone#4c410523#20190501"); Result row = table.get(new Get(rowkey)); printRow(row); } catch (IOException e) { System.out.println( "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } }
Example 3
Source File: Reads.java From java-docs-samples with Apache License 2.0 | 6 votes |
public static void readRowPartial(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Table table = connection.getTable(TableName.valueOf(tableId)); byte[] rowkey = Bytes.toBytes("phone#4c410523#20190501"); Result row = table.get( new Get(rowkey).addColumn(Bytes.toBytes("stats_summary"), Bytes.toBytes("os_build"))); printRow(row); } catch (IOException e) { System.out.println( "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } }
Example 4
Source File: HelloWorldTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@BeforeClass public static void beforeClass() { projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); instanceId = requireEnv(INSTANCE_ENV); try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Admin admin = connection.getAdmin(); HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_ID)); descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME)); admin.createTable(descriptor); Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(TABLE_ID))); String rowKey = "phone#4c410523#20190401"; Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn( Bytes.toBytes(COLUMN_FAMILY_NAME), Bytes.toBytes("os_name"), Bytes.toBytes("android")); table.put(put); } catch (Exception e) { System.out.println("Error during beforeClass: \n" + e.toString()); } }
Example 5
Source File: Reads.java From java-docs-samples with Apache License 2.0 | 6 votes |
public static void readPrefix(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Table table = connection.getTable(TableName.valueOf(tableId)); Scan prefixScan = new Scan().setRowPrefixFilter(Bytes.toBytes("phone")); ResultScanner rows = table.getScanner(prefixScan); for (Result row : rows) { printRow(row); } } catch (IOException e) { System.out.println( "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } }
Example 6
Source File: HelloWorldTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void testWrite() { HelloWorldWrite.main( new String[] { "--bigtableProjectId=" + projectId, "--bigtableInstanceId=" + instanceId, "--bigtableTableId=" + TABLE_ID }); long count = 0; try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Table table = connection.getTable(TableName.valueOf(TABLE_ID)); Scan scan = new Scan(); ResultScanner rows = table.getScanner(scan); for (Result row : rows) { count++; } } catch (IOException e) { System.out.println( "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } assertThat(count).isGreaterThan(0); }
Example 7
Source File: WriteSimple.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void writeSimple(String projectId, String instanceId, String tableId) { // String projectId = "my-project-id"; // String instanceId = "my-instance-id"; // String tableId = "mobile-time-series"; try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { final Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId))); long timestamp = System.currentTimeMillis(); byte[] one = new byte[]{0, 0, 0, 0, 0, 0, 0, 1}; String rowKey = "phone#4c410523#20190501"; Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), timestamp, one); put.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, one); put.addColumn( COLUMN_FAMILY_NAME, Bytes.toBytes("os_build"), timestamp, Bytes.toBytes("PQ2A.190405.003")); table.put(put); System.out.printf("Successfully wrote row %s", rowKey); } catch (Exception e) { System.out.println("Error during WriteSimple: \n" + e.toString()); } }
Example 8
Source File: KeyVizArtTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@AfterClass public static void afterClass() { try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Admin admin = connection.getAdmin(); Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(TABLE_ID))); admin.disableTable(table.getName()); admin.deleteTable(table.getName()); } catch (Exception e) { System.out.println("Error during afterClass: \n" + e.toString()); } }
Example 9
Source File: KeyVizArtTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@BeforeClass public static void beforeClass() { projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); instanceId = requireEnv(INSTANCE_ENV); try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Admin admin = connection.getAdmin(); HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_ID)); descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME)); admin.createTable(descriptor); } catch (Exception e) { System.out.println("Error during beforeClass: \n" + e.toString()); } }
Example 10
Source File: WritesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@BeforeClass public static void beforeClass() { projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); instanceId = requireEnv(INSTANCE_ENV); try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Admin admin = connection.getAdmin(); HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_ID)); descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME)); admin.createTable(descriptor); } catch (Exception e) { System.out.println("Error during beforeClass: \n" + e.toString()); } }
Example 11
Source File: BigtableHelper.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Connect will establish the connection to Cloud Bigtable. */ public static void connect() throws IOException { if (PROJECT_ID == null || INSTANCE_ID == null) { if (sc != null) { sc.log("environment variables BIGTABLE_PROJECT, and BIGTABLE_INSTANCE need to be defined."); } return; } connection = BigtableConfiguration.connect(PROJECT_ID, INSTANCE_ID); }
Example 12
Source File: Connections.java From styx with Apache License 2.0 | 5 votes |
public static Connection createBigTableConnection(Config config) { final String projectId = config.getString(BIGTABLE_PROJECT_ID); final String instanceId = config.getString(BIGTABLE_INSTANCE_ID); LOG.info("Creating Bigtable connection for project:{}, instance:{}", projectId, instanceId); final Configuration bigtableConfiguration = new Configuration(); bigtableConfiguration.set("google.bigtable.project.id", projectId); bigtableConfiguration.set("google.bigtable.instance.id", instanceId); bigtableConfiguration.setBoolean("google.bigtable.rpc.use.timeouts", true); return BigtableConfiguration.connect(bigtableConfiguration); }
Example 13
Source File: BigtableTargetIT.java From datacollector with Apache License 2.0 | 5 votes |
private static void dropTable() { Connection conn = BigtableConfiguration.connect(projectID, instanceID); try { Admin admin = conn.getAdmin(); admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); } catch (Exception ex) { LOG.info("dropTable(): exception {} ", ex.toString()); } }
Example 14
Source File: Reads.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void readFilter(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Table table = connection.getTable(TableName.valueOf(tableId)); ValueFilter valueFilter = new ValueFilter(CompareOp.EQUAL, new RegexStringComparator("PQ2A.*")); Scan scan = new Scan().setFilter(valueFilter); ResultScanner rows = table.getScanner(scan); for (Result row : rows) { printRow(row); } } catch (IOException e) { System.out.println( "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } }
Example 15
Source File: Reads.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void readRowRanges(String projectId, String instanceId, String tableId) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Table table = connection.getTable(TableName.valueOf(tableId)); List<RowRange> ranges = new ArrayList<>(); ranges.add( new RowRange( Bytes.toBytes("phone#4c410523#20190501"), true, Bytes.toBytes("phone#4c410523#20190601"), false)); ranges.add( new RowRange( Bytes.toBytes("phone#5c10102#20190501"), true, Bytes.toBytes("phone#5c10102#20190601"), false)); Filter filter = new MultiRowRangeFilter(ranges); Scan scan = new Scan().setFilter(filter); ResultScanner rows = table.getScanner(scan); for (Result row : rows) { printRow(row); } } catch (IOException e) { System.out.println( "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } }
Example 16
Source File: BigtableTargetIT.java From datacollector with Apache License 2.0 | 4 votes |
private void createColumnFamily(String tab, String cf) throws Exception { Connection conn = BigtableConfiguration.connect(projectID, instanceID); Admin admin = conn.getAdmin(); admin.addColumn(TableName.valueOf(tab), new HColumnDescriptor(cf)); }
Example 17
Source File: BigtableConnect.java From cloud-bigtable-examples with Apache License 2.0 | 4 votes |
public static void connectWithAppProfile() throws IOException { connection = BigtableConfiguration.connect(projectId, instanceId, appProfileId); }
Example 18
Source File: KeyVizArtTest.java From java-docs-samples with Apache License 2.0 | 4 votes |
@Test public void testWriteAndRead() { LoadData.main( new String[]{ "--bigtableProjectId=" + projectId, "--bigtableInstanceId=" + instanceId, "--bigtableTableId=" + TABLE_ID, "--gigabytesWritten=" + GIGABYTES_WRITTEN, "--megabytesPerRow=" + MEGABYTES_PER_ROW }); long count = 0; try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { Table table = connection.getTable(TableName.valueOf(TABLE_ID)); Scan scan = new Scan(); ResultScanner rows = table.getScanner(scan); for (Result row : rows) { count++; } } catch (IOException e) { System.out.println( "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } assertEquals(10, count); ReadDataOptions options = PipelineOptionsFactory.fromArgs("--bigtableProjectId=" + projectId, "--bigtableInstanceId=" + instanceId, "--bigtableTableId=" + TABLE_ID, "--gigabytesWritten=" + GIGABYTES_WRITTEN, "--megabytesPerRow=" + MEGABYTES_PER_ROW, "--filePath=gs://keyviz-art/maxgrid.txt").withValidation().as(ReadDataOptions.class); Pipeline p = Pipeline.create(options); CloudBigtableTableConfiguration bigtableTableConfig = new CloudBigtableTableConfiguration.Builder() .withProjectId(options.getBigtableProjectId()) .withInstanceId(options.getBigtableInstanceId()) .withTableId(options.getBigtableTableId()) .build(); // Initiates a new pipeline every second p.apply(Create.of(1L)) .apply(ParDo.of(new ReadFromTableFn(bigtableTableConfig, options))); p.run().waitUntilFinish(); String output = bout.toString(); assertThat(output).contains("got 10 rows"); options = PipelineOptionsFactory.fromArgs("--bigtableProjectId=" + projectId, "--bigtableInstanceId=" + instanceId, "--bigtableTableId=" + TABLE_ID, "--gigabytesWritten=" + GIGABYTES_WRITTEN, "--megabytesPerRow=" + MEGABYTES_PER_ROW, "--filePath=gs://keyviz-art/halfgrid.txt").withValidation().as(ReadDataOptions.class); p = Pipeline.create(options); // Initiates a new pipeline every second p.apply(Create.of(1L)) .apply(ParDo.of(new ReadFromTableFn(bigtableTableConfig, options))); p.run().waitUntilFinish(); output = bout.toString(); assertThat(output).contains("got 5 rows"); }
Example 19
Source File: BigtableConnect.java From cloud-bigtable-examples with Apache License 2.0 | 4 votes |
public static void connect() throws IOException { connection = BigtableConfiguration.connect(projectId, instanceId); }
Example 20
Source File: QuickstartIT.java From cloud-bigtable-examples with Apache License 2.0 | 4 votes |
@After public void cleanup() throws Exception { try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { connection.getAdmin().deleteTable(TableName.valueOf(tableId)); } }