HBase的shell操作和JavaAPI的使用:

Shell

表操作

创建表

1
create 'student','info'           #表名  列族

插入表

1
2
3
4
5
put 'student','1001','info:sex','male'
put 'student','1001','info:age','18'
put 'student','1002','info:name','Janna'
put 'student','1002','info:sex','female'
put 'student','1002','info:age','20'

查看表数据

1
2
3
scan 'student'
scan 'student',{STARTROW => '1001', STOPROW  => '1002'}   #左闭右开
scan 'student',{STARTROW => '1001'}

查看表结构

1
2
describe 'student'
desc 'student'             #效果相同

更新指定字段

1
2
put 'student','1001','info:name','Nick'
put 'student','1001','info:age','100'  #  表明  rowkey  列族:列 值

查看指定行数据

1
get 'student','1001'

查看指定列族:列

1
get 'student','1001','info:name'

统计表行数

1
count 'student'

删除数据

1
deleteall 'student','1001'

删除rowkey的某一列

1
delete 'student','1002','info:sex'

清空数据

1
2
truncate 'student'
#表的操作顺序为先disable,然后再truncate。

删除表

1
disable 'student'

表更表信息

1
alter 'student',{NAME=>'info',VERSIONS=>3} ##将info列族中的数据存放3个版本:

Java API

环境准备

1
2
3
4
5
6
7
8
9
10
11
<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>1.3.1</version>
</dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.3.1</version>
</dependency>

HBaseAPI

获取Configuration对象

1
2
3
4
5
6
7
public static Configuration conf;
static{//使用HBaseConfiguration的单例方法实例化conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.101"); ## 换成自己的ZK节点IP
conf.set("hbase.zookeeper.property.clientPort", "2181");
}

判断表是否存在

1
2
3
4
5
6
public static boolean isTableExist(String tableName) throws MasterNotRunningException,ZooKeeperConnectionException, IOException{
//在HBase中管理、访问表需要先创建HBaseAdmin对象HBaseAdmin admin = new HBaseAdmin(conf);return admin.tableExists(tableName);
}

创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void createTable(String tableName, String... columnFamily) throwsMasterNotRunningException, ZooKeeperConnectionException, IOException{HBaseAdmin admin = new HBaseAdmin(conf);//判断表是否存在if(isTableExist(tableName)){System.out.println("表" + tableName + "已存在");//System.exit(0);}else{//创建表属性对象,表名需要转字节HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));//创建多个列族for(String cf : columnFamily){descriptor.addFamily(new HColumnDescriptor(cf));}//根据对表的配置,创建表admin.createTable(descriptor);System.out.println("表" + tableName + "创建成功!");}
}

删除表

1
2
3
4
5
6
7
8
9
10
11
public static void dropTable(String tableName) throws MasterNotRunningException,ZooKeeperConnectionException, IOException{HBaseAdmin admin = new HBaseAdmin(conf);if(isTableExist(tableName)){admin.disableTable(tableName);admin.deleteTable(tableName);System.out.println("表" + tableName + "删除成功!");}else{System.out.println("表" + tableName + "不存在!");}
}

插入数据

1
2
3
4
5
6
7
8
9
10
11
12
public static void addRowData(String tableName, String rowKey, String columnFamily, Stringcolumn, String value) throws IOException{//创建HTable对象HTable hTable = new HTable(conf, tableName);//向表中插入数据Put put = new Put(Bytes.toBytes(rowKey));//向Put对象中组装数据put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));hTable.put(put);hTable.close();System.out.println("插入数据成功");
}

删除多行数据

1
2
3
4
5
6
7
8
9
10
public static void deleteMultiRow(String tableName, String... rows) throws IOException{HTable hTable = new HTable(conf, tableName);List<Delete> deleteList = new ArrayList<Delete>();for(String row : rows){Delete delete = new Delete(Bytes.toBytes(row));deleteList.add(delete);}hTable.delete(deleteList);hTable.close();
}

获取所有数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void getAllRows(String tableName) throws IOException{HTable hTable = new HTable(conf, tableName);//得到用于扫描region的对象Scan scan = new Scan();//使用HTable得到resultcanner实现类的对象ResultScanner resultScanner = hTable.getScanner(scan);for(Result result : resultScanner){Cell[] cells = result.rawCells();for(Cell cell : cells){//得到rowkeySystem.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));}}
}

获取某一行数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void getRow(String tableName, String rowKey) throws IOException{HTable table = new HTable(conf, tableName);Get get = new Get(Bytes.toBytes(rowKey));//get.setMaxVersions();显示所有版本//get.setTimeStamp();显示指定时间戳的版本Result result = table.get(get);for(Cell cell : result.rawCells()){System.out.println("行键:" + Bytes.toString(result.getRow()));System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));System.out.println("时间戳:" + cell.getTimestamp());}
}

获取某一行指定“列族:列”的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void getRowQualifier(String tableName, String rowKey, String family, Stringqualifier) throws IOException{HTable table = new HTable(conf, tableName);Get get = new Get(Bytes.toBytes(rowKey));get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));Result result = table.get(get);for(Cell cell : result.rawCells()){System.out.println("行键:" + Bytes.toString(result.getRow()));System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));}
}

MapReduce

HBase的相关JavaAPI,可以实现伴随HBase操作的MapReduce过程,使用MapReduce将数据从本地文件系统导入到HBase的表中或者从HBase中读取一些原始数据后使用MapReduce做数据分析。

Hive集成Hbase

编译:hive-hbase-handler-1.2.2.jar

1
2
3
4
5
6
7
8
ln -s $HBASE_HOME/lib/hbase-common-1.3.1.jar  $HIVE_HOME/lib/hbase-common-1.3.1.jar
ln -s $HBASE_HOME/lib/hbase-server-1.3.1.jar $HIVE_HOME/lib/hbase-server-1.3.1.jar
ln -s $HBASE_HOME/lib/hbase-client-1.3.1.jar $HIVE_HOME/lib/hbase-client-1.3.1.jar
ln -s $HBASE_HOME/lib/hbase-protocol-1.3.1.jar $HIVE_HOME/lib/hbase-protocol-1.3.1.jar
ln -s $HBASE_HOME/lib/hbase-it-1.3.1.jar $HIVE_HOME/lib/hbase-it-1.3.1.jar
ln -s $HBASE_HOME/lib/htrace-core-3.1.0-incubating.jar $HIVE_HOME/lib/htrace-core-3.1.0-incubating.jar
ln -s $HBASE_HOME/lib/hbase-hadoop2-compat-1.3.1.jar $HIVE_HOME/lib/hbase-hadoop2-compat-1.3.1.jar
ln -s $HBASE_HOME/lib/hbase-hadoop-compat-1.3.1.jar $HIVE_HOME/lib/hbase-hadoop-compat-1.3.1.jar

hive-site.xml中修改zookeeper的属性,如下:

1
2
3
4
5
6
7
8
9
10
<property><name>hive.zookeeper.quorum</name><value>datanode1,datanode2,datanode3</value><description>The list of ZooKeeper servers to talk to. This is only needed for read/write locks.</description>
</property>
<property><name>hive.zookeeper.client.port</name><value>2181</value><description>The port of ZooKeeper servers to talk to. This is only needed for read/write locks.</description>
</property>

案例

创建hive关联表

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE hive_hbase_emp_table(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:ename,info:job,info:mgr,info:hiredate,info:sal,info:comm,info:deptno")
TBLPROPERTIES ("hbase.table.name" = "hbase_hive_emp_table_");

在Hive中创建临时中间表,用于load文件中的数据

1
2
3
4
5
6
7
8
9
10
CREATE TABLE emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by ',';

向Hive中间表中load数据

1
load data local inpath '/home/hadoop/emp.csv' into table emp;

通过insert命令将中间表中的数据导入到Hive关联HBase的那张表中

1
insert into table hive_hbase_emp_table select * from emp;

查看HIVE表

1
select * from hive_hbase_emp_table;

查看HBase表

1
scan ‘hbase_emp_table’

在HBase中已经存储了某一张表hbase_emp_table,然后在Hive中创建一个外部表来关联HBase中的hbase_emp_table这张表,使之可以借助Hive来分析HBase这张表中的数据。

Hive创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE EXTERNAL TABLE relevance_hbase_emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
STORED BY
'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" =
":key,info:ename,info:job,info:mgr,info:hiredate,info:sal,info:comm,info:deptno")
TBLPROPERTIES ("hbase.table.name" = "hbase_emp_table");

关联后就可以使用Hive函数进行一些分析操作了

1
select * from relevance_hbase_emp;

HBase的Shell命令和JavaAPI相关推荐

  1. Hbase的shell命令学习

    在学习Hbase的shell命令,之前先得了解如何进入hbase的shell命令行,通过执行如下简单的命令回车后进入hbase的shell命令行界面 hbase shell 进入hbase命令行后,执 ...

  2. Hbase教程(二) Hbase数据库Shell命令

    Hbase教程(二) Hbase数据库Shell命令 1 Hbase- Shell命令 Hbase是分布式是一个分布式的.面向列的开源数据库,Hbase也提供了Shell命令对数据库增.删.改.查的权 ...

  3. HBase 常用Shell命令

    转自:http://my.oschina.net/u/189445/blog/595232 两个月前使用过hbase,现在最基本的命令都淡忘了,留一个备查~ hbase shell命令         ...

  4. 【转】Hbase之shell命令的使用

    shell命令 DDL(数据定义语言) 名称 描述 表达式 list 列出存在哪些表 list desc/describe 表描述信息 desc 'tableName' create 创建表 cf表示 ...

  5. HBase总结(二十)HBase常用shell命令详细说明

    进入hbase shell console $HBASE_HOME/bin/hbase shell 如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成 ...

  6. Hbase之一月速成:Hbase的shell命令

    目录 一.命令总汇 二.需求 三.基本操作 1.创建表 2.查看表 3.删除表 1)禁用表 3)删除表 四.数据的操作 1.添加数据 2. 获取数据 3.更新数据 4.删除数据 1)删除指定列 2)删 ...

  7. Hbase 常用 Shell 命令

    一.基本命令 打开 Hbase Shell: # hbase shell 1.1 获取帮助 # 获取帮助 help # 获取命令的详细信息 help 'status' 1.2 查看服务器状态 stat ...

  8. Linux启动hbase的shell命令出现警告_Linux 系统故障排查和修复技巧

    我发现Linux系统在启动过程中会出现一些故障,导致系统无法正常启动,我在这里写了几个应用单用户模式.GRUB命令操作.Linux救援模式的故障修复案例帮助大家了解此类问题的解决. (一)单用户模式 ...

  9. HBase常用shell命令和实践

    create 创建表 指定表名.列族.列族版本号 hbase >> create 't1' , { NAME => 'f1' , VERSIONS => 5} 创建表t1,3个 ...

最新文章

  1. 电脑蓝屏终止代码irql_电脑蓝屏代码浅析
  2. 计算两个日期相差几年几个月
  3. java代码编译时修改行为_GitHub - niuzhihua/AST_demo: 利用JavaParser框架在编译时修改语法树(源码)的 demo...
  4. mysql 异常处理实例
  5. 浅谈C++的virtual 动态绑定。
  6. matlab拟合参数最优,使用matlab最优化方法拟合获得多个动力学参数中的问题 - 计算模拟 - 小木虫 - 学术 科研 互动社区...
  7. mysql文件类型_MyCat教程:实现MySql主从复制
  8. 磁盘及文件系统的管理
  9. image pil 图像保存_使用PIL保存图像
  10. JavaScript练习
  11. Oracle在SQL语句中对时间操作、运算
  12. spss20安装许可证代码_SPSS 22下载安装教程
  13. java 编写浏览器_用Java自己写一个浏览器 —— JavaFX 入门
  14. android studio项目同步失败,java - 在android studio 3.2中Gradle项目同步失败 - 堆栈内存溢出...
  15. SPOJ 4487 Splay 基本操作
  16. 【PTA】【878真题】浙江大学软件学院878自命题2016真题
  17. 新浪微博授权登陆获取个人信息
  18. 10_放置街灯(Placing Lampposts,UVa 10859)
  19. Windows Live Message (MSN) 登陆错误解决办法 -- windows live communications platform 遇到问题需要关闭
  20. 计算机修改人类记忆曲线,遗忘曲线——揭秘人类记忆存储的奥秘

热门文章

  1. powerbi python词云图_Python 练手项目: 抓取豆瓣陈情令评论,并制作词云图
  2. jquery load 事件用法
  3. oracle备份表和数据
  4. 自定义的plot函数参数date坐标模型[x,y]的使用建议
  5. xmlxml约束dtdxml解析器
  6. 基于界面自动化测试框架的发展
  7. [转]ubuntu network is unreachable 解决记
  8. Leetcode——连续子数组的最大和(剑指offer 42)
  9. python元组和集合的区别_python 元组与list的区别
  10. PostgreSQL如何使用PLJava支持Java编程