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
- HBase的javaAPI的增删改查的基本操作实现
- HBase Shell 基本的增删改查CRUD操作
- 关于hbase增删改查的操作
- Hbase 的java 增删改查操作
- 链表的基本操作的实现---- 增删改查
- Dao接口实现基本的数据库操作。增删改查
- MyBatis的增删改查基本操作
- Ibatis基本的增删改查操作
- ldap的增删改查基本操作
- Mybatis 增删改查的基本操作
- mysql基本的增删改查操作
- mysql 基本的增删查改操作
- mysql操作的基本增删改查
- mongoDB-- 基本的操作增删改查
- 单链表的增删改查基本操作
- 双链表的增删改查基本操作
- 栈的增删改查基本操作
- js实现的增删改查操作
- 重拾编程之路--移位运算&按位运算运算
- 有用的资料:光场相关Light Field(不定时更新)
- Homework1-15
- C++:友元3(复数的加法)
- freemark如何判空容错
- HBase的javaAPI的增删改查的基本操作实现
- ubuntu14.04 caffe配置中cudnn的配置
- linux 挂载硬盘,创建新分区,删除分区
- 数据结构之数组和字符串的反转java实现
- [数据挖掘]离群点检测---基于kNN的离群点检测、LOF算法和CLOF算法
- HDU 1713 相遇周期【这也算数学吗?】
- 【应用】归并有序表
- 如何自己创建Servlet并配置web.xml
- 自学BootStrap(第一季)