hbaseAPI DML資料增刪改
阿新 • • 發佈:2019-01-14
public static void main(String[] args) throws IOException { System.setProperty("hadoop.home.dir", "D:\\hadoop-2.6.0-cdh5.15.0"); Configuration conf = new Configuration(); conf.set("zookeeper.znode.parent", "/hbase"); conf.set("hbase.zookeeper.quorum", "candle"); conf.set("hbase.zookeeper.property.clientPort", "2181"); Connection connection = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("hadoop:human"); Table table = connection.getTable(tableName); //插入資料 //插入操作封裝到了Put 必須要指定rowkey // int rowkey = 825373492; // Put put = new Put(Bytes.toBytes(rowkey)); // put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("id"), Bytes.toBytes("1")); // put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("candle")); // // //觸發插入的操作 // table.put(put); //往table插入多行 // ArrayList<Put> puts = new ArrayList<>(); // // Put put1 = new Put(Bytes.toBytes("001")); // put1.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("id"), Bytes.toBytes("1")); // put1.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("candle")); // puts.add(put1); // // Put put2 = new Put(Bytes.toBytes("002")); // put2.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("id"), Bytes.toBytes("1")); // put2.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("candle")); // puts.add(put2); // // table.put(puts); //查詢Get 需要指定rowkey Get get = new Get(Bytes.toBytes("2")); //查詢一個單元格 //get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("column")); //Result 封裝了查詢的結果 Result result = table.get(get); //一次查詢一行 Map<String, String> cellMap = getRowResult(result); //......... //查詢得到一個單元格資料 //byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("column")); //System.err.println(new String(value)); //全表掃描 獲取整個表 info列族資料 ResultScanner rs = table.getScanner(Bytes.toBytes("info")); Iterator<Result> iterator = rs.iterator(); while (iterator.hasNext()) { Result next = iterator.next(); getRowResult(next); } //刪除 Delete Delete delete = new Delete(Bytes.toBytes("2")); //row key delete.addColumn(Bytes.toBytes("info"), Bytes.toBytes("column")); table.delete(delete); } //把result其中 每一個cell 以map格式儲存 public static Map<String, String> getRowResult(Result result) { if(result.isEmpty()) { return null; //判斷查詢的結果是否為空,如果為空直接返回 } HashMap<String, String> cellMap = new HashMap<>(); //獲取所有cell List<Cell> cells = result.listCells(); String family = null; String key = null; String value = null; for (Cell tmp:cells) { //列族 列名 值 // family = Bytes.toString(tmp.getFamilyArray(), tmp.getFamilyOffset(), tmp.getFamilyLength()); key = Bytes.toString(tmp.getQualifierArray(), tmp.getQualifierOffset(), tmp.getQualifierLength()); value = Bytes.toString(tmp.getValueArray(), tmp.getValueOffset(), tmp.getValueLength()); System.err.println("famliy:" + family + "-key:" + key + "-value" + value); cellMap.put(key,value); // tmp.getFamilyArray();//列族所在陣列 // tmp.getFamilyOffset(); //偏移 // tmp.getFamilyLength(); //長度 // // // tmp.getQualifierArray(); // tmp.getQualifierOffset(); // tmp.getQualifierLength(); // // // tmp.getValueArray(); // tmp.getValueOffset(); // tmp.getValueLength(); } return cellMap; }