hbase--知识点总结2
--用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的更多相关文章
- hbase 知识点
hbase 教程:http://www.yiibai.com/hbase/ mac下hbase安装:https://www.jianshu.com/p/510e1d599123 HBase是建立在Ha ...
- 大白话详解大数据HBase核心知识点,老刘真的很用心(2)
前言:老刘目前为明年校招而努力,写文章主要是想用大白话把自己复习的大数据知识点详细解释出来,拒绝资料上的生搬硬套,做到有自己的理解! 01 HBase知识点 第6点:HRegionServer架构 为 ...
- 大白话详解大数据HBase核心知识点,老刘真的很用心(3)
老刘目前为明年校招而努力,写文章主要是想用大白话把自己复习的大数据知识点详细解释出来,拒绝资料上的生搬硬套,做到有自己的理解! 01 HBase知识点(3) 第13点:HBase表的热点问题 什么是热 ...
- 用大白话讲大数据HBase,老刘真的很用心(1)
老刘今天复习HBase知识发现很多资料都没有把概念说清楚,有很多专业名词一笔带过没有解释.比如这个框架高性能.高可用,那什么是高性能高可用?怎么实现的高性能高可用?没说! 如果面试官听了你说的,会有什 ...
- Hbase--知识点总结3
Hbase知识点总结: hbase表中为什么列族的数量不能太多? 因为当一个列族数据溢写的时候,其他列族也会发生数据溢写,但是其他列族中数据的数量还没有达到溢写的阈值,就会导致产生的小文件数量增多. ...
- 大数据核心知识点:Hbase、Spark、Hive、MapReduce概念理解,特点及机制
今天,上海尚学堂大数据培训班毕业的一位学生去参加易普软件公司面试,应聘的职位是大数据开发.面试官问了他10个问题,主要集中在Hbase.Spark.Hive和MapReduce上,基础概念.特点.应用 ...
- HBase核心知识点总结
一.HBase介绍 1.基本概念 HBase是一种Hadoop数据库,经常被描述为一种稀疏的,分布式的,持久化的,多维有序映射,它基于行键.列键和时间戳建立索引,是一个可以随机访问的存储和检索数据的平 ...
- 一文让您全面了解清楚HBase数据库的所有知识点,值得收藏!
一.HBase基本概念:列式数据库 在Hadoop生态体系结构中,HBase位于HDFS(Hadoop分布式文件系统)的上一层,不依赖于MapReduce,那么如果没有HBase这种Nosql数据库会 ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- 【甘道夫】HBase基本数据操作的详细说明【完整版,精绝】
介绍 之前具体写了一篇HBase过滤器的文章.今天把基础的表和数据相关操作补上. 本文档參考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 全部代码均基于& ...
随机推荐
- vs2017 编译vue 错误 TS2307 Build:找不到模块“webpack”。
升级了vs2017之后,编译之前正常的工程,发现出现了对vue的编译错误, 提示一些列找不到模块的错误 错误 TS2307 Build:找不到模块“webpack”. ...... 错误 TS2345 ...
- nodeJS网络操作
var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, ...
- xshell连接linux主机时,出现错误:Could not connect to '***.***.***.***' (port 22)
xshell连接linux主机时,会出现错误:Could not connect to '192.168.89.144' (port 22): Connection failed. 但是这时能ping ...
- python------面向对象进阶反射详解(重点)
一.反射 通过字符串映射或者修改程序运行时的状态,属性,或者方法. 1.getattr(object,name,default=None) 2.hasattr(object,name) 3.setat ...
- 【python接口自动化框架-unittest】如何传参数到下一个case
1.前提 平时我们用unittest的时候,都知道每个test_ 都是相互独立的,但是很多现实情况是,我们下一个接口参数,可能会用到上一个接口返回的json字段,那么,我们怎么去实现呢 2.实例 1. ...
- PythonStudy——三种字符串 Three strings
# 普通字符串:u'以字符作为输出单位'print(u'abc') # 用于显示 # 二进制字符串:b'' 二进制字符串以字节作为输出单位print(b'abc') # 用于传输 # 原义字符串:r' ...
- visual studio2017----编码和行尾
在 Visual Studio 中,以下字符将解释为换行符: CR LF:回车符 + 换行符,Unicode 字符 000D + 000A LF:换行符,Unicode 字符 000A NEL:下一行 ...
- laravel 路由分組
laravel 路由分組 Route::group(['prefix' => 'admin'], function () { $namespacePrefix="\\App\\Http ...
- Guava 12:Guava EventBus源码剖析
一.架构速读 传统上,Java的进程内事件分发都是通过发布者和订阅者之间的显式注册实现的.设计EventBus就是为了取代这种显示注册方式,使组件间有了更好的解耦.EventBus不是通用型的发布-订 ...
- Zuul 跨域
JS访问会出现跨域问题的解决, 一.对单个接口,处理跨域,只需要在被调用的类或或方法增加注解 CoossOrigin 如下设置 allowCredenticals=true,表示运行Cookie跨域 ...