在之前的文章中记录了安装步骤文章链接下面学习怎么使用
基本shell命令
#启动,如果配置了环境变量就这样
[root@node-1 bin]# hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/export/server/hbase/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/export/server/hadoop-2.7.4/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr 6 19:36:54 PDT 2017
hbase(main):001:0>
#显示hbase中的表,这里显示的我之前建的表
hbase(main):001:0> list
TABLE
user
1 row(s) in 0.1500 seconds
=> ["user"]
#删除表, 删除之前先停用
hbase(main):002:0> disable 'user'
0 row(s) in 2.6330 seconds
hbase(main):003:0> drop 'user'
0 row(s) in 1.2430 seconds
#再次查看,成功删除
hbase(main):004:0> list
TABLE
0 row(s) in 0.0050 seconds
=> []
#创建user_def表,包含info、data两个列族,列族设置默认属性
#create 'user_def', 'info', 'data'
#创建user表,包含info、data两个列族,列族设置属性
#create 'user', {NAME => 'info', VERSIONS => '3'},{NAME => 'data', VERSIONS => '2'}
hbase(main):005:0> create 'user_def', 'info', 'data'
0 row(s) in 1.3080 seconds
=> Hbase::Table - user_def
hbase(main):006:0> create 'user', {NAME => 'info', VERSIONS => '3'},{NAME => 'data', VERSIONS => '2'}
0 row(s) in 1.2270 seconds
=> Hbase::Table - user
#增加删除列族:(在表有数据的情况下,要先disable表,才能alter)
# 增加:alter 'userInfo', 'add_family'
# alter 'userInfo', { NAME => 'add_new', VERSIONS => 3 }
# 删除:alter 'userInfo', 'delete'=> 'add_family'
# alter 'userInfo', { NAME => 'add_new', METHOD => 'delete' }
#再次查询
hbase(main):007:0> list
TABLE
user
user_def
2 row(s) in 0.0060 seconds
=> ["user", "user_def"]
#向user表中插入信息,row key为rk0001,列族info中添加name列标示符,值为zhangsan
hbase(main):008:0> put 'user', 'rk0001', 'info:name', 'zhangsan'
0 row(s) in 0.0780 seconds
#向user表中插入信息,row key为rk0001,列族info中添加gender列标示符,值为female
hbase(main):009:0> put 'user', 'rk0001', 'info:gender', 'female'
0 row(s) in 0.0110 seconds
#向user表中插入信息,row key为rk0001,列族info中添加age列标示符,值为20
hbase(main):010:0> put 'user', 'rk0001', 'info:age', 20
0 row(s) in 0.0080 seconds
#向user表中插入信息,row key为rk0001,列族data中添加pic列标示符,值为picture
hbase(main):001:0> put 'user', 'rk0001', 'data:pic', 'picture'
0 row(s) in 0.2020 seconds
#获取user表中row key为rk0001的所有信息
hbase(main):001:0> get 'user', 'rk0001'
COLUMN CELL
data:pic timestamp=1574233372994, value=picture
info:age timestamp=1574233309431, value=20
info:gender timestamp=1574233266809, value=female
info:name timestamp=1574233198507, value=zhangsan
1 row(s) in 0.1770 seconds
#获取user表中row key为rk0001,info列族的所有信息
hbase(main):002:0> get 'user', 'rk0001', 'info'
COLUMN CELL
info:age timestamp=1574233309431, value=20
info:gender timestamp=1574233266809, value=female
info:name timestamp=1574233198507, value=zhangsan
1 row(s) in 0.0130 seconds
#获取user表中row key为rk0001,info列族的name、age列标示符的信息
hbase(main):003:0> get 'user', 'rk0001', 'info:name', 'info:age'
COLUMN CELL
info:age timestamp=1574233309431, value=20
info:name timestamp=1574233198507, value=zhangsan
1 row(s) in 0.0130 seconds
#获取user表中row key为rk0001,info、data列族的信息
hbase(main):004:0> get 'user', 'rk0001', 'info', 'data'
COLUMN CELL
data:pic timestamp=1574233372994, value=picture
info:age timestamp=1574233309431, value=20
info:gender timestamp=1574233266809, value=female
info:name timestamp=1574233198507, value=zhangsan
1 row(s) in 0.0150 seconds
hbase(main):005:0> get 'user', 'rk0001', {COLUMN => ['info', 'data']}
COLUMN CELL
data:pic timestamp=1574233372994, value=picture
info:age timestamp=1574233309431, value=20
info:gender timestamp=1574233266809, value=female
info:name timestamp=1574233198507, value=zhangsan
1 row(s) in 0.0130 seconds
hbase(main):006:0> get 'user', 'rk0001', {COLUMN => ['info:name', 'data:pic']}
COLUMN CELL
data:pic timestamp=1574233372994, value=picture
info:name timestamp=1574233198507, value=zhangsan
1 row(s) in 0.0180 seconds
基本增删改查java实现
public class HbaseDemo {
private Configuration conf = null;
@Before
public void init(){
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "weekend05,weekend06,weekend07");
}
@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, "person_info");
Put p = new Put(Bytes.toBytes("person_rk_bj_zhang_000002"));
p.add("base_info".getBytes(), "name".getBytes(), "zhangwuji".getBytes());
table.put(p);
table.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();
}
@Test
public void testGet() throws Exception{
HTable table = new HTable(conf, "person_info");
Get get = new Get(Bytes.toBytes("person_rk_bj_zhang_000001"));
get.setMaxVersions(5);
Result result = table.get(get);
List<Cell> cells = result.listCells();
for(Cell c:cells){
}
//result.getValue(family, qualifier); 可以从result中直接取出一个特定的value
//遍历出result中所有的键值对
List<KeyValue> kvs = result.list();
//kv ---> f1:title:superise.... f1:author:zhangsan f1:content:asdfasldgkjsldg
for(KeyValue kv : kvs){
String family = new String(kv.getFamily());
System.out.println(family);
String qualifier = new String(kv.getQualifier());
System.out.println(qualifier);
System.out.println(new String(kv.getValue()));
}
table.close();
}