java hbase 基本操作

来源:互联网 发布:录像后期制作软件 编辑:程序博客网 时间:2024/06/08 04:43

hbase1.2.6
结构图

package com.xuteng;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.apache.commons.io.filefilter.PrefixFileFilter;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseClusterManager;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.Admin;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Delete;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.client.Table;import org.apache.hadoop.hbase.filter.BinaryComparator;import org.apache.hadoop.hbase.filter.ByteArrayComparable;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.RowFilter;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.hbase.util.HBaseConfTool;import org.junit.After;import org.junit.Before;import org.junit.Test;public class HbaseDao {    static String zkip="192.168.2.1:2181";//zookepper地址     Connection createConnection;     Admin admin ;     Table table2 ;    @Before    public  void initConnection() throws IOException{        Configuration conf = HBaseConfiguration.create();        conf.set("hbase.zookeeper.quorum",zkip);        createConnection = ConnectionFactory.createConnection(conf);         admin = createConnection.getAdmin();        TableName table= TableName.valueOf("t_buy_cart");         table2 = createConnection.getTable(table);    }    @After    public void closeConnection() throws IOException{        admin.close();    }    @Test    public void delete() throws IOException{        Delete delete=new Delete( Bytes.toBytes("user_000"));        delete.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num"));        table2.delete(delete);    }    @Test    public void update() throws IOException{        Put put=new Put(Bytes.toBytes("user_04"));//行键        put.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_id_2"), Bytes.toBytes("pd_002_008"));//插入一个商品id        table2.put(put);    }    @Test    public void get() throws IOException{        Get get=new Get(Bytes.toBytes("user_04"));        get.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_id_2"));        get.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num"));        get.addColumn(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"));        Result result = table2.get(get);        byte[] p1=result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_id_2"));        byte[] n2 = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_num"));        byte[] r3 = result.getValue(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"));        System.out.println(Bytes.toString(p1));        System.out.println(Bytes.toInt(n2));        System.out.println(Bytes.toString(r3));    }    @Test    public void scan() throws IOException{        Scan scan = new Scan(Bytes.toBytes("user_03"),Bytes.toBytes("user_05"));        ResultScanner scanner = table2.getScanner(scan);        Iterator<Result> iterator = scanner.iterator();        while(iterator.hasNext()){            Result result = iterator.next();            byte[] p1=result.getValue(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"));            byte[] p2 = result.getValue(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"));            System.out.println(Bytes.toString(p1));            System.out.println(Bytes.toString(p2));        }    }    /**     * 这个方法多个查询条件进行查询     * @throws IOException      */    @Test    public void filter() throws IOException{        Scan scan=new Scan(Bytes.toBytes("user_01"),Bytes.toBytes("user_05"));        scan.addFamily(Bytes.toBytes("product"));//指定scan扫描的列族        //实例一        Filter filter=new PrefixFilter(Bytes.toBytes("user"));//前缀过滤器满足指定前缀行        //实例二        ByteArrayComparable rowComparator=new BinaryComparator(Bytes.toBytes("user_01"));//行条件过滤器,按照指定行键查找数据        RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, rowComparator);        //实例三        SubstringComparator substringCompator = new SubstringComparator("_003_");        //列查找过滤器,模糊匹配指定数据的列,注意如果所查询数据的列不是指定的列,该数据也会返回,同样的列族也是这个道理        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("product"), Bytes.toBytes("product_id"), CompareOp.EQUAL,substringCompator);        //将不符合列条件的数据也进行过滤        singleColumnValueFilter.setFilterIfMissing(true);        scan.setFilter(singleColumnValueFilter);        ResultScanner scanner = table2.getScanner(scan);            Iterator<Result> iterator = scanner.iterator();        while(iterator.hasNext()){            System.out.println(iterator.next());        }    }    @Test    public void putList() throws IOException{        List<Put> pusList=new ArrayList<Put>();        Put put=new Put(Bytes.toBytes("user_04"));//行键        put.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_id_2"), Bytes.toBytes("pd_002_007"));//插入一个商品id        put.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num"), Bytes.toBytes(4));//插入一个商品数量        put.addColumn(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"), Bytes.toBytes("pd_002_008"));//插入一个推荐商品数量        Put put2=new Put(Bytes.toBytes("user_05"));//行键        put2.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_id_3"), Bytes.toBytes("pd_003_007"));//插入一个商品id        put2.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num"), Bytes.toBytes(5));//插入一个商品数量        put2.addColumn(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"), Bytes.toBytes("pd_003_008"));//插入一个推荐商品数量        pusList.add(put);        pusList.add(put2);        table2.put(pusList);    }    @Test    public void put() throws IOException{        Put put=new Put(Bytes.toBytes("user_00"));//行键        put.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_id"), Bytes.toBytes("pd_001_007"));//插入一个商品id        put.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num"), Bytes.toBytes(5));//插入一个商品数量        put.addColumn(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"), Bytes.toBytes("pd_001_008"));//插入一个推荐商品数量        table2.put(put);     }    public static void main(String[] args) throws IOException {        Configuration conf = HBaseConfiguration.create();        conf.set("hbase.zookeeper.quorum",                zkip);        Connection createConnection = ConnectionFactory.createConnection(conf);        Admin admin = createConnection.getAdmin();/*      admin.createNamespace(NamespaceDescriptor.create("habase_java").build());*/      TableName a=TableName.valueOf("t_buy_cart");        HTableDescriptor hTableDescriptor =new HTableDescriptor(a);        HColumnDescriptor family=new HColumnDescriptor("product");        family.setMaxVersions(3);        hTableDescriptor.addFamily(family);        HColumnDescriptor recommend=new HColumnDescriptor("recommend");        recommend.setMaxVersions(3);        hTableDescriptor.addFamily(recommend);        admin.createTable(hTableDescriptor);        admin.close();    }}
原创粉丝点击