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

来源:互联网 发布:toefl培训知乎 编辑:程序博客网 时间:2024/06/05 10:00

/*

以下是基本的增删改查的代码 针对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