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(); }}
阅读全文
0 0
- Hbase Java 基本操作
- java hbase 基本操作
- Hbase Java Api 基本操作
- Hbase Java编程基本操作
- java对HBase的基本操作
- JAVA操作Hbase管理类HbaseManage基本详细操作
- JAVA操作Hbase管理类HbaseManage基本详细操作
- HBase 基本操作
- hbase基本操作
- Hbase shell基本操作
- Hbase基本操作
- hbase基本操作
- hbase基本操作2
- HBase的基本操作
- Hbase的基本操作
- HBase命令行基本操作
- HBase基本操作
- Hbase 基本操作
- Android之进程、线程、进程间通信的相关概念理解
- PSX 610G 使用说明书
- 转载:Amazon Aurora: Design Considerations for High Throughput Cloud-Native Relational Databases
- @RestController和@Controller的区别
- < 笔记 > CSS
- java hbase 基本操作
- python Beautiful Soup的用法
- eventbus 初步使用
- Coursera 深度学习 deep learning.ai 吴恩达 第三课 结构化机器学习项目 第一周 测试题目 Bird recognition in the city of Peacetopia
- 基于SSH协议的端口转发
- 每周一本书之《驾驭大数据》:如何实现大数据的应用性
- 智能合约不智能
- 2017杭州·云栖大会召开在即,世界都在聆听的声音,数据猿将带来全景式记录
- 荣之联发布大数据平台DataZoo,阿里巴巴将在欧洲建第二个数据中心 | 大数据24小时