hbase基本操作
来源:互联网 发布:ps mac版多少钱 编辑:程序博客网 时间:2024/06/05 17:39
首先: 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;
0 0
- HBase 基本操作
- hbase基本操作
- Hbase shell基本操作
- Hbase基本操作
- hbase基本操作
- hbase基本操作2
- HBase的基本操作
- Hbase的基本操作
- HBase命令行基本操作
- HBase基本操作
- Hbase 基本操作
- Hbase基本操作示例
- Hbase 基本操作总结
- hbase基本操作
- Hbase Java 基本操作
- hbase的基本操作
- Hbase 基本操作
- Hbase API 基本操作
- React Native 运行android程序报SDK文件不存在错误
- C 经典 冒泡排序
- 使用Runnable接口实现线程
- TimesTen 应用层数据库缓存学习:10. 监控缓存组的autorefresh操作
- Insterstellar wallpapers
- hbase基本操作
- c语言数据类型取值范围
- java中功能选项的移除指南(二)
- 山东省第五届ACM大学生程序设计竞赛-Weighted Median(模拟)
- HDU 1540 Tunnel Warfare(区间合并)
- 各种有关文件的对话框的创建方法
- java时间戳正序和倒序
- Java EE -> Get与Post 编码问题
- LeetCode *** 207. Course Schedule (Topological Sort )