博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hbaseAPI DML数据增删改
阅读量:4161 次
发布时间:2019-05-26

本文共 3520 字,大约阅读时间需要 11 分钟。

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
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
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
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
getRowResult(Result result) { if(result.isEmpty()) { return null; //判断查询的结果是否为空,如果为空直接返回 } HashMap
cellMap = new HashMap<>(); //获取所有cell List
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; }

 

转载地址:http://hrjxi.baihongyu.com/

你可能感兴趣的文章
LDD3源码分析之llseek分析(二)
查看>>
printk及控制台的日志级别
查看>>
Linux驱动加载实例
查看>>
详解数据库设计中的三大范式理论
查看>>
JDBCUtils工具类
查看>>
Linux基本命令(1)
查看>>
Linux基本命令(二)
查看>>
Hive2.0函数大全(中文版)
查看>>
hive里面的连接操作(join)
查看>>
卸载oracle
查看>>
hive 自定义函数jar发布的方法
查看>>
对DMA传输机制的学习
查看>>
QT中this指针
查看>>
java中的异常机制
查看>>
java SE面向对象思维导图
查看>>
数据结构常见排序
查看>>
Docker搭建各平台的启动命令集锦
查看>>
Docker第八篇-docker-compose教程(介绍,安装,入门示例)
查看>>
Docker第九篇-docker-compose命令和模板文件说明
查看>>
CSDN-markdown编辑器使用
查看>>