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;
}
hbaseAPI DML数据增删改
猜你喜欢
转载自blog.csdn.net/suojie123/article/details/86250750
今日推荐
周排行