您的位置:首页 > 其它

HBase API——封装成工具类Utils

2015-03-11 18:03 363 查看
前面介绍了HBase的常用API并给出了实例:HBase之Java API

为了便于以后使用,把对HBase表的常见CRUD操作,封装在一个类HBaseUtil里面,实现为:

import java.io.IOException;
import java.util.HashMap;

import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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;

public class HBaseUtil {

//禁用表
public void disableTable(HBaseAdmin admin, String table)  {
try {
admin.disableTable(table);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//删除表
public void dropTable(HBaseAdmin admin, String tableName)
{
if (existsTable(admin, tableName)) {
disableTable(admin, tableName);
try {
admin.deleteTable(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

//判定表是否存在
public static boolean existsTable(HBaseAdmin admin, String tableName) {
try {
return admin.tableExists(tableName.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}

// 创建数据库
public static void createTable(HBaseAdmin ha, String tableName, String cf) {
HTableDescriptor hd = new HTableDescriptor(tableName);
HColumnDescriptor hc = new HColumnDescriptor(cf);
//	hc.setMaxVersions(10);
hd.addFamily(hc);
try {
ha.createTable(hd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static Result getValues(HTableInterface htable, String rowkey,
String cf) {
Result result = null;
Get get = new Get(rowkey.getBytes());
get.addFamily(cf.getBytes());
try {
result = htable.get(get);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

// 插入hbase中获得数据,傳入表名tableName,行键rowkey,列族cf,列名column,得到值value.
public static String getValue(HTableInterface htable,
String rowkey, String cf, String column) {
Get get = new Get(rowkey.getBytes());
get.addColumn(cf.getBytes(), column.getBytes());
String createTime = null;
try {
Result result = htable.get(get);
if (result.value() != null) {
createTime = new String(result.value());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return createTime;
}

// 插入數據到Hbase中,傳入表名tableName,行键rowkey,列族cf,列名column,值value.
public static void putToHBase(HTableInterface htable,
String rowkey, String cf, String column, String value) {

Put put = new Put(rowkey.getBytes());
put.add(cf.getBytes(), column.getBytes(), value.getBytes());
try {
htable.put(put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void putOVC(HTableInterface htable,String rowkey, String cf,String column,int value,String day){
// 得到day的时间戳
Long ts = DateFormatUtil.formatStringTimeToLong(day);
Put put=new Put(Bytes.toBytes(rowkey));
// put.add(cf,column,timestamp,value)
put.add(Bytes.toBytes(cf),Bytes.toBytes(column),ts,Bytes.toBytes(value+""));
try {
htable.put(put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void scanOVC( HTableInterface htable, String cf,String colum,String day,HashMap<String,Integer> sumDevice_map)
throws IOException {
Long timeStamp = DateFormatUtil.formatStringTimeToLong(day)-86400000;
Scan scan = new Scan();
scan.setTimeStamp(timeStamp);
scan.addColumn(Bytes.toBytes(cf), Bytes.toBytes(colum));
ResultScanner scanner = htable.getScanner(scan);
for(Result res:scanner){
KeyValue[] keyvalue = res.raw();
for (KeyValue kv : keyvalue) {
sumDevice_map.put(new String(kv.getRow()), Integer.parseInt(new String(kv.getValue())));
}
}
scanner.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: