This topic describes how to use a Java client to access an ApsaraDB for HBase Standard Edition cluster.
Prerequisites
-
If connecting over an internal network, you must use an ECS instance in the same VPC as the cluster and add the instance's IP address to the cluster's whitelist.
-
If you do not have an ECS instance, see Create an ECS instance.
-
If you have not configured a whitelist, see Configure whitelists and security groups.
-
-
To connect to the cluster over the Internet, you must use the client provided by Alibaba Cloud. To download the client, see ApsaraDB for HBase SDK for Java.
If you have not configured a whitelist, see Configure whitelists and security groups.
Background information
-
You can access an ApsaraDB for HBase Standard Edition cluster using an open source HBase community client or the custom client provided by Alibaba Cloud.
-
If your runtime environment lacks the JAR packages required for the HBase client, add the following Maven configuration to bundle all client dependencies into the final JAR package.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <artifactSet> </artifactSet> </configuration> </execution> </executions> </plugin> </plugins> </build>
Procedure
-
Obtain the ZooKeeper endpoints of your cluster.
-
Log on to the ApsaraDB for HBase console.
-
On the Clusters page, click the name of your Standard Edition instance to open the cluster details page.
-
On the cluster details page, click Database Connection to open the Database Connection page.
On the Database Connection page, locate the ZooKeeper Endpoints (VPC) row. This row lists the addresses of the three master nodes, formatted as
hb-<InstanceID>-master<N>-001.hbase.rds.aliyuncs.com:2181(where N is 1, 2, or 3). Copy these addresses to configure your connection.
-
-
Configure the ZooKeeper endpoints to connect to the cluster.
In your application code, use the following configuration to connect to the cluster.
NoteReplace the placeholder ZooKeeper endpoints in the sample code with your cluster's actual endpoints. The example demonstrates creating a table, writing data, and reading data.
private static final String TABLE_NAME = "mytable"; private static final String CF_DEFAULT = "cf"; public static final byte[] QUALIFIER = "col1".getBytes(); private static final byte[] ROWKEY = "rowkey1".getBytes(); public static void main(String[] args) { Configuration config = HBaseConfiguration.create(); // The ZooKeeper endpoints for the cluster. String zkAddress = "hb-bp1f5xxxx48a0r17i-001.hbase.rds.aliyuncs.com:2181,hb-bp1f5xxxx48a0r17i-002.hbase.rds.aliyuncs.com:2181,hb-bp1f5xxxx48a0r17i-003.hbase.rds.aliyuncs.com:2181"; config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress); Connection connection = null; try { connection = ConnectionFactory.createConnection(config); HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME)); tableDescriptor.addFamily(new HColumnDescriptor(CF_DEFAULT)); System.out.print("Creating table. "); Admin admin = connection.getAdmin(); admin.createTable(tableDescriptor); System.out.println(" Done."); Table table = connection.getTable(TableName.valueOf(TABLE_NAME)); try { Put put = new Put(ROWKEY); put.addColumn(CF_DEFAULT.getBytes(), QUALIFIER, "this is value".getBytes()); table.put(put); Get get = new Get(ROWKEY); Result r = table.get(get); byte[] b = r.getValue(CF_DEFAULT.getBytes(), QUALIFIER); // returns current version of value System.out.println(new String(b)); } finally { if (table != null) table.close(); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Next steps
You can also download a sample Java project. After you download the project, replace the placeholder value for the ZooKeeper variable with your cluster's endpoints.