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编写. 全部代码均基于& ...
随机推荐
- 内网服务器设置NAT123端口映射,方便外网连接;如何测试端口连通情况。
一.nat123设置端口映射. 1)首先去nat123官网注册账号. http://www.nat123.com/ 2)下载nat123客户端 http://www.nat123.com/Pages_ ...
- django 多对多 增 删 改 查
一.通过url方式实现多对多的:增加,删除,编辑 代码目录: urls.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
- tgp助手开启逆战游戏无反应
tgp助手开启逆战游戏无反应(一直显示正在运行游戏)就是没有游戏的登录界面 解决的一些方法(不一定有效): 检查显卡的驱动 检查游戏文件是否损坏 检查是否开启的防护软件程序
- Google - chanceToLose24Game
/* 一个类似24点的游戏,假设牌桌上有无数张1-10的牌,然后你手上的牌的总和是k,现在你可以随机到牌桌上抽牌加到总和里,如果你手上牌的总和在20-25之间就是win,如果总和超过25就是lose, ...
- selenium 网络请求
selenium 网络请求 browser.find_element_by_id("id的name")browser.find_element("")brows ...
- 使用mysqlproxy实现mysql读写分离
先说一下什么是读写分离吧. 以三台虚拟机为例,搭建一主一从一代理,全部配置好之后,当从proxy插入数据时,该数据会同时插入主数据库,因为主从关系,从数据库也会有数据.当把从数据库执行slave st ...
- pxe+Kickstart自动装机补充知识点
1.vmlinuzvmlinuz是可引导的.压缩的内核.“vm”代表“Virtual Memory”.Linux 支持虚拟内存,不像老的操作系统比如DOS有640KB内存的限制.Linux能够使用硬盘 ...
- 服务容错和Hystrix
一.雪崩效应 在微服务架构中,通常有多个服务层调用,如果某个服务不可用,造成调用的服务也不可用,造成整个系统不可用的情况,叫做雪崩效应 二.Hystrix 放雪崩利器Hystrix,基于Netflix ...
- c#中枚举类型 显示中文
public enum AuditEnum { [Description("未送审")] Holding=0, [Description("审核中")] Aud ...
- Docker Images for MySQL Group Replication 5.7.14
In this post, I will point you to Docker images for MySQL Group Replication testing. There is a new ...