HBase的javaAPI的增删改查的基本操作实现

/*

以下是基本的增删改查的代码 针对HBAse的基本的操作

*/

import java.io.IOException;

import java.util.UUID;


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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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;


public class HBase {
public static final String TABLE_NAME = "yy";
public static final String FAMILY_NAME = "f";
public static final String ROWKEY_NAME = "rk" + Math.random();
public static final String COLUMN_NAME = "c";
public static final String VALUE = UUID.randomUUID().toString();


private static Configuration getConfiguration() {
final Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://222.27.174.67:9000/hbase");
conf.set("hbase.zookeeper.quorum", "222.27.174.66");
conf.set("hbase.zookeeper.quorum", "222.27.174.67");
return conf;
}


private static void createTable(final Configuration conf)
throws MasterNotRunningException, ZooKeeperConnectionException,
IOException {
final HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
final boolean tableExists = hBaseAdmin.tableExists(TABLE_NAME);
if (tableExists) {
System.out.println("这个表已经存在了");
} else {
HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
desc.addFamily(family);
hBaseAdmin.createTable(desc);
System.out.println("创建表成功了!");
}
}


private static void put() throws IOException {
final HTable hTable = new HTable(getConfiguration(), TABLE_NAME);
Put put = new Put(ROWKEY_NAME.getBytes());
put.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), VALUE
.getBytes());
hTable.put(put);
System.out.println(VALUE);
}


private static void get() throws IOException {
final HTable hTable = new HTable(getConfiguration(), TABLE_NAME);
Get get = new Get(ROWKEY_NAME.getBytes());
final Result result = hTable.get(get);
System.out.println(ROWKEY_NAME + "---" + result);
}


private static void scan() throws IOException {
final HTable hTable = new HTable(getConfiguration(), TABLE_NAME);
Scan scan = new Scan();
// scan.setStartRow("rk".getBytes());
// scan.setStopRow("rk0.7".getBytes());
final ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println(result);
}
}


private static void delete() throws MasterNotRunningException,
ZooKeeperConnectionException, IOException {
final HBaseAdmin hBaseAdmin = new HBaseAdmin(getConfiguration());
hBaseAdmin.disableTable(TABLE_NAME);
hBaseAdmin.deleteTable(TABLE_NAME);
}


public static void main(String[] args) throws Exception {
final Configuration conf = getConfiguration();
createTable(conf);


put();


get();


scan();


delete();
System.out.println(VALUE);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值