01.加载maven依赖

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
	<version>2.1.1</version>
</dependency>

02.准备数据

image-20220417014152030

​ 在dbeaver上显示为:

image-20220417014230168

03.连接读取数据

​ hbase.zookeeper.quorum使用的是Zookeeper的信息

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

public class JavaConectHbase {
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.1.10:2181");
        try {
            Connection connection = ConnectionFactory.createConnection(conf);
//            Admin admin = connection.getAdmin();
            System.out.println("连接测试通过");
        } catch (Exception e){
            e.printStackTrace();
        }
        Connection conn = ConnectionFactory.createConnection(conf);
        Table gadaite = conn.getTable(TableName.valueOf("Gadaite"));
        ResultScanner scanner = gadaite.getScanner(new Scan());
        for (Result sc:scanner){
            for (Cell c:sc.rawCells()){
                System.out.println(c);
            }
        }
    }
}

​ 输出结果:

image-20220417014452190

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐