HBase Shell And JavaAPI使用

HBase shell操作

表创建

#进入hbase shell
[root@centos bin]# hbase shell
#创建一张user表,有三个列族
hbase(main):001:0> create 'user','uid','address','info44'
#查看表
hbase(main):002:0> list
TABLE
user

查看表结构

hbase(main):001:0> describe 'user'
DESCRIPTION                                                                                           ENABLED
 'user', {NAME => 'address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SCOPE  true
 => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELET
 ED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCACH
 E => 'true'}, {NAME => 'info44', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_S
 COPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_
 DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOC
 KCACHE => 'true'}, {NAME => 'uid', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION
 _SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEE
 P_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BL
 OCKCACHE => 'true'}

 删除表

#不能直接删除
hbase(main):005:0* drop 'user'
ERROR: Table user is enabled. Disable it first.'
Here is some help for this command:
Drop the named table. Table must first be disabled: e.g. "hbase> drop 't1'"
#先disable一张表
hbase(main):006:0> disable 'user'
0 row(s) in 2.1410 seconds
#才能删除
hbase(main):007:0> drop 'user'
0 row(s) in 1.1180 seconds
hbase(main):008:0> list
TABLE
0 row(s) in 0.0400 seconds

 插入一条记录

#表明,行健,列族,值
hbase(main):004:0> put 'user','lisi','address:city','beijing'
hbase(main):005:0> get 'user','lisi'
COLUMN                                   CELL
 address:city                            timestamp=1439284919358, value=beijing
#查询address列族信息
hbase(main):005:0> get 'user','lisi','address'
#查询address列族中city修饰符
hbase(main):005:0> get 'user','lisi','address:city'

查看记录版本信息,默认最多显示3条例是版本

#查看wangwu info列族的age标识符版本信息
hbase(main):016:0> get 'user','wangwu',{COLUMN=>'info:age',VERSIONS=>3}
COLUMN                                   CELL
 info:age                                timestamp=1439285717405, value=28
 info:age                                timestamp=1439285714541, value=27
 info:age                                timestamp=1439285711351, value=26

 查询整长表

hbase(main):001:0> scan 'user'

删除一行记录

hbase(main):001:0> deleteall 'user','wangwu'

删除一列记录

#删除行健为wangwu的info列族中age标识符列
hbase(main):001:0>delete 'user','wangwu','info:age'

清空表数据

hbase(main):001:0> truncate 'user'

HBase JavaAPI

创建表

@Test
	public void createTableTest() throws Exception {
		Configuration configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://192.168.56.101:/hbase");
		configuration.set("hbase.zookeeper.quorum", "centos");
		
		HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
		// exist table name
		if (!hBaseAdmin.tableExists("tablee")) {
			// create table
			HTableDescriptor tableDescriptor = new HTableDescriptor("tablee");
			// create colum family
			HColumnDescriptor columnDescriptor = new HColumnDescriptor("familyy");
			// set column family in table
			tableDescriptor.addFamily(columnDescriptor);
			// create
			hBaseAdmin.createTable(tableDescriptor);
		}
		hBaseAdmin.close();
	}

 删除表

@Test
	public void deleteTableTest() throws Exception {
		Configuration configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://192.168.56.101:/hbase");
		configuration.set("hbase.zookeeper.quorum", "centos");
		
		HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
		hBaseAdmin.disableTable("tablee");
		hBaseAdmin.deleteTable("tablee");
		hBaseAdmin.close();
	}

 插入一条记录

@Test
	public void insertDataTest() throws Exception {
		Configuration configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://192.168.56.101:/hbase");
		configuration.set("hbase.zookeeper.quorum", "centos");

		HTable hTable = new HTable(configuration, "tablee");
		// set rowkey
		Put put = new Put("rowkey3".getBytes());
		// set column family (列族名,列族标识符,值)
		put.add("familyy".getBytes(), "age".getBytes(), "25".getBytes());
		hTable.put(put);
		hTable.close();
	}

 查询一条记录

@Test
	public void getRecordDataTest() throws Exception {
		Configuration configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://192.168.56.101:/hbase");
		configuration.set("hbase.zookeeper.quorum", "centos");

		HTable hTable = new HTable(configuration, "tablee");
		Get get = new Get("rowkey1".getBytes());
		Result result = hTable.get(get);
		System.out.println(result);
		// get column family
		byte[] ageValue = result.getValue("familyy".getBytes(), "age".getBytes());
		System.out.println(new String(ageValue));
		hTable.close();
	}

 查询全表数据

@Test
	public void findAllDataTest() throws Exception {
		Configuration configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://192.168.56.101:/hbase");
		configuration.set("hbase.zookeeper.quorum", "centos");

		HTable hTable = new HTable(configuration, "tablee");
		ResultScanner resultScanner = hTable.getScanner(new Scan());
		for (Result result : resultScanner) {
			byte[] row = result.getRow();
			byte[] ageValue = result.getValue("familyy".getBytes(), "age".getBytes());
			System.out.println(new String(row) + "\t" + new String(ageValue));
		}
		hTable.close();
	}

猜你喜欢

转载自mvplee.iteye.com/blog/2234107