首先 根据 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 实战项目的更多相关文章

  1. hbase实战——(1.1 nosql介绍)

    什么是nosql NoSQL(NoSQL = Not Only SQL),意思是不仅仅是SQL的扩展,一般指的是非关系型的数据库. 随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0 ...

  2. Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录

    一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...

  3. Python实战项目网络爬虫 之 爬取小说吧小说正文

    本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...

  4. Linux系统实战项目——sudo日志审计

    Linux系统实战项目——sudo日志审计   由于企业内部权限管理启用了sudo权限管理,但是还是有一定的风险因素,毕竟运维.开发等各个人员技术水平.操作习惯都不相同,也会因一时失误造成误操作,从而 ...

  5. HBase 实战(2)--时间序列检索和面检索的应用场景实战

    前言: 作为Hadoop生态系统中重要的一员, HBase作为分布式列式存储, 在线实时处理的特性, 备受瞩目, 将来能在很多应用场景, 取代传统关系型数据库的江湖地位. 本篇主要讲述面向时间序列/面 ...

  6. android经典实战项目视频教程下载

    注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...

  7. 前后端分离之vue2.0+webpack2 实战项目 -- webpack介绍

    webpack的一点介绍 Webpack 把任何一个文件都看成一个模块,模块间可以互相依赖(require or import),webpack 的功能是把相互依赖的文件打包在一起.webpack 本 ...

  8. vue+websocket+express+mongodb实战项目(实时聊天)

    继上一个项目用vuejs仿网易云音乐(实现听歌以及搜索功能)后,发现上一个项目单纯用vue的model管理十分混乱,然后我去看了看vuex,打算做一个项目练练手,又不想做一个重复的项目,这次我就放弃颜 ...

  9. vue+websocket+express+mongodb实战项目(实时聊天)(二)

    原项目地址:[ vue+websocket+express+mongodb实战项目(实时聊天)(一)][http://blog.csdn.net/blueblueskyhua/article/deta ...

随机推荐

  1. MT【286】最佳有理逼近

    2017北大优秀中学生夏令营已知$\omega $是整系数方程$x^2+ax+b=0$的一个无理数根, 求证:存在常数$C$,使得对任意互质的正整数$p,q$都有$$|\omega-\dfrac{p} ...

  2. Leetcode 349. 两个数组的交集 By Python

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: nums1 = [4,9,5], ...

  3. Double.valueOf(0.0D) 分析

    private Double price = Double.valueOf(0.0D); 查看Java API 文档如下: doubleValue public double doubleValue( ...

  4. [SPOJ913]QTREE2 - Query on a tree II【倍增LCA】

    题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各 ...

  5. linux rpm安装 failed depenencie(失败的依赖关系)错误原因

    rpm安装nfs 出现failed depenencie 经查资料得知命令后加上--nodeps --force,就可以了 加上那两个参数的意义就在于,安装时不再分析包之间的依赖关系而直接安装,也就不 ...

  6. js中获取css样式属性值

    关于js中style,currentStyle和getComputedStyle几个注意的地方 (1)用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的.针对css ...

  7. layer 弹出层

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. A.01.02—模块的输入—高端输入

    高端输入即一个高电平信号输入到模块,模块采样时最典型的是采用下拉电阻采样,当然,还有限流电阻和分压电阻,具体可以参见实际电路. 高端输入在汽车上用得不多,这种类型的输入既可以是开关提供的也可以是模块提 ...

  9. 20165223《Java程序设计》第八周Java学习总结

    教材学习内容总结 第12章-JAVA多线程机制 要点 Java中的线程 Thread类与线程的创建 线程的常用方法 线程同步 协调同步的线程 线程联合 GUI线程 计时器线程 教材学习中的问题和解决过 ...

  10. 牛客网NOIP赛前集训营-提高组(第一场)

    牛客的这场比赛感觉真心不错!! 打得还是很过瘾的.水平也比较适合. T1:中位数: 题目描述 小N得到了一个非常神奇的序列A.这个序列长度为N,下标从1开始.A的一个子区间对应一个序列,可以由数对[l ...