Hadoop3系列——(八)Java訪問Hbase

重金求子發表於2020-10-07

pom檔案

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>2.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-auth</artifactId>
            <version>3.1.2</version>
        </dependency>

 

建立表:

public static void createTable(){
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node1,node2,node3");

        try {
            //建立連線
            Connection conn = ConnectionFactory.createConnection(conf);
            Admin admin = conn.getAdmin();
            //新增表描述
            HTableDescriptor tableDes = new HTableDescriptor(TableName.valueOf("test11"));
            //新增族列
            HColumnDescriptor colDesc = new HColumnDescriptor("cf1");
            tableDes.addFamily(colDesc);//把族列新增到表
            admin.createTable(tableDes);//建立表
        } catch (Exception e) {
            e.printStackTrace();
        }
}

 

插入資料(修改也是用該方法):

public static void addData(){
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node1,node2,node3");

        try {
            //建立連線
            Connection conn = ConnectionFactory.createConnection(conf);
            //連線表
            Table table = conn.getTable(TableName.valueOf("test11"));
            //設定Row Key
            Put put = new Put("110".getBytes());
            //設定列族名cf1,列名name,列值
            put.addColumn("cf1".getBytes(),"name".getBytes(),"aaaa".getBytes());
            table.put(put);
        } catch (Exception e) {
            e.printStackTrace();
        }
}

 

查詢資料:

查詢指定cell的資料

//獲取指定cell資料
    public static void getData(){
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node1,node2,node3");

        try {
            //建立連線
            Connection conn = ConnectionFactory.createConnection(conf);
            //連線表
            Table table = conn.getTable(TableName.valueOf("test11"));
            //通過Row Key獲取指定行
            Get get = new Get("110".getBytes());
            Result rs = table.get(get);
            //通過行指定列,獲取cell
            Cell cell = rs.getColumnLatestCell("cf1".getBytes(),"name".getBytes());
            System.out.println(new String(CellUtil.cloneRow(cell),"utf-8"));
            System.out.println(new String(CellUtil.cloneValue(cell),"utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

查詢指定表的全部資料

//獲取指定表的所有資料
    public static void getDataByScan(){
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node1,node2,node3");

        try {
            //建立連線
            Connection conn = ConnectionFactory.createConnection(conf);
            //連線表
            Table table = conn.getTable(TableName.valueOf("test11"));
            Scan scan = new Scan();

            //獲取遍歷查詢的結果
            ResultScanner resultScanner = table.getScanner(scan);
            //對結果集進行迭代
            Iterator its = resultScanner.iterator();
            while (its.hasNext()){
                Result rs = (Result) its.next();
                //通過行指定列,獲取cell
                Cell cell = rs.getColumnLatestCell("cf1".getBytes(),"name".getBytes());
                System.out.println(new String(CellUtil.cloneRow(cell),"utf-8"));
                System.out.println(new String(CellUtil.cloneValue(cell),"utf-8"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

相關文章