hadoop - hbase shell命令及Java接口

Posted mthoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hadoop - hbase shell命令及Java接口相关的知识,希望对你有一定的参考价值。

一. shell命令

1. 进入hbase命令行  ./hbase shell


2. 显示hbase中的表  list

3. 创建user表,包括info、data两个列族
create ‘user‘, ‘info‘, ‘data‘
create ‘user‘, {NAME => ‘info‘, VERSIONS => ‘3‘}


4. 向表中插入信息:

向user表中插入信息。row key为rk0001。列族info中加入name列标示符,值为zhangsan
put ‘user‘, ‘rk0001‘, ‘info:name‘, ‘zhangsan‘

向user表中插入信息。row key为rk0001。列族info中加入gender列标示符,值为female
put ‘user‘, ‘rk0001‘, ‘info:gender‘, ‘female‘

向user表中插入信息。row key为rk0001,列族info中加入age列标示符。值为20
put ‘user‘, ‘rk0001‘, ‘info:age‘, 20

向user表中插入信息,row key为rk0001,列族data中加入pic列标示符,值为picture
put ‘user‘, ‘rk0001‘, ‘data:pic‘, ‘picture‘


5. 获取表中的信息:

获取user表中row key为rk0001的全部信息
get ‘user‘, ‘rk0001‘

获取user表中row key为rk0001,info列族的全部信息
get ‘user‘, ‘rk0001‘, ‘info‘

获取user表中row key为rk0001,info列族的name、age列标示符的信息
get ‘user‘, ‘rk0001‘, ‘info:name‘, ‘info:age‘

获取user表中row key为rk0001,info、data列族的信息
get ‘user‘, ‘rk0001‘, ‘info‘, ‘data‘
get ‘user‘, ‘rk0001‘, {COLUMN => [‘info‘, ‘data‘]}
get ‘user‘, ‘rk0001‘, {COLUMN => [‘info:name‘, ‘data:pic‘]}

获取user表中row key为rk0001,列族为info,版本最新5个的信息
get ‘people‘, ‘rk0002‘, {COLUMN => ‘info‘, VERSIONS => 2}
get ‘user‘, ‘rk0001‘, {COLUMN => ‘info:name‘, VERSIONS => 5}
get ‘user‘, ‘rk0001‘, {COLUMN => ‘info:name‘, VERSIONS => 5, TIMERANGE => [1392368783980, 1392380169184]}

获取user表中row key为rk0001。cell的值为zhangsan的信息
get ‘people‘, ‘rk0001‘, {FILTER => "ValueFilter(=, ‘binary:图片‘)"}

获取user表中row key为rk0001,列标示符中含有a的信息
get ‘people‘, ‘rk0001‘, {FILTER => "(QualifierFilter(=,‘substring:a‘))"}

put ‘user‘, ‘rk0002‘, ‘info:name‘, ‘fanbingbing‘
put ‘user‘, ‘rk0002‘, ‘info:gender‘, ‘female‘
put ‘user‘, ‘rk0002‘, ‘info:nationality‘, ‘中国‘
get ‘user‘, ‘rk0002‘, {FILTER => "ValueFilter(=, ‘binary:中国‘)"}


6. 查询表中信息:

查询user表中的全部信息
scan ‘user‘

查询user表中列族为info的信息
scan ‘people‘, {COLUMNS => ‘info‘}
scan ‘user‘, {COLUMNS => ‘info‘, RAW => true, VERSIONS => 5}
scan ‘persion‘, {COLUMNS => ‘info‘, RAW => true, VERSIONS => 3}
查询user表中列族为info和data的信息
scan ‘user‘, {COLUMNS => [‘info‘, ‘data‘]}
scan ‘user‘, {COLUMNS => [‘info:name‘, ‘data:pic‘]}

查询user表中列族为info、列标示符为name的信息
scan ‘user‘, {COLUMNS => ‘info:name‘}

查询user表中列族为info、列标示符为name的信息,而且版本号最新的5个
scan ‘user‘, {COLUMNS => ‘info:name‘, VERSIONS => 5}

查询user表中列族为info和data且列标示符中含有a字符的信息
scan ‘people‘, {COLUMNS => [‘info‘, ‘data‘], FILTER => "(QualifierFilter(=,‘substring:a‘))"}

查询user表中列族为info。rk范围是[rk0001, rk0003)的数据
scan ‘people‘, {COLUMNS => ‘info‘, STARTROW => ‘rk0001‘, ENDROW => ‘rk0003‘}

查询user表中row key以rk字符开头的
scan ‘user‘,{FILTER=>"PrefixFilter(‘rk‘)"}

查询user表中指定范围的数据
scan ‘user‘, {TIMERANGE => [1392368783980, 1392380169184]}

7. 删除表中数据:
删除user表row key为rk0001,列标示符为info:name的数据

delete ‘people‘, ‘rk0001‘, ‘info:name‘


删除user表row key为rk0001,列标示符为info:name。timestamp为1392383705316的数据
delete ‘user‘, ‘rk0001‘, ‘info:name‘, 1392383705316

清空user表中的数据
truncate ‘people‘


8. 其它操作

改动表结构:
首先停用user表(新版本号不用)
disable ‘user‘

加入两个列族f1和f2
alter ‘people‘, NAME => ‘f1‘

alter ‘user‘, NAME => ‘f2‘


启用表enable ‘user‘


###disable ‘user‘(新版本号不用)
删除一个列族:
alter ‘user‘, NAME => ‘f1‘, METHOD => ‘delete‘ 或 alter ‘user‘, ‘delete‘ => ‘f1‘

加入列族f1同一时候删除列族f2
alter ‘user‘, {NAME => ‘f1‘}, {NAME => ‘f2‘, METHOD => ‘delete‘}

将user表的f1列族版本改为5
alter ‘people‘, NAME => ‘info‘, VERSIONS => 5
启用表
enable ‘user‘

删除表
disable ‘user‘

drop ‘user‘

技术分享


二. java接口

public class HbaseDemo {

	private Configuration conf = null;
	
	@Before
	public void init(){
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
	}
	
	@Test
	public void testDrop() throws Exception{
		HBaseAdmin admin = new HBaseAdmin(conf);
		admin.disableTable("account");
		admin.deleteTable("account");
		admin.close();
	}
	
	@Test
	public void testPut() throws Exception{
		HTable table = new HTable(conf, "user");
		Put put = new Put(Bytes.toBytes("rk0003"));
		put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));
		table.put(put);
		table.close();
	}
	
	@Test
	public void testGet() throws Exception{
		HTable table = new HTable(conf, "user");
		Get get = new Get(Bytes.toBytes("rk0001"));
		get.setMaxVersions(5);
		Result result = table.get(get);
		for(KeyValue kv : result.list()){
			String family = new String(kv.getFamily());
			String qualifier = new String(kv.getQualifier());
			String value = new String(kv.getValue());
			System.out.println("family: " + ", qualifier: " + qualifier + ", value: " + value);
		}
		table.close();
	}
	
	@Test
	public void testScan() throws Exception{
		HTablePool pool = new HTablePool(conf, 10);
		HTableInterface table = pool.getTable("user");
		Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0002"));
		scan.addFamily(Bytes.toBytes("info"));
		ResultScanner scanner = table.getScanner(scan);
		for(Result r : scanner){
			byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
			System.out.println(new String(value));
		}
		pool.close();
	}
	
	
	@Test
	public void testDel() throws Exception{
		HTable table = new HTable(conf, "user");
		Delete del = new Delete(Bytes.toBytes("rk0001"));
		del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
		table.delete(del);
		table.close();
	}
}
























































































































以上是关于hadoop - hbase shell命令及Java接口的主要内容,如果未能解决你的问题,请参考以下文章

HBase学习01(HBase入门及HBase Shell简单操作)

HBase学习01(HBase入门及HBase Shell简单操作)

Hadoop集群(第13期)_HBase 常用Shell命令

hbase 导入导出hbase shell 基本命令。

HBase集群搭建及hbaseshell使用

Hbase使用shell命令出现报错:PleaseHoldException: Master is initializing 解决办法