HBase 5分钟 快速入门

1、下载,解压

2、修改conf/habase-site.xml文件,如下:

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hbase.rootdir</name> <value>file:///data/hbase</value> </property> </configuration>

file:///data/hbase为数据文件储存目录:/data/hbase

3、启动:bin/start-hbase.sh

4、浏览器访问:http://192.168.1.103:60010/

5、启动hbase shell:$:./hbase shell,如下:

$ ./bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version: 0.90.0, r1001068, Fri Sep 24 13:55:42 PDT 2010

hbase(main):001:0>
hbase(main):004:0> list
TABLE
0 row(s) in 0.0510 seconds
hbase(main):003:0> create 'test', 'cf'
0 row(s) in 1.2200 seconds
hbase(main):010:0> list
TABLE
test
1 row(s) in 0.0600 seconds
hbase(main):004:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.0560 seconds
hbase(main):005:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0370 seconds
hbase(main):006:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0450 seconds
hbase(main):007:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1288380727188, value=value1
row2 column=cf:b, timestamp=1288380738440, value=value2
row3 column=cf:c, timestamp=1288380747365, value=value3
3 row(s) in 0.0590 seconds
hbase(main):008:0> get 'test', 'row1'
COLUMN CELL
cf:a timestamp=1288380727188, value=value1
1 row(s) in 0.0400 seconds
hbase(main):012:0> disable 'test'
0 row(s) in 1.0930 seconds
hbase(main):013:0> drop 'test'
0 row(s) in 0.0770 seconds
hbase(main):014:0> exit

6、停止HBase stop-hbase.sh

7、Java客户端调用:

基本API介绍:

HBaseAdmin: 管理HBase,create and drop tables, list and alter tables
HTable:表访问
Put:新增,创建Put实例,调用HTable.put(Put)来插入数据。
Delete:删除,调用HTable.delete(Delete)
Get:查询一行(Row)数据,调用HTable.get(Get),返回Result对象,Result是一个KeyValue List,List<KeyValue>
Scan:查询多行数据,调用HTable.getScanner(Scan) ,类似cursor访问,返回 ResultScanner,调用next方法,返回行数据Result
Put,Get,Delete会锁住数据行Row

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class HBaseMainClient { public static void main(String[] args) throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "192.168.1.103"); config.set("hbase.zookeeper.property.clientPort", "2181"); HBaseAdmin admin = new HBaseAdmin(config); String tableName = "TestTable"; if (admin.tableExists(tableName)) { System.out.println("table Exists:" + tableName); } else { HTableDescriptor tableDesc = new HTableDescriptor(tableName); tableDesc.addFamily(new HColumnDescriptor("TestFamily")); admin.createTable(tableDesc); System.out.println("create table ok ."); } HTable table = new HTable(config, "TestTable"); Put p = new Put(Bytes.toBytes("TestRow")); p.add( Bytes.toBytes("TestFamily"), Bytes.toBytes("someQualifier"), Bytes.toBytes("Some Value")); table.put(p); Get g = new Get(Bytes.toBytes("TestRow")); Result r = table.get(g); byte[] value = r .getValue(Bytes.toBytes("TestFamily"), Bytes.toBytes("someQualifier")); String valueStr = Bytes.toString(value); System.out.println("GET: " + valueStr); Scan s = new Scan(); s.addColumn(Bytes.toBytes("TestFamily"), Bytes.toBytes("someQualifier")); ResultScanner scanner = table.getScanner(s); try { for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { System.out.println("Found row: " + rr); } } finally { admin.disableTable(tableName); admin.deleteTable(tableName); scanner.close(); } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值