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编写. 全部代码均基于& ...
随机推荐
- vue学习笔记——组件的优化
Vue 应用性能优化指南 1 给组件定义name,然后在同级目录新建index文件: import Count from './count.vue' export Count; 2 优化大数据的列表 ...
- 使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development、production)
使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development.production) --mode 指定环境模式 ...
- python的set处理二维数组转一维数组
for splitValue in set(dataset[:, featureIndex].tolist()): 首先set是一个无序,无重复的数据结构,所以很多时候使用它来进行去重:但是set接收 ...
- spring boot通过Interceptor和HandlerMethodReturnValueHandler实现统一处理为controller返回对象统计处理时间
思路:实现思路都是基于Aop实现,方式上可以通过spring aop和spring mvc的aop机制都能实现. 通过Interceptor的可以实现为controller插入开始时间和执行结束时间, ...
- Struts1 不能进入Action,页面空白问题解决
http://blog.csdn.net/barry_liao/article/details/35592935 Struts1 不能进入Action,页面空白问题解决 标签: Struts1Acti ...
- java代码------charAt()的用法
总结:你看这个方法的用处真的蛮多比如这个计算器小项目,用这个charAt()方法来装运算符 package com.mmm; import java.util.Scanner; public clas ...
- 6.8 出口条件循环:do while
while循环和for循环都是入口条件循环,即在循环的每次迭代之前检查测试条件,所以有可能根本不执行循环体中的内容.C语言还有出口条件循环(exit-condition loop),即在循环的每次迭代 ...
- Scrapy学习篇(十二)之设置随机IP代理(IPProxy)
当我们需要大量的爬取网站信息时,除了切换User-Agent之外,另外一个重要的方式就是设置IP代理,以防止我们的爬虫被拒绝,下面我们就来演示scrapy如何设置随机IPProxy. 设置随机IPPr ...
- 涂抹mysql笔记-数据导出导入
数据导出导入<>利用CSV存储引擎加载数据:CSV存储引擎基于CSV格式文件存储数据,CSV格式是纯文本格式的文件,以逗号分隔取值.CSV引擎表的所有列值不能为空.Excel可以直接打开有 ...
- Java笔试面试题整理第六波(修正版)
转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...