You need to enable JavaScript to run this app.
导航

使用 Java API 连接实例

最近更新时间2023.11.01 11:23:00

首次发布时间2022.05.11 14:00:33

本文介绍如何使用 Java API 访问 HBase 实例。

前提条件

  • 如需通过私网地址访问 HBase 实例,需同时满足如下要求:
    • 已购 ECS 服务器与 HBase 实例在相同私有网络 VPC 下。ECS 服务器的购买方法,请参见购买云服务器
    • 已将 ECS 服务器的 IP 地址添加至 HBase 中的白名单中。白名单设置方法,请参见编辑白名单
  • 如需通过公网地址访问 HBase 实例,需确保运行 Java 工具的设备 IP 地址已加入 HBase 实例的白名单中。白名单设置方法,请参见编辑白名单
  • 已在 ECS 实例或本地设备上安装 Java 环境,建议使用 JDK 8 版本。更多详情,请参见 Java Downloads

操作步骤

  1. 获取 HBase 实例的 ZK 连接地址。具体操作步骤,请参见查看连接地址

  2. 配置 ZK 地址连接 HBase 实例。

    1. 在本地业务环境的 Maven 中添加如下配置:
      <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-client</artifactId>
          <version>2.2.5</version>
      </dependency>
      
    2. 在本地业务代码中增加如下配置文件来访问实例。

      说明

      您需要修改代码中 config.set 的如下配置:

      • 用步骤 1 中获取的 ZK 地址替换代码中的 zkEndpoint:Port
      • 用目标 HBase 实例 ID 替换代码中的 HBase 实例 ID。您可以在 HBase 控制台实例列表页找到并复制目标实例 ID。
      • 通过 ZK 私网连接地址访问实例

        import java.io.IOException;
        import org.apache.hadoop.conf.Configuration;
        import org.apache.hadoop.hbase.HBaseConfiguration;
        import org.apache.hadoop.hbase.HConstants;
        import org.apache.hadoop.hbase.KeyValue;
        import org.apache.hadoop.hbase.TableName;
        import org.apache.hadoop.hbase.client.Connection;
        import org.apache.hadoop.hbase.client.ConnectionFactory;
        import org.apache.hadoop.hbase.client.Get;
        import org.apache.hadoop.hbase.client.Table;
        import org.apache.hadoop.hbase.client.Put;
        import org.apache.hadoop.hbase.client.Result;
        import org.apache.hadoop.hbase.client.ResultScanner;
        import org.apache.hadoop.hbase.client.Scan;
        import org.apache.hadoop.hbase.util.Bytes;
        
        // Class that has nothing but a main.
        // Does a Put, Get and a Scan against an hbase table.
        // The API described here is since HBase 1.0.
        public class MyLittleHBaseClient {
            public static void main(String[] args) throws IOException {
                // You need a configuration object to tell the client where to connect.
                // When you create a HBaseConfiguration, it reads in whatever you've set
                // into your hbase-site.xml and in hbase-default.xml, as long as these can
                // be found on the CLASSPATH
                Configuration config = HBaseConfiguration.create();
        
                // Next you need a Connection to the cluster. Create one. When done with it,
                // close it. A try/finally is a good way to ensure it gets closed or use
                // the jdk7 idiom, try-with-resources: see
                // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
                //
                // Connections are heavyweight.  Create one once and keep it around. From a Connection
                // you get a Table instance to access Tables, an Admin instance to administer the cluster,
                // and RegionLocator to find where regions are out on the cluster. As opposed to Connections,
                // Table, Admin and RegionLocator instances are lightweight; create as you need them and then
                // close when done.
                //
                
                config.set(HConstants.ZOOKEEPER_QUORUM, <zkEndpoint:Port>);
                config.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/hbase/<HBase 实例 ID>");
                //HConstants.ZOOKEEPER_ZNO
                Connection connection = ConnectionFactory.createConnection(config);
        
                try {
        
                    // The below instantiates a Table object that connects you to the "myLittleHBaseTable" table
                    // (TableName.valueOf turns String into a TableName instance).
                    // When done with it, close it (Should start a try/finally after this creation so it gets
                    // closed for sure the jdk7 idiom, try-with-resources: see
                    // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)
                    Table table = connection.getTable(TableName.valueOf("myLittleHBaseTable"));
                    try {
        
                        // To add to a row, use Put.  A Put constructor takes the name of the row
                        // you want to insert into as a byte array.  In HBase, the Bytes class has
                        // utility for converting all kinds of java types to byte arrays.  In the
                        // below, we are converting the String "myLittleRow" into a byte array to
                        // use as a row key for our update. Once you have a Put instance, you can
                        // adorn it by setting the names of columns you want to update on the row,
                        // the timestamp to use in your update, etc. If no timestamp, the server
                        // applies current time to the edits.
                        Put p = new Put(Bytes.toBytes("myLittleRow"));
        
                        // To set the value you'd like to update in the row 'myLittleRow', specify
                        // the column family, column qualifier, and value of the table cell you'd
                        // like to update.  The column family must already exist in your table
                        // schema.  The qualifier can be anything.  All must be specified as byte
                        // arrays as hbase is all about byte arrays.  Lets pretend the table
                        // 'myLittleHBaseTable' was created with a family 'myLittleFamily'.
                        KeyValue kv = new KeyValue(p.getRow(), Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
                                Bytes.toBytes("Some Value"));
                        p.add(kv);
        
                        // Once you've adorned your Put instance with all the updates you want to
                        // make, to commit it do the following (The HTable#put method takes the
                        // Put instance you've been building and pushes the changes you made into
                        // hbase)
                        table.put(p);
        
                        // Now, to retrieve the data we just wrote. The values that come back are
                        // Result instances. Generally, a Result is an object that will package up
                        // the hbase return into the form you find most palatable.
                        Get g = new Get(Bytes.toBytes("myLittleRow"));
                        Result r = table.get(g);
                        byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"),
                                Bytes.toBytes("someQualifier"));
        
                        // If we convert the value bytes, we should get back 'Some Value', the
                        // value we inserted at this location.
                        String valueStr = Bytes.toString(value);
                        System.out.println("GET: " + valueStr);
        
                        // Sometimes, you won't know the row you're looking for. In this case, you
                        // use a Scanner. This will give you cursor-like interface to the contents
                        // of the table.  To set up a Scanner, do like you did above making a Put
                        // and a Get, create a Scan.  Adorn it with column names, etc.
                        Scan s = new Scan();
                        s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
                        ResultScanner scanner = table.getScanner(s);
                        try {
                            // Scanners return Result instances.
                            // Now, for the actual iteration. One way is to use a while loop like so:
                            for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
                                // print out the row we found and the columns we were looking for
                                System.out.println("Found row: " + rr);
                            }
        
                            // The other approach is to use a foreach loop. Scanners are iterable!
                            // for (Result rr : scanner) {
                            //   System.out.println("Found row: " + rr);
                            // }
                        } finally {
                            // Make sure you close your scanners when you are done!
                            // Thats why we have it inside a try/finally clause
                            scanner.close();
                        }
        
                        // Close your table and cluster connection.
                    } finally {
                        if (table != null) table.close();
                    }
                } finally {
                    connection.close();
                }
            }
        }
        
      • 通过 ZK 公网连接地址访问实例

        import java.io.IOException;
        import org.apache.hadoop.conf.Configuration;
        import org.apache.hadoop.hbase.HBaseConfiguration;
        import org.apache.hadoop.hbase.HConstants;
        import org.apache.hadoop.hbase.KeyValue;
        import org.apache.hadoop.hbase.TableName;
        import org.apache.hadoop.hbase.client.Connection;
        import org.apache.hadoop.hbase.client.ConnectionFactory;
        import org.apache.hadoop.hbase.client.Get;
        import org.apache.hadoop.hbase.client.Table;
        import org.apache.hadoop.hbase.client.Put;
        import org.apache.hadoop.hbase.client.Result;
        import org.apache.hadoop.hbase.client.ResultScanner;
        import org.apache.hadoop.hbase.client.Scan;
        import org.apache.hadoop.hbase.util.Bytes;
        
        // Class that has nothing but a main.
        // Does a Put, Get and a Scan against an hbase table.
        // The API described here is since HBase 1.0.
        public class MyLittleHBaseClient {
            public static void main(String[] args) throws IOException {
                // You need a configuration object to tell the client where to connect.
                // When you create a HBaseConfiguration, it reads in whatever you've set
                // into your hbase-site.xml and in hbase-default.xml, as long as these can
                // be found on the CLASSPATH
                Configuration config = HBaseConfiguration.create();
        
                // Next you need a Connection to the cluster. Create one. When done with it,
                // close it. A try/finally is a good way to ensure it gets closed or use
                // the jdk7 idiom, try-with-resources: see
                // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
                //
                // Connections are heavyweight.  Create one once and keep it around. From a Connection
                // you get a Table instance to access Tables, an Admin instance to administer the cluster,
                // and RegionLocator to find where regions are out on the cluster. As opposed to Connections,
                // Table, Admin and RegionLocator instances are lightweight; create as you need them and then
                // close when done.
                //
        
                config.set(HConstants.ZOOKEEPER_QUORUM, <zkEndpoint:Port>);
                config.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/hbase/<HBase 实例 ID>");
                config.set("zookeeper.znode.metaserver", "public-meta-region-server");
                config.set("zookeeper.znode.master", "public-master")
                
                //HConstants.ZOOKEEPER_ZNO
                Connection connection = ConnectionFactory.createConnection(config);
        
                try {
        
                    // The below instantiates a Table object that connects you to the "myLittleHBaseTable" table
                    // (TableName.valueOf turns String into a TableName instance).
                    // When done with it, close it (Should start a try/finally after this creation so it gets
                    // closed for sure the jdk7 idiom, try-with-resources: see
                    // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)
                    Table table = connection.getTable(TableName.valueOf("myLittleHBaseTable"));
                    try {
        
                        // To add to a row, use Put.  A Put constructor takes the name of the row
                        // you want to insert into as a byte array.  In HBase, the Bytes class has
                        // utility for converting all kinds of java types to byte arrays.  In the
                        // below, we are converting the String "myLittleRow" into a byte array to
                        // use as a row key for our update. Once you have a Put instance, you can
                        // adorn it by setting the names of columns you want to update on the row,
                        // the timestamp to use in your update, etc. If no timestamp, the server
                        // applies current time to the edits.
                        Put p = new Put(Bytes.toBytes("myLittleRow"));
        
                        // To set the value you'd like to update in the row 'myLittleRow', specify
                        // the column family, column qualifier, and value of the table cell you'd
                        // like to update.  The column family must already exist in your table
                        // schema.  The qualifier can be anything.  All must be specified as byte
                        // arrays as hbase is all about byte arrays.  Lets pretend the table
                        // 'myLittleHBaseTable' was created with a family 'myLittleFamily'.
                        KeyValue kv = new KeyValue(p.getRow(), Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
                                Bytes.toBytes("Some Value"));
                        p.add(kv);
        
                        // Once you've adorned your Put instance with all the updates you want to
                        // make, to commit it do the following (The HTable#put method takes the
                        // Put instance you've been building and pushes the changes you made into
                        // hbase)
                        table.put(p);
        
                        // Now, to retrieve the data we just wrote. The values that come back are
                        // Result instances. Generally, a Result is an object that will package up
                        // the hbase return into the form you find most palatable.
                        Get g = new Get(Bytes.toBytes("myLittleRow"));
                        Result r = table.get(g);
                        byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"),
                                Bytes.toBytes("someQualifier"));
        
                        // If we convert the value bytes, we should get back 'Some Value', the
                        // value we inserted at this location.
                        String valueStr = Bytes.toString(value);
                        System.out.println("GET: " + valueStr);
        
                        // Sometimes, you won't know the row you're looking for. In this case, you
                        // use a Scanner. This will give you cursor-like interface to the contents
                        // of the table.  To set up a Scanner, do like you did above making a Put
                        // and a Get, create a Scan.  Adorn it with column names, etc.
                        Scan s = new Scan();
                        s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
                        ResultScanner scanner = table.getScanner(s);
                        try {
                            // Scanners return Result instances.
                            // Now, for the actual iteration. One way is to use a while loop like so:
                            for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
                                // print out the row we found and the columns we were looking for
                                System.out.println("Found row: " + rr);
                            }
        
                            // The other approach is to use a foreach loop. Scanners are iterable!
                            // for (Result rr : scanner) {
                            //   System.out.println("Found row: " + rr);
                            // }
                        } finally {
                            // Make sure you close your scanners when you are done!
                            // Thats why we have it inside a try/finally clause
                            scanner.close();
                        }
        
                        // Close your table and cluster connection.
                    } finally {
                        if (table != null) table.close();
                    }
                } finally {
                    connection.close();
                }
            }
        }