Idea創建maven項目,通過java API操作Hbase數據庫

一、準備

啓動hdfs集羣(之前的HA集羣),啓動Hbase。

 

二、創建工程

三、API操作Hbase分佈式數據庫

1)插入一條數據

 @Test
    public void put() throws Exception {
        //創建conf對象    會加載你項目資源文件下的兩個XML文件
        Configuration conf = HBaseConfiguration.create();
        //通過連接工廠創建連接對象
        Connection conn = ConnectionFactory.createConnection(conf);
        //通過連接查詢tableName對象    表名
        TableName tname = TableName.valueOf("ns1:t1");
        //獲得table
        Table table = conn.getTable(tname);
        //通過bytes工具類創建字節數組(將字符串)
        byte[] rowid = Bytes.toBytes("row1");  //rowkey名
        //創建put對象
        Put put = new Put(rowid);
        byte[] f1 = Bytes.toBytes("f1");  //列簇名
        byte[] id = Bytes.toBytes("id") ; //列名
        byte[] value = Bytes.toBytes(102); //設置值
        put.addColumn(f1,id,value);
        //執行插入
        table.put(put);
    }

 執行結束,在shell裏面查詢結果如下:(hbase存儲的數據都是字節數組)

 

2)批量插入一萬條數據

  @Test
    public void bigInsert() throws Exception {
        DecimalFormat format = new DecimalFormat();
        format.applyPattern("0000");
        long start = System.currentTimeMillis() ;
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");
        HTable table = (HTable)conn.getTable(tname);
        //不要自動清理緩衝區
        table.setAutoFlush(false);
        for(int i = 1 ; i < 10000 ; i ++){
            Put put = new Put(Bytes.toBytes("row" + format.format(i))) ;
            //關閉寫前日誌
            put.setWriteToWAL(false);
            put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("id"),Bytes.toBytes(i));
            put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"),Bytes.toBytes("tom" + i));
            put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("age"),Bytes.toBytes(i % 100));
            table.put(put);
            if(i % 2000 == 0){
                table.flushCommits();
            }
        }
        //手動刷新緩衝區
        table.flushCommits();
        System.out.println(System.currentTimeMillis() - start );
    }

 

3)獲取某一列的數據

 @Test
    public void get() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t1");
        Table table = conn.getTable(tname);
        byte[] rowid = Bytes.toBytes("row1");
        Get get = new Get(rowid);
        Result r = table.get(get);
        byte[] idvalue = r.getValue(Bytes.toBytes("f1"),Bytes.toBytes("id"));
        System.out.println(Bytes.toInt(idvalue));
    }

4)創建工作空間

    @Test
    public void createNameSpace() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //創建名字空間描述符
        NamespaceDescriptor nsd = NamespaceDescriptor.create("ns2").build();
        admin.createNamespace(nsd);
        //列舉出所有的工作空間
        NamespaceDescriptor[] ns = admin.listNamespaceDescriptors();
        for(NamespaceDescriptor n : ns){
            System.out.println(n.getName());
        }
    }

 因爲NamespaceDescriptor對象的構造函數是私有的,沒法直接new,所以創建模式是一下的調用步驟。使用了設計模式

 工作空間的創造步驟:

5)創建表

    @Test
    public void createTable() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //創建表名對象
        TableName tableName = TableName.valueOf("ns2:t2");
        //創建表描述符對象
        HTableDescriptor tbl = new HTableDescriptor(tableName);
        //創建列族描述符
        HColumnDescriptor col = new HColumnDescriptor("f1" );
        tbl.addFamily(col);

        admin.createTable(tbl);
        System.out.println("over");
    }

 HBaseAdmin這個類有很多關於工作空間、表、Region等的操作

6)根據rowkey範圍掃描表

 @Test
    public void scan() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");
        Table table = conn.getTable(tname);
        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes("row5000"));//包前  [row5000,
        scan.setStopRow(Bytes.toBytes("row5050"));//不包後         ,row5050)
        ResultScanner rs = table.getScanner(scan);
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()) {
            Result r = it.next();
            byte[] name = r.getValue(Bytes.toBytes("f1"), Bytes.toBytes("name"));
            System.out.println(Bytes.toString(name));
        }
    }

7)指定版本掃描

 @Test
    public void getWithVersions() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t3");
        Table table = conn.getTable(tname);
        Get get = new Get(Bytes.toBytes("row001"));
        //檢索所有版本
        get.setMaxVersions();
        Result r = table.get(get);
        List<Cell> cells = r.getColumnCells(Bytes.toBytes("f1"), Bytes.toBytes("name"));
        for (Cell c : cells) {
            String f = Bytes.toString(c.getFamily());
            String col = Bytes.toString(c.getQualifier());
            long ts = c.getTimestamp();
            String val = Bytes.toString(c.getValue());
            System.out.println(f + "/" + col + "/" + ts + "=" + val);
        }
    }

8)過濾器

  @Test
    public void testRowFilter() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");
        Scan scan = new Scan();
        //掃描出rowkey < row0100的數據,rowkey根據字典排序的
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                new BinaryComparator(Bytes.toBytes("row0100")));
        scan.setFilter(rowFilter);
        Table t = conn.getTable(tname);
        ResultScanner rs = t.getScanner(scan);
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()) {
            Result r = it.next();
            System.out.println(Bytes.toString(r.getRow()));
        }
    }

 

 過濾器類型:

比較方式類型:

這些就是基本的Hbase的API操作,有的類沒有測試,比如多條件的scan,常用的過濾器等。

還有一個比較重要的是協處理器coprocessor的API。

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章