1、下载,解压
2、修改conf/habase-site.xml文件,如下:
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
Type "exit
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
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(); } } }