hbase的Java簡單應用

hbase0.20.6 + hadoop0.20.2

需要的jar包爲:

hadoop-0.20.2-core.jar

hbase-0.20.6.jar

zookeeper-3.2.2.jar

log4j-1.2.15.jar

commons-logging-1.0.4.jar


初始化操作:

1、先new一個configuration(hadoop);

2、賦予zookeeper的IP地址、客戶端訪問端口;

3、得到HbaseConfiguration;

Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set("hbase.zookeeper.quorum", ip);
HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", String.valueOf(port));
cfg = new HBaseConfiguration(HBASE_CONFIG);


創建表操作:

1、得到HbaseAdmin;

2、new一個表名的HTableDescriptor;

3、增加列族;

4、創建表;

HBaseAdmin admin = new HBaseAdmin(cfg);
HTableDescriptor tableDesc = new HTableDescriptor("tablename");
tableDesc.addFamily(new HColumnDescriptor("columnFamilyName"));
admin.createTable(tableDesc);


插入/更新數據:

1、通過上面的cfg、表名,得到HTable;

2、根據一個rowkey,得到put對象;

3、將對應列族、對應列的值,存入;

4、增加/更新到表中;

HTable table = new HTable(cfg, "tableName");
Put put = new Put(Bytes.toBytes("rowKey"));
put.add(Bytes.toBytes("columnFamilyName"), Bytes.toBytes("columnName"),
		Bytes.toBytes("value"));
table.put(put);


判斷表是否存在:

admin.tableExists("tableName")


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章