hbase API操作范例

来源:互联网 发布:淘宝正品阿迪 编辑:程序博客网 时间:2024/04/29 03:29
public class HbaseDemo {private Configuration conf = null;@Beforepublic void init(){conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "lp5,lp6,lp7");}/* * 新建表 */@Testpublic void create() throws Exception{//Provides an interface to manage HBase database table metadata + general administrative functions. Use HBaseAdmin to create, drop, list, enable and disable tables. Use it also to add and drop table column families.//See HTable to add, update, and delete data from an individual table.//Currently HBaseAdmin instances are not expected to be long-lived. For example, an HBaseAdmin instance will not ride over a Master restart.//操纵hbase的客户端HBaseAdmin client = new HBaseAdmin(conf);//表名TableName tableName = TableName.valueOf("production");//表描述HTableDescriptor htd = new HTableDescriptor(tableName);//列族描述HColumnDescriptor base_info = new HColumnDescriptor("base_info");base_info.setMaxVersions(5);HColumnDescriptor external_info = new HColumnDescriptor("external_info");external_info.setMaxVersions(5);//添加列族htd.addFamily(base_info);htd.addFamily(external_info);//建表client.createTable(htd);client.close();}/* * 删除表结构 */@Testpublic void drop() throws Exception{HBaseAdmin client = new HBaseAdmin(conf);client.disableTable("production");client.deleteTable("production");client.close();}/* * 向表中添加数据 */@Testpublic void insert() throws Exception{//1.这种方法线程不安全//得到表HTable table = new HTable(conf, "production");//指定一行Put put = new Put(Bytes.toBytes("p_computer_0001"));//插入值put.add("base_info".getBytes(),"name".getBytes(),"Hongji".getBytes());put.add("base_info".getBytes(),"price".getBytes(),"3890".getBytes());put.add("external_info".getBytes(),"image".getBytes(),"nothing".getBytes());table.put(put);table.close();/*HConnection conn = HConnectionManager.getConnection(conf);HTableInterface table = conn.getTable("production");Put put = new Put(Bytes.toBytes("p_computer_0001"));put.add("base_info".getBytes(),"name".getBytes(),"Apple".getBytes());table.put(put);table.close();*/}/* * 删除表中数据 * 但是删除新版本的数据后,旧版本的就顶上来了,咋个办???、、、指定版本即可 */@Testpublic void delete() throws Exception{HTable table = new HTable(conf,"production");//单行删除Delete del = new Delete(Bytes.toBytes("p_computer_0001"));del.deleteColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"));table.delete(del);table.close();}/* * 查找数据 */@Testpublic void get() throws IOException{HTable table = new HTable(conf,"production");Get get = new Get(Bytes.toBytes("p_computer_0001"));get.setMaxVersions(1);//设定要查几个版本的数据Result result = table.get(get);List<Cell> cells = result.listCells();for(KeyValue kv : result.list()){String family = new String(kv.getFamily());//获取列族System.out.println(family);String qualifier = new String(kv.getQualifier());System.out.println(qualifier);System.out.println(new String(kv.getValue()));}table.close();}/* * 过滤查找数据 */@Testpublic void scan() throws IOException{HTable table = new HTable(conf,"production");Scan scan = new Scan(Bytes.toBytes("p_computer_0001"), Bytes.toBytes("p_computer_0003"));//前缀过滤器----针对行键Filter filter = new PrefixFilter(Bytes.toBytes("p"));//行过滤器ByteArrayComparable rowComparator = new BinaryComparator(Bytes.toBytes("p_computer_0001"));RowFilter rf = new RowFilter(CompareOp.LESS_OR_EQUAL, rowComparator);/**         * 假设rowkey格式为:创建日期_发布日期_ID_TITLE         * 目标:查找  发布日期  为  2014-12-21  的数据         */        rf = new RowFilter(CompareOp.EQUAL , new SubstringComparator("_2014-12-21_"));//单值过滤器 1 完整匹配字节数组new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, "zhangsan".getBytes());//单值过滤器2 匹配正则表达式ByteArrayComparable comparator = new RegexStringComparator("zhang.");new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);//单值过滤器2 匹配是否包含子串,大小写不敏感comparator = new SubstringComparator("wu");new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);//键值对元数据过滤-----family过滤----字节数组完整匹配        FamilyFilter ff = new FamilyFilter(                CompareOp.EQUAL ,                 new BinaryComparator(Bytes.toBytes("base_info"))   //表中不存在inf列族,过滤结果为空                );        //键值对元数据过滤-----family过滤----字节数组前缀匹配        ff = new FamilyFilter(                CompareOp.EQUAL ,                 new BinaryPrefixComparator(Bytes.toBytes("inf"))   //表中存在以inf打头的列族info,过滤结果为该列族所有行                );               //键值对元数据过滤-----qualifier过滤----字节数组完整匹配                filter = new QualifierFilter(                CompareOp.EQUAL ,                 new BinaryComparator(Bytes.toBytes("na"))   //表中不存在na列,过滤结果为空                );        filter = new QualifierFilter(                CompareOp.EQUAL ,                 new BinaryPrefixComparator(Bytes.toBytes("na"))   //表中存在以na打头的列name,过滤结果为所有行的该列数据        );        //基于列名(即Qualifier)前缀过滤数据的ColumnPrefixFilter        filter = new ColumnPrefixFilter("na".getBytes());                //基于列名(即Qualifier)多个前缀过滤数据的MultipleColumnPrefixFilter        byte[][] prefixes = new byte[][] {Bytes.toBytes("na"), Bytes.toBytes("me")};        filter = new MultipleColumnPrefixFilter(prefixes);         //为查询设置过滤条件        scan.setFilter(filter);                scan.addFamily(Bytes.toBytes("base_info"));ResultScanner scanner = table.getScanner(scan);for(Result r : scanner){/**for(KeyValue kv : r.list()){String family = new String(kv.getFamily());System.out.println(family);String qualifier = new String(kv.getQualifier());System.out.println(qualifier);System.out.println(new String(kv.getValue()));}*///直接从result中取到某个特定的valuebyte[] value = r.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));System.out.println(new String(value));}table.close();}}

0 0
原创粉丝点击