HBase基本代码实现(一)
2017-06-16 18:47
204 查看
Step 1: 加载设置
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Step 9:
Step 10:
HConnection hTablePool = null; static Configuration conf =null; public HBaseDAOImp() { conf = new Configuration(); String zk_list = "node12,node13,node14"; conf.set("hbase.zookeeper.quorum", zk_list); try { hTablePool = HConnectionManager.createConnection(conf) ; } catch (IOException e) { e.printStackTrace(); } }
Step 2:
public void save(Put put, String tableName) { // TODO Auto-generated method stub HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; table.put(put) ; } catch (Exception e) { e.printStackTrace() ; }finally{ try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } }
Step 3:
/** * 插入一个cell * @param tableName * @param rowKey * @param family * @param quailifer * @param value */ public void insert(String tableName, String rowKey, String family, String quailifer, String value) { // TODO Auto-generated method stub HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; Put put = new Put(rowKey.getBytes()); put.add(family.getBytes(), quailifer.getBytes(), value.getBytes()) ; table.put(put); } catch (Exception e) { e.printStackTrace(); }finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } }
Step 4:
/** * 在一个列族下插入多个单元格 * @param tableName * @param rowKey * @param family * @param quailifer * @param value */ public void insert(String tableName,String rowKey,String family,String quailifer[],String value[]) { HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; Put put = new Put(rowKey.getBytes()); // 批量添加 for (int i = 0; i < quailifer.length; i++) { String col = quailifer[i]; String val = value[i]; put.add(family.getBytes(), col.getBytes(), val.getBytes()); } table.put(put); } catch (Exception e) { e.printStackTrace(); }finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } }
Step 5:
public void save(List<Put> Put, String tableName) { // TODO Auto-generated method stub HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; table.put(Put) ; } catch (Exception e) { // TODO: handle exception }finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } }
Step 6:
public Result getOneRow(String tableName, String rowKey) { // TODO Auto-generated method stub HTableInterface table = null; Result rsResult = null; try { table = hTablePool.getTable(tableName) ; Get get = new Get(rowKey.getBytes()) ; rsResult = table.get(get) ; } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return rsResult; }
Step 7:
/** * 最常用的方法,优化查询 * 查询一行数据, * @param tableName * @param rowKey * @param cols * @return */ public Result getOneRowAndMultiColumn(String tableName, String rowKey,String[] cols) { // TODO Auto-generated method stub HTableInterface table = null; Result rsResult = null; try { table = hTablePool.getTable(tableName) ; Get get = new Get(rowKey.getBytes()) ; for (int i = 0; i < cols.length; i++) { get.addColumn("cf".getBytes(), cols[i].getBytes()) ; } rsResult = table.get(get) ; } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return rsResult; }
Step 8:
public List<Result> getRows(String tableName, String rowKeyLike) { // TODO Auto-generated method stub HTableInterface table = null; List<Result> list = null; try { FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL); table = hTablePool.getTable(tableName) ; PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); SingleColumnValueFilter filter1 = new SingleColumnValueFilter( "order".getBytes(), "order_type".getBytes(), CompareOp.EQUAL, Bytes.toBytes("1") ); fl.addFilter(filter); fl.addFilter(filter1); Scan scan = new Scan(); scan.setFilter(fl); ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rs : scanner) { list.add(rs) ; } } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; }
Step 9:
public List<Result> getRows(String tableName, String rowKeyLike ,String cols[]) { // TODO Auto-generated method stub HTableInterface table = null; List<Result> list = null; try { table = hTablePool.getTable(tableName) ; PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); Scan scan = new Scan(); for (int i = 0; i < cols.length; i++) { scan.addColumn("cf".getBytes(), cols[i].getBytes()) ; } scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rs : scanner) { list.add(rs) ; } } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; }
Step 10:
public List<Result> getRowsByOneKey(String tableName, String rowKeyLike ,String cols[]) { // TODO Auto-generated method stub HTableInterface table = null; List<Result> list = null; try { table = hTablePool.getTable(tableName) ; PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); Scan scan = new Scan(); for (int i = 0; i < cols.length; i++) { scan.addColumn("cf".getBytes(), cols[i].getBytes()) ; } scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rs : scanner) { list.add(rs) ; } } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; }
相关文章推荐
- 【hbase】HBase基本数据操作代码实现详解
- HBase基本代码实现(二)
- linux基本的聊天软件代码实现
- ReactiveSwift源码解析(十二) MutableProperty基本代码实现
- 排序--冒泡排序的基本思想及其代码实现
- 决策树基本理论学习以及Python代码实现和详细注释
- HBase1.1.2增删改查scala代码实现
- 100行代码用C++基本实现双向链表
- OpenGL+Qt界面(三) Qt中实现OPENGL的基本交互+代码
- hbase RowFilter如何根据rowkey查询以及实例实现代码
- IOS开发UI—纯代码实现控件及其基本属性
- 基本的传染病模型:SI、SIS、SIR及其Python代码实现
- Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud
- Cuckoo Hash 基本思想和代码实现
- 二叉树的基本操作 C++代码实现
- Spark访问HBase的Eclipse代码实现
- 循环队列的基本操作之代码实现_legend
- 运用ajax应用开始时基本实现功能代码
- 快速排序基本思想以及代码实现
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果