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 ...
随机推荐
- maven "mvn不是内部或外部命令,也不是可运行的程序或批处理文件"
配置maven环境变量cmd控制台提示:mvn不是内部或外部命令,也不是可运行的程序或批处理文件 首先maven环境变量: 变量名:MAVEN_HOME 变量值:E:\apache-maven-3.2 ...
- zabbix python 微信告警脚本
测试zabbix的微信告警耗费了大量时间,使用了开源工具(OneOaaS weixin-alert).shell脚本工具(手动执行正常,服务器调用失败),均没有实现相关功能以下是自己优化过的Pytho ...
- CS academy Binary Flips(dp)
开学啦,没啥时间写博客..过几天就能又停课啦qwq 做点中等 \(dp\) 题来找找 noip 的感觉 233 题意 原题戳这里. 给你一个 \(n \times m\) 的矩阵 \(A\) ,一开始 ...
- Codeforces Round #543 Div1题解(并不全)
Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...
- photoshop学习3
一.仿制图章工具 快捷键:S. 操作:先按住ALT键,再点击图片的一个地方,然后松开ALT和鼠标(这叫取样).之后到画布的另一个地方用鼠标绘画. 特点:绘画出和取样点一样的图像.这个工具原样复制了取样 ...
- ajaxmin.exe 命令参数
这个工具用来压缩JS和CSS文件的,可去掉多余空格,换行,还修改函数名变得难以读取等. 下载这工具,下载之后是一个.msi文件可以安装.安装之后在命令行里操作即可 (下载地址和文档) http://a ...
- Codeforces Round #402 (Div. 2) D. String Game
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- 觉得一篇讲SPFA还不错的文章
我觉得他整理的有一些乱,我都改成插入代码了,看的顺眼一些 转载自http://blog.csdn.net/juststeps/article/details/8772755 下面的都是原文: 最短路径 ...
- Ability
Base:网络安全,sklearn(ML),日本語,企业存储 Branch1:自动化,Git Branch2:HW系统架构 Branch3:shadowsocks源码:
- Nginx反向代理后端多节点下故障节点的排除思路
仔细想来,其实是个非常简单的问题:开发和运维觉得两个后端节点跑起来压力太大了,就扩充了两个新的后端节点上去,这一加就出问题了,访问时页面间歇性丢失,这尼玛什么情况...想了半天没思路,查了Nginx的 ...