您的位置:首页 > 编程语言 > Java开发

通过Java Api对Hbase进行操作

2016-11-03 22:37 375 查看
HBase提供了Java Api的访问接口,所以可以通过java实现HBase中的各种操作

表结构见上一个博客这里写链接内容

package GID.AID;
import java.io.IOException;
import java.nio.ByteBuffer;
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.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
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;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class HBase {
private static Configuration conf = null;
private static HBaseAdmin admin = null;
private static Logger log = Logger.getLogger(App.class);
private static String hTableName = "hbase_1102";
private static String[] families = {"cf1", "cf2"};

@SuppressWarnings("deprecation")
public static void main( String[] args ) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
// 初始化log模块,这是hbase-client的依赖带来的额外工作
initLog();
// 设定conf,指定ZooKeeper的位置
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "vm10-0-0-2.ksc.com");
// 建立一个角色,登陆到ZooKeeper上
admin = new HBaseAdmin(conf);
log.info("Log in");

// Ex2 使用表存在、删除
doExercise2();
// Ex3 createTable
doExercise3();
// Ex4读写数据
doExercise4();
//删除一条数据
doExercise5();
//查询所有数据
doExercise6();
// 关闭连接——很重要,不然服务会挂
admin.close();
}

//查找所有数据
private static void doExercise6() throws IOException {
//插入一条数据
if (tableExist(hTableName)){
getAllRow(hTableName);
}
}
//删除一条数据
private static void doExercise5() throws IOException {
//插入一条数据
if (tableExist(hTableName)){
deleteRow(hTableName, "003");
}
}

//添加一条数据
private static void doExercise4() throws IOException {
//插入一条数据
if (tableExist(hTableName)){
createData(hTableName, "003", "cf1", "name", "Curry");
}
}

private static void doExercise2() throws IOException {
Boolean bExist = admin.tableExists(hTableName);
log.info("Table Exists? " + bExist);
if (true == bExist){
admin.disableTable(hTableName);
admin.deleteTable(hTableName);
}
}

private static void doExercise3() throws IOException{
if (tableExist(hTableName)){
return;
}
createTable(hTableName, families);
}

private static boolean tableExist(String tableName) throws IOException{
return admin.tableExists(hTableName);
}

private static void createTable(String tableName, String[] families) throws IOException {
@SuppressWarnings("deprecation")
HTableDescriptor desc = new HTableDescriptor("hbase_1102_chenweican");
for (int iFamily = 0; iFamily < families.length; iFamily++){
HColumnDescriptor columnFamilyDesc = new HColumnDescriptor(families[iFamily]);
desc.addFamily(columnFamilyDesc);
}
admin.createTable(desc);
}

//添加一条数据
private static void createData(String tableName,String rowKey,String columnFamily,
String column, String value) throws IOException{
Put put = new Put(Bytes.toBytes(rowKey));
HTable table = new HTable(conf, tableName);
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
}

//删除一条数据
private static void deleteRow(String tableName,String rowKey) throws IOException{
HTable table = new HTable(conf, tableName);
Delete del = new Delete(Bytes.toBytes(rowKey));
table.delete(del);
}

private static void getAllRow(String tableName) throws IOException {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
for(Result result:results){
for(KeyValue rowKV : result.raw()){
System.out.print("行名:" + new String(rowKV.getRow()) + " ");
System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
System.out.println("值:" + new String(rowKV.getValue()));
}
}
}

private static void initLog(){
BasicConfigurator.configure();
log.setLevel(Level.INFO);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  api hbase Hbase-java