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 ...
随机推荐
- sg函数小结
sg函数小结 sg函数是处理博弈问题的重要工具. 我们知道sg(x)=mex{sg(j)|x能到达状态j} sg(x)=0时代表后手赢,否则先手赢. 对于一个问题,如果某些子问题是相互独立的,我们就可 ...
- Android LinearLayout 渐变背景
更多 参考 Drawable Resources | Android Developers main_header.xml: <?xml version="1.0" enc ...
- 天梯赛 L2-001 紧急救援
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道 ...
- 简明的Python教程当中的几个疑惑点分析#1
#1简明的Python教程中的第11章面向对象编程学习中的类与对象的方法里面 有这么一个案例:使用类与对象的变量 #coding:utf-8 #类与对象的变量学习 class Person: popu ...
- webpack中babel配置 --- runtime-transform和babel-pollfill
webpack - babel配置 babel是一个javascript编译器,是前端开发中的一个利器.它突破了浏览器实现es标准的限制,使我们在开发中可以使用最新的javascript语法. 通过构 ...
- 厘摩(centimorgan,cM)到底是啥鬼
根据维基百科的定义: 厘摩(centimorgan,简写为cM),或称为图距单位(map unit),是遗传连锁中的距离单位,以现代遗传学之父托马斯·亨特·摩尔根的名字命名.1厘摩的定义为两个位点间平 ...
- 斯坦福大学公开课机器学习:advice for applying machine learning | model selection and training/validation/test sets(模型选择以及训练集、交叉验证集和测试集的概念)
怎样选用正确的特征构造学习算法或者如何选择学习算法中的正则化参数lambda?这些问题我们称之为模型选择问题. 在对于这一问题的讨论中,我们不仅将数据分为:训练集和测试集,而是将数据分为三个数据组:也 ...
- 在windows中把一个文件夹打成war包
转: 在windows中把一个文件夹打成war包 一般开发打war包时都是用MyEclipse或IntelliJ IDEA等直接导出war文件,这里介绍一种如何把一个文件夹打成war包的方式,如下 ...
- struts2 对EL的改变
Struts2对EL的改变 1.Struts2中使用EL的问题: 前提: 我们应该知道,如果我们没有往值栈(根)中放入数据的话,那么我们的动作类默认是在值栈的栈顶 2.关于EL问题的分析: 分析: ...
- JDBC的简单笔记
JDBC笔记: JDBC:java database connectivity SUN公司提供的一套操作数据库的标准规范. JDBC与数据库驱动的关系:接口与实现的关系. JDBC规范(掌握四个核心对 ...