--用java操作hbase

1、配置jar包环境

创建hbase项目 --> 新建folder文件夹 --> 将hbase相关jar包全部导入到java项目之中 --> add buildpath -->导入hbase conf文件夹下面的配置文件 (配置hbase环境时修改过的所有配置文件)-->

将配置文件放到hbase的src目录下面 (目的:让java找到hbase)-->导入hadoop相关jar包

2、查看hbase方法api的方法:在hbase源码安装包中的docs文件夹下apidocs

3、hbase基本操作源码

package com.wcg.Hbase;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceAudience.Public;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.RetriesExhaustedWithDetailsException;
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.apache.hadoop.hbase.generated.master.table_jsp;
import org.apache.hadoop.hbase.thrift.generated.Hbase.Processor.get;
import org.apache.hadoop.hbase.thrift2.generated.TMutation;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.GetDataEncryptionKeyRequestProto;
import org.apache.hadoop.io.Stringifier;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.exceptions.verification.NeverWantedButInvoked; import com.google.common.collect.Table;
import com.wcg.Hbase.Phone.PhoneDetail;
import com.wcg.Hbase.Phone.dayPhoneDetail; public class HbaseDemo { Configuration conf = null;
HBaseAdmin admin = null;
private String TM = "phone";
HTable table = null; @Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
//首先进行初始化
conf = new Configuration();
//连接zookeeper
conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
admin = new HBaseAdmin(conf);
table = new HTable(conf, TM); } @Test
public void createTable() throws IOException{
//在创建表之前先进行判断该表是否已经存在
if(admin.tableExists(TM)){
admin.disableTable(TM);
admin.deleteTable(TM); } //创建一个表描述的类
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TM));
HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
desc.addFamily(family);
admin.createTable(desc); } //向表中插入数据
@Test
public void insertDB() throws RetriesExhaustedWithDetailsException, InterruptedIOException{
String rowkey = "111111"; Put put = new Put(rowkey.getBytes()); put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes()); table.put(put); } /**
* 模拟手机通话记录
* 一共有10个用户,每个用户产生了1000条数据
* @throws ParseException
* @throws InterruptedIOException
* @throws RetriesExhaustedWithDetailsException
* @throws IOException
*/
@Test
public void insertDB2() throws ParseException, RetriesExhaustedWithDetailsException, InterruptedIOException{
List<Put> list = new ArrayList<Put>();
for(int i=0;i<5;i++){
String phoneNum = getPhone("158");
for(int j= 0; j<1000;j++){
//产生一些其他类型的数据
String dnum = getPhone("182");
String length = r.nextInt(99)+"";
String type = r.nextInt(2)+"";
String date = getDate("2019");
//随机产生一个rowkey
String rowkey = phoneNum+"_"+(Long.MAX_VALUE-sdf.parse(date).getTime());
Put put = new Put(rowkey.getBytes());
put.add("cf".getBytes(), "dnum".getBytes(), dnum.getBytes());
put.add("cf".getBytes(), "length".getBytes(), length.getBytes());
put.add("cf".getBytes(), "type".getBytes(), type.getBytes());
put.add("cf".getBytes(), "date".getBytes(), date.getBytes());
list.add(put); }
}
table.put(list);
}
/*
* 使用protobuf进行数据插入
*
*/
@Test
public void insertDB3() throws ParseException, RetriesExhaustedWithDetailsException, InterruptedIOException{
List<Put> list = new ArrayList<Put>();
for(int i=0;i<5;i++){
String phoneNum = getPhone("158");
for(int j= 0; j<1000;j++){
//产生一些其他类型的数据
String dnum = getPhone("182");
String length = r.nextInt(99)+"";
String type = r.nextInt(2)+"";
String date = getDate("2019");
//随机产生一个rowkey
String rowkey = phoneNum+"_"+(Long.MAX_VALUE-sdf.parse(date).getTime());
Phone.PhoneDetail.Builder phoneDetail = Phone.PhoneDetail.newBuilder();
phoneDetail.setDate(date);
phoneDetail.setDnum(dnum);
phoneDetail.setLength(length);
phoneDetail.setType(type);
Put put = new Put(rowkey.getBytes());
put.add("cf".getBytes(), "phoneDetail".getBytes(), phoneDetail.build().toByteArray());
//注:不能写成phoneDetail.toString().getBytes(),这样会将只想对象的地址转化为字节数组 list.add(put); }
}
table.put(list);
}
/*
* 以用户作为rowkey进行数据压缩
*
*
*/
@Test
public void insertDB4() throws ParseException, RetriesExhaustedWithDetailsException, InterruptedIOException{
List<Put> puts = new ArrayList<Put>();
for(int i =0;i<10;i++){
String phoneNum = getPhone("158");
String rowkey = phoneNum+"_"+(Long.MAX_VALUE-sdf.parse("20190101000000").getTime());
Phone.dayPhoneDetail.Builder dayPhone = Phone.dayPhoneDetail.newBuilder();
for (int j = 0 ;j<1000;j++){
//产生一些其他类型的数据
String dnum = getPhone("182");
String length = r.nextInt(99)+"";
String type = r.nextInt(2)+"";
String date = getDate2("20190101");
//随机产生一个rowkey
Phone.PhoneDetail.Builder phoneDetail = Phone.PhoneDetail.newBuilder();
phoneDetail.setDate(date);
phoneDetail.setDnum(dnum);
phoneDetail.setLength(length);
phoneDetail.setType(type);
dayPhone.addDayofPhone(phoneDetail);
}
Put put = new Put(rowkey.getBytes());
put.add("cf".getBytes(), "day".getBytes(),dayPhone.build().toByteArray());
puts.add(put);
}
table.put(puts);
} private String getDate2(String string) {
return string+String.format("%02d%02d%02d",r.nextInt(24),r.nextInt(60),r.nextInt(60));
} /*
* 通过get方法获取一条用protobuf存储的数据
*
*/
@Test
public void get2() throws IOException{
Get get = new Get("15865543021_9223370490642209807".getBytes());
Result re = table.get(get);
Cell cell = re.getColumnLatestCell("cf".getBytes(), "phoneDetail".getBytes());
//将获取到的Cell字节数组转化为一个对象
Phone.PhoneDetail phoneDetail1 = Phone.PhoneDetail.parseFrom(CellUtil.cloneValue(cell));
System.out.println(phoneDetail1); } /*
* 用get方法获取之前插入的数据
*
*
*/
@Test
public void get3() throws IOException{
int count = 0;
Get get = new Get("15897845910_9223370490582775807".getBytes());
Result re = table.get(get);
Cell cell = re.getColumnLatestCell("cf".getBytes(), "day".getBytes());
//将获取到的Cell字节数组转化为一个对象
Phone.dayPhoneDetail dayPhone = Phone.dayPhoneDetail.parseFrom(CellUtil.cloneValue(cell));
for(PhoneDetail pd: dayPhone.getDayofPhoneList()){ System.out.println(pd);
count++;
} System.out.println(count);
} /*
* 查询某一个用户2月份的所有通话记录
*
*/
@Test
public void scan2() throws ParseException, IOException{
String phoneNum = "15890601889";
String startRow = phoneNum+"_"+(Long.MAX_VALUE-sdf.parse("20190301000000").getTime());
String stopRow = phoneNum+"_"+(Long.MAX_VALUE-sdf.parse("20190201000000").getTime());
Scan scan= new Scan();
scan.setStartRow(startRow.getBytes());
scan.setStopRow(stopRow.getBytes());
//从table对象中获取scan对象
ResultScanner rss = table.getScanner(scan);
for(Result rs : rss){
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes())))));
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "length".getBytes())))));
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "type".getBytes())))));
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "date".getBytes())))));
} } /*
* 查询主叫用户的数据
* 用过滤器对主叫用户的数据进行过滤
*
*/
@Test
public void filter() throws IOException{
String phoneNum = "15890601889";
FilterList lists = new FilterList();
SingleColumnValueFilter filter1 = new SingleColumnValueFilter("cf".getBytes(), "type".getBytes(), CompareOp.EQUAL, "1".getBytes());
PrefixFilter filter2 = new PrefixFilter(phoneNum.getBytes());
lists.addFilter(filter1);
lists.addFilter(filter2);
Scan scan = new Scan();
scan.setFilter(lists);
ResultScanner rss = table.getScanner(scan);
for(Result rs : rss){
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "dnum".getBytes())))));
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "length".getBytes())))));
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "type".getBytes())))));
System.out.println(new String(CellUtil.cloneValue((rs.getColumnLatestCell("cf".getBytes(), "date".getBytes())))));
} } private String getDate(String string) { return string+string.format("%02d%02d%02d%02d%02d", r.nextInt(12)+1,r.nextInt(31),r.nextInt(24),r.nextInt(60),r.nextInt(60));
} Random r = new Random();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
private String getPhone(String string){
//产生一个8位的随机整数,当数据不足8位的时候,在前面用0补齐
return string+String.format("%08d", r.nextInt(99999999)); } //用get的方式获取数据
@Test
public void get() throws IOException{
String rowkey = "111111"; Get get = new Get(rowkey.getBytes());
Result rs = table.get(get);
//Bytes.toString((rs.getValue("cf".getBytes(), "name".getBytes())));
System.out.println("+++++++++++++++数据开始得到+++++++++++++++");
Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
//System.out.println(org.apache.hadoop.hbase.util.Bytes.toString(cell.getValue()));
String mm = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(mm);
System.out.println("++++++++++++++++数据已经得到+++++++++++++++++++++"); } @After
public void destroy() throws IOException{
if(admin!=null){
admin.close();
} if(table!=null){
table.close(); }
} }

hbase--知识点总结2的更多相关文章

  1. hbase 知识点

    hbase 教程:http://www.yiibai.com/hbase/ mac下hbase安装:https://www.jianshu.com/p/510e1d599123 HBase是建立在Ha ...

  2. 大白话详解大数据HBase核心知识点,老刘真的很用心(2)

    前言:老刘目前为明年校招而努力,写文章主要是想用大白话把自己复习的大数据知识点详细解释出来,拒绝资料上的生搬硬套,做到有自己的理解! 01 HBase知识点 第6点:HRegionServer架构 为 ...

  3. 大白话详解大数据HBase核心知识点,老刘真的很用心(3)

    老刘目前为明年校招而努力,写文章主要是想用大白话把自己复习的大数据知识点详细解释出来,拒绝资料上的生搬硬套,做到有自己的理解! 01 HBase知识点(3) 第13点:HBase表的热点问题 什么是热 ...

  4. 用大白话讲大数据HBase,老刘真的很用心(1)

    老刘今天复习HBase知识发现很多资料都没有把概念说清楚,有很多专业名词一笔带过没有解释.比如这个框架高性能.高可用,那什么是高性能高可用?怎么实现的高性能高可用?没说! 如果面试官听了你说的,会有什 ...

  5. Hbase--知识点总结3

    Hbase知识点总结:  hbase表中为什么列族的数量不能太多? 因为当一个列族数据溢写的时候,其他列族也会发生数据溢写,但是其他列族中数据的数量还没有达到溢写的阈值,就会导致产生的小文件数量增多. ...

  6. 大数据核心知识点:Hbase、Spark、Hive、MapReduce概念理解,特点及机制

    今天,上海尚学堂大数据培训班毕业的一位学生去参加易普软件公司面试,应聘的职位是大数据开发.面试官问了他10个问题,主要集中在Hbase.Spark.Hive和MapReduce上,基础概念.特点.应用 ...

  7. HBase核心知识点总结

    一.HBase介绍 1.基本概念 HBase是一种Hadoop数据库,经常被描述为一种稀疏的,分布式的,持久化的,多维有序映射,它基于行键.列键和时间戳建立索引,是一个可以随机访问的存储和检索数据的平 ...

  8. 一文让您全面了解清楚HBase数据库的所有知识点,值得收藏!

    一.HBase基本概念:列式数据库 在Hadoop生态体系结构中,HBase位于HDFS(Hadoop分布式文件系统)的上一层,不依赖于MapReduce,那么如果没有HBase这种Nosql数据库会 ...

  9. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

  10. 【甘道夫】HBase基本数据操作的详细说明【完整版,精绝】

    介绍 之前具体写了一篇HBase过滤器的文章.今天把基础的表和数据相关操作补上. 本文档參考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 全部代码均基于& ...

随机推荐

  1. Arcmap连接数据库需管理员获取许可——创建ArcSDE连接文件

    一.在装有server的服务器上创建ArcSDE连接文件 1.打开ArcMap<<ArcToolBox<<数据管理工具<<工作空间<<创建ArcSDE连 ...

  2. Linux监控平台、安装zabbix、修改zabbix的admin密码

    1.Linux监控平台 2. zabbix监控 3.zabbix的安装下载:wget -c https://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbi ...

  3. 《DSP using MATLAB》Problem 7.9

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  4. 学习笔记TF042:TF.Learn、分布式Estimator、深度学习Estimator

    TF.Learn,TensorFlow重要模块,各种类型深度学习及流行机器学习算法.TensorFlow官方Scikit Flow项目迁移,谷歌员工Illia Polosukhin.唐源发起.Scik ...

  5. Redis集群配置(linux)

     *弄了一天,有问题直接问我.qq:137416943   1.redis集群的配置和简单使用   Redis集群配置 0.首先要配置环境: 0.1 安装c++ yum install gcc-c++ ...

  6. redis的内存优化【转】

    Redis所有的数据都在内存中,而内存又是非常宝贵的资源.对于如何优化内存使用一直是Redis用户非常关注的问题.本文让我们深入到Redis细节中,学习内存优化的技巧.分为如下几个部分: 一.redi ...

  7. Linux内核分析第九次作业

    理解进程调度时机跟踪分析进程调度与进程切换的过程 一.基础知识 Linux系统的一般执行过程 一般情况:正在运行的用户态进程X切换到运行用户态进程Y的过程 1. 正在运行的用户态进程X 2. 发生中断 ...

  8. 创建一个HTTP接口测试

    jmeter Apache JMeter是Apache组织开发的基于Java的压力测试工具. Apache jmeter 可以用于对静态的和动态的资源(文件,Servlet,Perl脚本,java 对 ...

  9. public class PageRender implements ResponseRender

    package cn.ubibi.jettyboot.demotest.controller.render; import cn.ubibi.jettyboot.framework.commons.S ...

  10. 2th Dec 2018

    北京的冬天越来越冷了,是那种钻进骨头里的冷.果,爸爸又走了.每次离开都格外的难受,这种感觉是加剧的,一次比一次强烈.走的时候,你一脸的不高兴,能感觉出来你的不开心,你勉强让爷爷从我怀里面接过去.3个半 ...