java操做Hbase

public class Test {
    public Connection connection;
    // 用HBaseconfiguration初始化配置信息是會自動加載當前應用的classpath下的hbase-site.xml
    public static Configuration configuration = HBaseConfiguration.create();

    public Test() throws Exception {
        // 固然也能夠手動加載配置文件,手動加載配置文件時要調用configuration的addResource方法
        // configuration.addResource("hbase-site.xml");
        connection = ConnectionFactory.createConnection(configuration);
    }

    public void createTable(String tableName, String... cf1) throws Exception {
        Admin admin = connection.getAdmin();

        //HTD須要TableName類型的tableName,建立TableName類型的tableName
        TableName tbName = TableName.valueOf(tableName);
        //判斷表述否已存在,不存在則建立表
        if (admin.tableExists(tbName)) {
            System.err.println("表" + tableName + "已存在!");
            return;
        }
        //經過HTableDescriptor建立一個HTableDescriptor將表的描述傳到createTable參數中
        HTableDescriptor HTD = new HTableDescriptor(tbName);
        //爲描述器添加表的詳細參數
        for (String cf : cf1) {
            // 建立HColumnDescriptor對象添加表的詳細的描述
            HColumnDescriptor HCD = new HColumnDescriptor(cf);
            HTD.addFamily(HCD);
        }
        //調用createtable方法建立表
        admin.createTable(HTD);
    }

    public void deleteTable(String tableName) throws Exception {
        Admin admin = connection.getAdmin();
        //經過tableName建立表名
        TableName tbName = TableName.valueOf(tableName);
        //判斷表是否存在,若存在就刪除,不存在就退出
        if (admin.tableExists(tbName)) {
            //首先將表解除佔用,不然沒法刪除
            admin.disableTable(tbName);
            //調用delete方法
            admin.deleteTable(tbName);
            System.err.println("表" + tableName + "已刪除");
        } else {
            System.err.println("表" + tableName + "不存在!");
        }
    }

    public void putData() throws Exception {
        //經過表名獲取tbName
        TableName tbname = TableName.valueOf("t1");
        //經過connection獲取相應的表
        Table table = connection.getTable(tbname);
        //建立Random對象以做爲隨機參數
        Random random = new Random();
        //hbase支持批量寫入數據,建立Put集合來存放批量的數據
        List<Put> batput = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            //實例化put對象,傳入行鍵
            Put put = new Put(Bytes.toBytes("rowkey_" + i));
            //調用addcolum方法,向f1列簇中添加字段
            put.addImmutable(Bytes.toBytes("f1"), Bytes.toBytes("username"), Bytes.toBytes("un_" + i));
            put.addImmutable(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes(random.nextInt(50) + 1));
            put.addImmutable(Bytes.toBytes("f1"), Bytes.toBytes("birthday"), Bytes.toBytes("2017" + i));
            put.addImmutable(Bytes.toBytes("f1"), Bytes.toBytes("phone"), Bytes.toBytes("phone:" + i));
            put.addImmutable(Bytes.toBytes("f1"), Bytes.toBytes("郵箱"), Bytes.toBytes("郵箱:" + i));
            //將測試數據添加到list中
            batput.add(put);
        }
        //調用put方法將list中的測試數據寫入hbase
        table.put(batput);
        System.err.println("數據插入完成!");
    }

    public void getData() throws Exception {
        //獲取想要查詢的表的TableName
        TableName tbname = TableName.valueOf("t1");
        //經過tbName得到Table對象
        Table table = connection.getTable(tbname);
        //建立Get的集合以承接查詢的條件
        List<Get> gets = new ArrayList<Get>();
        //循環五次,取前五個測試數據
        for (int i = 0; i < 5; i++) {
            //就將查詢條件放入get對象中
            Get get = new Get(Bytes.toBytes("rowkey_" + i));
            //將get對象放入聚合
            gets.add(get);
        }
        //調用table.get方法傳入查詢條件,得到查詢的結果的數組
        Result[] results = table.get(gets);
        //遍歷結果數組,利用CellScanner配合cellUtil得到對應的數據
        for (Result result : results) {
            //調用result.cellscanner建立scanner對象
            CellScanner cellScanner = result.cellScanner();
            //遍歷結果集,取出查詢結果,
            //若是存在下一個cell則advandce方法返回true,且current方法會返回一個有效的cell,能夠看成循環條件
            while (cellScanner.advance()) {
                //current方法返回一個有效的cell
                Cell cell = cellScanner.current();
                //使用CellUtil調用相應的方法獲取想用的數據,並利用Bytes.toString方法將結果轉換爲string輸出
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualify = Bytes.toString(CellUtil.cloneQualifier(cell));
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.err.println(family + "_" + qualify + "_" + rowkey + "_" + value);
            }
        }
    }

    public void getData1() throws Exception {
        TableName tbname = TableName.valueOf("bd14:fromJava");
        Table table = connection.getTable(tbname);
        List<Get> gets = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Get get = new Get(Bytes.toBytes("rowkey_" + i));
            gets.add(get);
        }
        Result[] results = table.get(gets);
        for (Result result : results) {
            NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
            for (byte[] cf : map.keySet()) {
                NavigableMap<byte[], NavigableMap<Long, byte[]>> valueWithColumnQualify = map.get(cf);
                for (byte[] columnQualify : valueWithColumnQualify.keySet()) {
                    NavigableMap<Long, byte[]> valueWithTimestamp = valueWithColumnQualify.get(columnQualify);
                    for (Long ts : valueWithTimestamp.keySet()) {
                        byte[] value = valueWithTimestamp.get(ts);
                        String rowKey = Bytes.toString(result.getRow());
                        String columnFamily = Bytes.toString(cf);
                        String columnqualify = Bytes.toString(columnQualify);
                        String timestamp = new Date(ts) + "";
                        String values = Bytes.toString(columnQualify);
                        System.out.println(rowKey + "-" + columnFamily + "-" + columnqualify + "-" + timestamp + "-" + values);
                    }
                }
            }
        }
    }

    public void getData2() throws Exception {
        TableName tbname = TableName.valueOf("bd14:fromJava");
        Table table = connection.getTable(tbname);
        List<Get> gets = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Get get = new Get(Bytes.toBytes("rowkey_" + i));
            gets.add(get);
        }
        Result[] results = table.get(gets);
        //遍歷結果對象results
        for (Result result : results) {
            //嵌套遍歷result獲取cell
            for (Cell cell : result.listCells()) {
                //使用CellUtil工具類直接獲取cell中的數據
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualify = Bytes.toString(CellUtil.cloneQualifier(cell));
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.err.println(family + "_" + qualify + "_" + rowkey + "_" + value);
            }
        }

    }

    public void updateData(String tableName, String rowKey, String family, String columkey, String updatedata) throws Exception {
        //hbase中更新數據一樣採用put方法,在相同的位置put數據,則在查詢時只會返回時間戳較新的數據
        //且在文件合併時會將時間戳較舊的數據捨棄
        Put put = new Put(Bytes.toBytes(rowKey));
        //將新數據添加到put中
        put.addImmutable(Bytes.toBytes(family), Bytes.toBytes(columkey), Bytes.toBytes(updatedata));
        Table table = connection.getTable(TableName.valueOf(tableName));
        //將put寫入HBase
        table.put(put);
    }

    //刪除某條記錄
    public void deleteData(String tableName,String rowKey,String family, String columkey) throws Exception{
        Table table = connection.getTable(TableName.valueOf(tableName));
        //建立delete對象
        Delete deletData= new Delete(Bytes.toBytes(rowKey));
        //將要刪除的數據的準確座標添加到對象中
        deletData.addColumn(Bytes.toBytes(family), Bytes.toBytes(columkey));
        //刪除表中數據
        table.delete(deletData);
    }

    //刪除一行數據
    public void deleteRow(String tableName,String rowKey) throws Exception{
        Table table = connection.getTable(TableName.valueOf(tableName));
        //經過行鍵刪除一整行的數據
        Delete deletRow= new Delete(Bytes.toBytes(rowKey));
        table.delete(deletRow);
    }


    public static void main(String[] args) {
        try {
            Test test = new Test();
            test.createTable("t1", "f1");
//            test.putData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索