HBase API——封装成工具类Utils
2015-03-11 18:03
363 查看
前面介绍了HBase的常用API并给出了实例:HBase之Java API
为了便于以后使用,把对HBase表的常见CRUD操作,封装在一个类HBaseUtil里面,实现为:
为了便于以后使用,把对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(); } }
相关文章推荐
- utils部分--一些通用的工具类封装
- ShareSDKUtils,封装的分享工具类,使用默认的GUI或者自定义分享某个平台
- Lucene5学习之LuceneUtils工具类简单封装
- utils部分--一些通用的工具类封装
- Lucene5学习之LuceneUtils工具类简单封装的例子
- 封装类与json的转换的工具类JsonUtils
- ShareSDKUtils,封装的分享工具类
- utils部分--一些通用的工具类封装
- Lucene5学习之LuceneUtils工具类简单封装
- Lucene5学习之LuceneUtils工具类简单封装
- ImageLoder封装工具类..
- ListView和GridView根据item数量自适应高度的工具类封装
- JDBCUtils 工具类
- BankCardUtils(根据银行卡号 获取 银行卡类型、银行名称和银行编码 自动格式化银行卡号、手机号、身份证号输入的工具类)
- Android 封装工具类之文件缓存
- php封装pdo操作数据的工具类
- Java常见工具类封装
- HttpURLConnection 单独封装网络请求工具类
- File文件工具类封装
- SQLite的工具类 ---- 通过反射把Cursor封装到VO对象