hbase java api
2016-01-07 13:54
483 查看
package com.jkxy.bigdata.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.ByteArrayComparable; import org.apache.hadoop.hbase.filter.ColumnPrefixFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.PageFilter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.SubstringComparator; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hdfs.util.ByteArray; import org.apache.hadoop.io.IOUtils; public class HBaseTest { public static void main(String[] args) throws Exception { // createTab(); testFilter(); } public static void testFilter() { HTable table = null; try { table = getTable(); Scan scan = new Scan(); // create filter Filter filter = null; filter = new PrefixFilter(Bytes.toBytes("rk")); filter = new PageFilter(3); // create hbase comparator ByteArrayComparable comp = null; comp = new SubstringComparator("lisi"); filter = new SingleColumnValueFilter( Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOp.EQUAL, comp); // set filter scan.setFilter(filter); ResultScanner rs = table.getScanner(scan); for (Result result : rs) { printResult(result); } } catch (Exception e) { e.printStackTrace(); } finally { if(table != null) { IOUtils.closeStream(table); } } } public static void scanData() { HTable table = null; try { table = getTable(); // create Scan instance Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("rk0001")); scan.setStopRow(Bytes.toBytes("rk0003")); scan.addFamily(Bytes.toBytes("info")); // set cache scan.setCacheBlocks(false); scan.setBatch(2); scan.setCaching(2); // set permmission // scan.setACL(perms); // get scanner results ResultScanner rs = table.getScanner(scan); for(Result result : rs) { printResult(result); } } catch (Exception e) { e.printStackTrace(); } finally { if(table != null) { IOUtils.closeStream(table); } } } public static void printResult(Result rs) { for (Cell cell : rs.rawCells()) { System.out.println( Bytes.toString(CellUtil.cloneFamily(cell)) + " : " + Bytes.toString(CellUtil.cloneRow(cell)) + " : " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " : " + Bytes.toString(CellUtil.cloneValue(cell)) ); } } public static void testNamespace() throws Exception { Configuration c = HBaseConfiguration.create(); HBaseAdmin admin = new HBaseAdmin(c ); // create namespace NamespaceDescriptor namespace = NamespaceDescriptor.create("ns1").build(); // admin.createNamespace(namespace ); admin.deleteNamespace("ns1"); // close resource admin.close(); } public static void getData() throws Exception { HTable table = getTable(); Get get = new Get(Bytes.toBytes("rk0001")); // get data Result result = table.get(get ); /*byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")); System.out.println(new String(value));*/ for (Cell cell : result.rawCells()) { // System.out.println(new String(cell.getFamily()) + " : " // + new String(cell.getQualifier()) + " : " + new String(cell.getValue())); System.out.println(Bytes.toString(CellUtil.cloneRow(cell))); System.out.println(Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println(Bytes.toString(CellUtil.cloneValue(cell))); } } public static void putData() throws Exception { //get table HTable table = getTable(); //put data Put put =new Put(Bytes.toBytes("rk0001")); put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lisi")); table.put(put); // close resource table.close(); } public static HTable getTable() throws Exception { Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, Bytes.toBytes("t1")); return table; } public static void createTab() throws Exception { // create a defult configuration Configuration conf = HBaseConfiguration.create(); // create a hbaseadmin HBaseAdmin admin = new HBaseAdmin(conf); // if exists. delete table boolean b = admin.tableExists(Bytes.toBytes("t1")); if (b) { admin.disableTable(Bytes.toBytes("t1")); admin.deleteTable("t1"); } HTableDescriptor table = new HTableDescriptor(TableName.valueOf("t1")); table.addFamily(new HColumnDescriptor(Bytes.toBytes("info"))); table.addFamily(new HColumnDescriptor(Bytes.toBytes("secret"))); // create table admin.createTable(table); admin.close(); } }
相关文章推荐
- java多态实现与说明
- Mybatis与Spring整合事物
- SpringMVC+JPA+Hibernate配置
- Spring声明式事务管理与配置介绍
- jdk安装
- eclipse 安装svn插件
- Eclipse导入项目No projects are found to import
- struts2请求过程源码分析(转)
- java环境配置
- spring与mybatis,strut2整合连接sqlserver不的不说的那点事儿
- spring3创建RESTFul Web Service
- eclipse 提高开发效率方法
- Struts2动态方法调用(DMI)小问题
- 在使用eclipse 运行 maven install的时候,出现这个错误:Unable to locate the Javac Compiler in:…
- Java获取字符的Unicode编码以及如何过滤特殊字符ZWNJ
- java中length,length(),size()区别
- Java专题 Socket编程之慕课网版本
- java 后台走过的坑
- Eclipse设置背景色
- java怎么连接sql server,需要注意的几点