package utils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * HBase操作 工具 类:Java工具类建议采用单例模式封装 */ public class HBaseUtils { Admin admin = null; Configuration configuration = null; Connection connection = null; /** * 私有改造方法 */ private HBaseUtils(){ configuration = new Configuration(); configuration.set("hbase.zookeeper.quorum", "localhost:2181"); configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } private static HBaseUtils instance = null; public static synchronized HBaseUtils getInstance() { if(null == instance) { instance = new HBaseUtils(); } return instance; } /** * 根据表名获取到Table实例 */ public Table getTable(String tableName) { Table table = null; try { table = connection.getTable(TableName.valueOf(tableName)); } catch (IOException e) { e.printStackTrace(); } return table; } /** * 添加一条记录到HBase表 * @param tableName HBase表名 * @param rowKey HBase表的rowkey * @param cf HBase表的columnfamily * @param column HBase表的列 * @param value 写入HBase表的值 */ public void put(String tableName, String rowKey, String cf, String column, String value){ try { Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); table.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 根据行键rowkey查找数据 * @param tableName 表名 * @param rowKey 行键 * @param colFamily 列族名 * @param col 列名 * @throws IOException */ public byte[] getData(String tableName,String rowKey,String colFamily,String col){ byte[] result = null; try { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); get.addColumn(colFamily.getBytes(),col.getBytes()); result = table.get(get).value(); return result; }catch (IOException e) { e.printStackTrace(); } return result; } public static void main(String[] args) { //HTable table = HBaseUtils.getInstance().getTable("imooc_course_clickcount"); //System.out.println(table.getName().getNameAsString()); String tableName = "course_clickcount" ; String rowkey = "20171111_88"; String cf = "info" ; String column = "click_count"; String value = "2"; HBaseUtils.getInstance().put(tableName, rowkey, cf, column, value); byte[] result = HBaseUtils.getInstance().getData(tableName, rowkey, cf, column); System.out.println(new String(result)); } }4607
以上所述就是小编给大家介绍的《Hbase 简单java API 操作》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- JavaScript骚操作之操作符
- Go 语言操作 MySQL 之 事务操作
- C# 数据操作系列 - 1. SQL基础操作
- Vim 跨行操作与 Ex 命令操作范围
- 并发环境下,先操作数据库还是先操作缓存?
- 关于HBase Shell基本操作的表操作示例
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Algorithm Design Manual
Steven S Skiena / Springer / 2011-11-14 / GBP 55.07
....The most comprehensive guide to designing practical and efficient algorithms.... Written by a well-known algorithms researcher who received the IEEE Computer Science and Engineering Teaching Aw......一起来看看 《The Algorithm Design Manual》 这本书的介绍吧!