hbase的Java简单应用

来源:互联网 发布:2015进出口数据 编辑:程序博客网 时间:2024/04/27 15:45

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")


 

原创粉丝点击