hbase 实战项目
首先 根据 hadoop 搭建 + hbase 搭建把 环境弄好
由于 hbase 依赖于 hdfs ,所以 需要 进入 hadoop --》sbin 下 启动 start-dfs.sh , start-yarn.sh
然后 进 hbase --> 下 启动 start-hbase.sh 如果 后面 运行失败 报 找不到 zookeeper 八成 你需要进 hbase-bin-> stop-hbase 然后 重run start-hbase.sh
这里列举下 hbase shell 的常用操作
#最有用命令 help
help 'status'
#建表
create 'FileTable','fileInfo','saveInfo'
#列出有哪些表
list
#描述表信息
desc 'FileTable'
#统计 表
count 'FileTable'
#添加列簇
alter 'FileTable','cf'
# 删除列簇 ,一定要注意大小写
alter 'FileTable',{NAME=>'cf',METHOD=>'delete'}
#插入数据
put 'FileTable','rowkey1','fileInfo:name','file1.txt'
0 row(s) in 3.9840 seconds
put 'FileTable','rowkey1','fileInfo:type','txt'
0 row(s) in 0.0410 seconds
put 'FileTable','rowkey1','fileInfo:size','1024'
0 row(s) in 0.1100 seconds
put 'FileTable','rowkey1','saveInfo:path','/home/pics'
0 row(s) in 0.1320 seconds
put 'FileTable' , 'rowkey1','saveInfo:creator','tom'
0 row(s) in 0.1430 seconds
#查询表总行数
hbase(main):016:0> count 'FileTable'
1 row(s) in 0.8500 seconds
# get 查询
get 'FileTable','rowkey1'
get 'FileTable','rowkey1','fileInfo'
#delete
#deleteall
#scan
#删除表之前必须先禁用表Drop the named table. Table must first be disabled:
disable 'FileTable'
is_enabled
is_disabled
drop 'FileTable'
查询所有列簇
查询指定列簇
HBase 连接类
package com.ghc.hbase.api;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
public class HBaseConn {
private static final HBaseConn INSTANCE = new HBaseConn();
private static Configuration configuration;
private static Connection connection;
private HBaseConn(){
try{
if(configuration == null){
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","192.168.32.129:2181");
}
}catch(Exception e){
e.printStackTrace();
}
}
private Connection getConnection(){
if(connection == null || connection.isClosed()){
try{
connection = ConnectionFactory.createConnection(configuration);
}catch(IOException e){
e.printStackTrace();
}
}
return connection;
}
public static Connection getHBaseConnection(){
return INSTANCE.getConnection();
}
public static Table getTable(String tableName) throws IOException{
return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
}
public static void closeConn(){
if(connection != null){
try{
connection.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
junit 测试一波连接类
import com.ghc.hbase.api.HBaseConn;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.junit.Test;
import java.io.IOException;
public class HBaseConnTest {
@Test
public void getConnTest(){
Connection conn = HBaseConn.getHBaseConnection();
System.out.println(conn.isClosed());
HBaseConn.closeConn();
System.out.println(conn.isClosed());
}
@Test
public void getTableTest(){
Table table = null;
try{table = HBaseConn.getTable("FileTable");
System.out.println(table.getName());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
hbase 增删操作类
package com.ghc.hbase.api;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class HBaseUtil {
public static Boolean createTable(String tableName,String[] cfs){
try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
if(admin.tableExists(tableName)) {
return false;
}
// 不存在 则创建
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
Arrays.stream(cfs).forEach(cf->{
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hColumnDescriptor.setMaxVersions(1);
hTableDescriptor.addFamily(hColumnDescriptor);
});
admin.createTable(hTableDescriptor);
}catch(Exception e){
e.printStackTrace();
}
return true;
}
public static Boolean putRow(String tableName, String rowKey, String cfName,String qualifier,String data){
try(Table table = HBaseConn.getTable(tableName)){
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
table.put(put);
}catch (IOException ioe){
ioe.printStackTrace();
}
return true;
}
public static Boolean putRows(String tableName,List<Put> puts){
try(Table table = HBaseConn.getTable(tableName)){
table.put(puts);
}catch(IOException ioe){
ioe.printStackTrace();
}
return true;
}
public static Result getRow(String tableName, String rowKey){
try(Table table = HBaseConn.getTable(tableName)){
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
public static Result getRow(String tableName, String rowKey, FilterList filterList){
try(Table table = HBaseConn.getTable(tableName)){
Get get = new Get(Bytes.toBytes(rowKey));
get.setFilter(filterList);
return table.get(get);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
public static ResultScanner getScanner(String tableName){
try(Table table = HBaseConn.getTable(tableName)){
Scan scan = new Scan();
scan.setCaching(1000);
return table.getScanner(scan);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){
try(Table table = HBaseConn.getTable(tableName)){
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setCaching(1000);
return table.getScanner(scan);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
// 使用过滤器
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){
try(Table table = HBaseConn.getTable(tableName)){
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRowKey));
scan.setStopRow(Bytes.toBytes(endRowKey));
scan.setCaching(1000);
scan.setFilter(filterList);
return table.getScanner(scan);
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
// 删除
public static Boolean deleteRow(String tableName,String rowKey){
try(Table table = HBaseConn.getTable(tableName)){
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
return true;
}catch(IOException ioe){ioe.printStackTrace();}
return null;
}
// 删除 列簇 用 admin
public static Boolean deleteColumnFamily(String tableName,String cfName){
try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){
admin.deleteColumn(tableName,cfName);
}catch(IOException ioe){
ioe.printStackTrace();
}
return true;
}
//删除 某列 用 table
public static Boolean deleteQualifier(String tableName, String rowKey, String cfName,String qualifier){
try(Table table = HBaseConn.getTable(tableName)){
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier));
table.delete(delete);
return true;
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
}
hbase 实战项目的更多相关文章
- hbase实战——(1.1 nosql介绍)
什么是nosql NoSQL(NoSQL = Not Only SQL),意思是不仅仅是SQL的扩展,一般指的是非关系型的数据库. 随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0 ...
- Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录
一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...
- Python实战项目网络爬虫 之 爬取小说吧小说正文
本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...
- Linux系统实战项目——sudo日志审计
Linux系统实战项目——sudo日志审计 由于企业内部权限管理启用了sudo权限管理,但是还是有一定的风险因素,毕竟运维.开发等各个人员技术水平.操作习惯都不相同,也会因一时失误造成误操作,从而 ...
- HBase 实战(2)--时间序列检索和面检索的应用场景实战
前言: 作为Hadoop生态系统中重要的一员, HBase作为分布式列式存储, 在线实时处理的特性, 备受瞩目, 将来能在很多应用场景, 取代传统关系型数据库的江湖地位. 本篇主要讲述面向时间序列/面 ...
- android经典实战项目视频教程下载
注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...
- 前后端分离之vue2.0+webpack2 实战项目 -- webpack介绍
webpack的一点介绍 Webpack 把任何一个文件都看成一个模块,模块间可以互相依赖(require or import),webpack 的功能是把相互依赖的文件打包在一起.webpack 本 ...
- vue+websocket+express+mongodb实战项目(实时聊天)
继上一个项目用vuejs仿网易云音乐(实现听歌以及搜索功能)后,发现上一个项目单纯用vue的model管理十分混乱,然后我去看了看vuex,打算做一个项目练练手,又不想做一个重复的项目,这次我就放弃颜 ...
- vue+websocket+express+mongodb实战项目(实时聊天)(二)
原项目地址:[ vue+websocket+express+mongodb实战项目(实时聊天)(一)][http://blog.csdn.net/blueblueskyhua/article/deta ...
随机推荐
- Error fetching command 'collectstatic': You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Command 'collectstatic' skipped
报错现象 报错解决 在 settings.py 中添加这一句话则可以解决 STATIC_ROOT = os.path.join(BASE_DIR, 'static') 测试不在有问题
- HNOI2019 游记
HNOI2019 游记 Day 0 其实考前几天,心里还是挺慌的.结果最后 Day 0 的时候,因为种种原因反而释然了.也许是觉得,在这一步退役,也没有什么好害怕的吧. OI 本身就是一项偶然性太大的 ...
- 【转】从Vue.js源码看异步更新DOM策略及nextTick
在使用vue.js的时候,有时候因为一些特定的业务场景,不得不去操作DOM,比如这样: <template> <div> <div ref="test" ...
- luogu3188/bzoj1190 梦幻岛宝珠 (分层背包dp)
他都告诉你能拆了 那就拆呗.把每个重量拆成$a*2^b$的形式 然后对于每个不同的b,先分开做30个背包 再设f[i][j]表示b<=i的物品中 容量为$ j*2^i+W\&((1< ...
- centos7下mysql半同步复制原理安装测试详解
原理简介: 在MySQL5.5之前,MySQL的复制其实都是异步复制(见下图),主库和从库的数据之间存在一定的延迟,这样存在一个隐患:当在主库上写入一个事务并提交成功,而从库尚未得到主库推送的BinL ...
- poj1182、hdu1829(并查集)
题目链接:http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- 商品详情页系统的Servlet3异步化实践
http://jinnianshilongnian.iteye.com/blog/2245925 博客分类: 架构 在京东工作的这一年多时间里,我在整个商品详情页系统(后端数据源)及商品详情页统一 ...
- bzoj1497 最大获利(最大权闭合子图)
题目链接 思路 对于每个中转站向\(T\)连一条权值为建这个中转站代价的边.割掉这条边表示会建这个中转站. 对于每个人向他的两个中转站连一条权值为\(INF\)的边.然后从\(S\)向这个人连一条权值 ...
- url加时间戳方法及作用
速记:URL 的末尾追加了时间.这就确保了请求不会在它第一次被发送后即缓存,而是会在此方法每次被调用后重新创建和重发:此 URL 会由于时间戳的不同而稍微有些不同.这种技巧常被用于确保到脚本的 POS ...
- navicat premium 12破解流程
具体步骤:这是破解的具体链接 仅此记录,以供后续之需