天下事有难易乎?为之,则难者亦易矣;不为,则易者亦难矣。

Hbase 常用API操作

itzoo 3433次浏览 0个评论

Hbase常用API操作,主要包括表的创建,数据的添加,获取,预分区的使用和Filter使用。

环境: Hadoop 2.5, Hbase版本0.98

代码

 package net.itzoo;


import java.io.IOException;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Hbase {
 
 private HBaseAdmin admin;
 private HTable table;
 private String name = "calllist";
 
 private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
 
 @Before
 public void setUp() {
 Configuration conf = new Configuration();
 conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
 try {
 admin = new HBaseAdmin(conf);
 table = new HTable(conf, name);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 
 @After
 public void end() {
 try {
 admin.close();
 table.close();
 } catch (IOException e) {
 e.printStackTrace();
 }finally{
// try {
// table.flushCommits();
// } catch (Exception e) {
// e.printStackTrace();
// }
 }
 }

 //创建表
 @Test
 public void createTbl() throws Exception {
 if(admin.tableExists(name)) {
 
 admin.disableTable(name);
 admin.deleteTable(name);
 }
 //指定表的命名空间
// admin.createNamespace(NamespaceDescriptor.create("myHbase").build());
 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name));
// desc.setDurability(Durability.SYNC_WAL);
 
 HColumnDescriptor family = new HColumnDescriptor("cf1");
 family.setInMemory(true);
 family.setMaxVersions(1);
 family.setInMemory(true);
 
 desc.addFamily(family);
 
 admin.createTable(desc);
 //创建预分区表
// admin.createTable(desc, arg1);
 }
 
 //添加数据
 @Test
 public void putData() throws Exception {
 String rowkey = "18336352609_45689784";
 Put put = new Put(rowkey.getBytes());
 put.add("cf1".getBytes(), "phone".getBytes(), "13164395330".getBytes());
 put.add("cf1".getBytes(), "date".getBytes(), "564398456".getBytes());
 put.add("cf1".getBytes(), "type".getBytes(), "1".getBytes());
 
 table.put(put);
 }
 
 //获取1条数据
 @Test
 public void getData() throws Exception {
 String rowkey = "18336352609_45689784";
 Get get = new Get(rowkey.getBytes());
 get.addColumn("cf1".getBytes(), "phone".getBytes());
 get.addColumn("cf1".getBytes(), "type".getBytes());
 Result result = table.get(get);
 Cell cell = result.getColumnLatestCell("cf1".getBytes(), "phone".getBytes());
 Cell cell2 = result.getColumnLatestCell("cf1".getBytes(), "type".getBytes());
 String str = new String(CellUtil.cloneValue(cell));
 String str2 = new String(CellUtil.cloneValue(cell2));
 System.out.println(str + str2);
 }
 
 //获取数据
 @Test
 public void scanData() throws Exception {
 Scan scan = new Scan();

 String startRow = "13129566180_" + (Long.MAX_VALUE-sdf.parse("2017010100000000").getTime());
 String stopRow = "13129566180_" + (Long.MAX_VALUE-sdf.parse("2016010100000000").getTime());
 
 scan.setStartRow(startRow.getBytes());
 scan.setStopRow(stopRow.getBytes());
 
 ResultScanner rs = table.getScanner(scan);
 for(Result r : rs) {
 System.out.println(new String (CellUtil.cloneValue(r.getColumnLatestCell("cf1".getBytes(), "phone".getBytes()))));
 System.out.println(new String (CellUtil.cloneValue(r.getColumnLatestCell("cf1".getBytes(), "type".getBytes()))));
 }
 }
 
 //添加数据
 @Test
 public void insertDates() throws Exception {
 List<Put> puts = new ArrayList<Put>();
 
 for(int i = 0; i < 10; i++) {
 String phone = getPhone("131");
 
 for(int j = 0; j < 100; j++) {

 Date date = sdf.parse(getDateTime());
 String rowkey = phone +"_"+(Long.MAX_VALUE-date.getTime());

 System.out.println("rowkey:" + rowkey);
 
 Put put = new Put(rowkey.getBytes());
 put.add("cf1".getBytes(), "phone".getBytes(), getPhone("183").getBytes());
 put.add("cf1".getBytes(), "type".getBytes(), (r.nextInt(2)+"").getBytes());
 
 puts.add(put);
 }
 }
 table.put(puts);
 }
 
 /**
 * 预分区
 * @param startKey
 * @param endKey
 * @param numRegions
 * @return
 */
 public static byte[][] getHexSplits(String startKey, String endKey,int numRegions) {
 
 byte[][] splits =new byte[numRegions-1][];
 
 BigInteger lowestKey =new BigInteger(startKey,16);
 BigInteger highestKey =new BigInteger(endKey,16);
 BigInteger range = highestKey.subtract(lowestKey);
 BigInteger regionIncrement = range.divide(BigInteger.valueOf(numRegions));
 lowestKey = lowestKey.add(regionIncrement);
 for(int i=0; i < numRegions-1;i++) {
 BigInteger key = lowestKey.add(regionIncrement.multiply(BigInteger.valueOf(i)));
 byte[] b = String.format("%016x", key).getBytes();
 splits[i] = b;
 }
 return splits;
 }
 
 
 //筛选出指定数据
 @Test
 public void filter() throws Exception {
 
 FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
 
 PrefixFilter preFilter = new PrefixFilter("13195453889_".getBytes());
 list.addFilter(preFilter);
 
 SingleColumnValueFilter valFilter = new SingleColumnValueFilter(
 "cf1".getBytes(), 
 "type".getBytes(), 
 CompareOp.EQUAL, 
 "1".getBytes());
 list.addFilter(valFilter);
 
 Scan scan = new Scan();
 scan.setFilter(list);
 
 ResultScanner res = table.getScanner(scan);
 
 for(Result r : res) {
 System.out.println(new String(CellUtil.cloneValue(r.getColumnLatestCell("cf1".getBytes(), "phone".getBytes())))
 +"-" +new String(CellUtil.cloneValue(r.getColumnLatestCell("cf1".getBytes(), "type".getBytes()))));
 }
 
 }
 
 
 
 
 Random r = new Random();
 
 
 public String getPhone(String perfix) {
 
 return perfix + String.format("%08d", r.nextInt(99999999));
 }
 
 public String getDateTime() {
 String month = String.format("%02d", r.nextInt(12) + 1);
 String day = String.format("%02d", r.nextInt(30) + 1);
 String hour = String.format("%02d", r.nextInt(24));
 String mon = String.format("%02d", r.nextInt(60));
 String s = String.format("%02d", r.nextInt(60));
 
 return 2016+month+day+hour+mon+s;
 }
 
 public static void main(String[] args) {
 
 Hbase h = new Hbase();
 System.out.println(h.getPhone("131"));
 }
}

ITZOO版权所有丨如未注明 , 均为原创丨转载请注明来自IT乐园 ->Hbase 常用API操作
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址