hbase基本操作
2016-04-21 20:56
369 查看
首先: conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "集群地址"); conf.set("hbase.master", "地址+端口"); conn = HConnectionManager.createConnection(conf); admin = new HBaseAdmin(conf); 插入 Put (更新同理)------------------------------------------------------ 插入单条记录 String tablename = "proreply"; String row = rowFixd; String columnFamily = "data"; String[] columns = new String[] { "content", "rusername", "ctime" }; String[] values = new String[] { "回复内容:........", "lxk", DateFormatUtils.format(new Date(), "yyyy-mm-dd HH:mm:ss.ms") }; HTableInterface table = null; try { table = conn.getTable(tablename); Put put = new Put(Bytes.toBytes(row)); for (int i = 0; i < columns.length; i++) { put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(String.valueOf(columns[i])), values[i] == null ? null : values[i].getBytes("utf-8")); table.put(put); } System.out.println("======插入记录完成======"); 插入多条记录 // table.setAutoFlush(false); // table.setWriteBufferSize(6 * 1024 * 1024); if (null != rowDatas && rowDatas.size() > 0) { for (RowData rowData : rowDatas) { String row = rowData.getRow(); String[] columns = rowData.getColumns(); String columnFamily = rowData.getColumnFamily(); String[] values = rowData.getValues(); Put put = new Put(Bytes.toBytes(row)); for (int i = 0; i < columns.length; i++) { put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(String.valueOf(columns[i])), values[i] == null ? null : values[i] .getBytes("utf-8")); put.setDurability(Durability.SKIP_WAL); table.put(put); // puts.add(put); } } // table.put(puts); // table.batch(puts); // table.flushCommits(); 删除----------------------------------------------------------------------- HTableInterface htable = conn.getTable("reguser".getBytes()); Delete del = new Delete(Bytes.toBytes("chenzhenjian1448850414722")); del.compareTo(del); htable.delete(del); 查询----------------------------------------------------------------------- 查询所有记录 HTableInterface htable = conn.getTable("表名".getBytes()); Scan scan = new Scan(); ResultScanner scanner = htable.getScanner(scan); for (Result result : scanner) { byte[] row = result.getRow(); List<KeyValue> list = result.list(); for (KeyValue keyValue : list) { System.out.print("行键:" + new String(row)); System.out.print("\t列簇:" + new String(keyValue.getFamily())); System.out.print("\t列名:" + new String(keyValue.getQualifier())); System.out.print("\t时间:" + keyValue.getTimestamp()); System.out.println("\t列值:" + new String(keyValue.getValue())); } } 查询一行记录 Get g = new Get(rowKey.getBytes()); Result rs = table.get(g); KeyValue[] raw = rs.raw(); Map<String, String> result = new HashMap<String, String>(); for (int i = 0; i < raw.length; i++) { byte[] row = raw[i].getRow(); result.put("row", new String(row)); byte[] family = raw[i].getFamily(); byte[] qualifier = raw[i].getQualifier(); byte[] value = raw[i].getValue(); result.put(new String(family) + ":" + new String(qualifier), new String(value)); } return result; 查询过滤条件--------------------------------------------------------------------------- 提取rowkey以[prefix]前缀开头 Filter filter=new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(prefix.getBytes())); Scan s=new Scan(); s.setFilter(filter); 提取rowkey以[endfix]结尾 Filter filter=new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(".*"+endfix+"$")); Scan s=new Scan(); s.setFilter(filter); 提取rowkey以包含[value] Filter filter=new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(value)); Scan s=new Scan(); s.setFilter(filter); 提取rowkey以[prefix]前缀开头、并且只包含rowkey、 Filter filter1=new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(prefix.getBytes())); KeyOnlyFilter filter2=new KeyOnlyFilter(); FilterList filtList =new FilterList(FilterList.Operator.MUST_PASS_ALL); filtList.addFilter(filter1); filtList.addFilter(filter2); Scan s=new Scan(); s.setFilter(filtList); 分页提取10条:rowkey以[prefix]前缀开头、 Filter filter1=new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(prefix.getBytes())); PageFilter filter2=new PageFilter(10); FilterList filtList =new FilterList(FilterList.Operator.MUST_PASS_ALL); filtList.addFilter(filter1); filtList.addFilter(filter2); Scan s=new Scan(); s.setFilter(filtList); 分页提取10条:仅获取data列族上某列上大于某时间戳的数据 PageFilter filter=new PageFilter(10); Scan s=new Scan(); s.addColumn("data".getBytes(), colmnName.getBytes()); s.setFilter(filter); if(0!=minStamp) s.setTimeRange(minStamp, Long.MAX_VALUE); return s; 分页提取10条:大于某时间戳的数据(针对所有列) PageFilter filter=new PageFilter(10); Scan s=new Scan(); s.setFilter(filter); if(0!=minStamp) s.setTimeRange(minStamp, Long.MAX_VALUE); return s;
相关文章推荐
- 实验二 作业调度模拟程序
- code first 创建和初始化数据库
- YTU 2335: 0-1背包问题
- iOS小技巧 - 利用xcode快速找到沙盒
- 初探科大讯飞-讯飞开放平台之语音合成
- OpenCV3.1编译
- 不支持滑动的GridView
- 四年IT之路,获得了什么
- 3dsMax界面概述
- 最大堆
- 二叉树的简单实现及其部分面试题
- Android实现点击更换头像
- hbase触发器问题汇总,持续更新中...
- 安装Python及后续
- 生产者消费者
- 典型用户和用户场景描述
- hdu 1224(动态规划 DAG上的最长路)
- STM32 USB学习笔记
- How to program EMW3165 using command line interface?
- GRE实验