HBase基本代码实现(二)
来源:互联网 发布:patagonia c4 淘宝哪家 编辑:程序博客网 时间:2024/06/06 17:00
Step 1:
public void deleteCell(String tableName, String rowkey,String cf,String column){ HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; Delete del = new Delete(rowkey.getBytes()); del.deleteColumn(cf.getBytes(), column.getBytes()); table.delete(del); } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } }
Step 2:
public void createTable(String tableName, String[] columnFamilys){ try { // admin 对象 HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { System.err.println("此表,已存在!"); } else { HTableDescriptor tableDesc = new HTableDescriptor( TableName.valueOf(tableName)); for (String columnFamily : columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } admin.createTable(tableDesc); System.err.println("建表成功!"); } admin.close();// 关闭释放资源 } catch (MasterNotRunningException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ZooKeeperConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Step 3:
/** * 删除一个表 * * @param tableName * 删除的表名 * */ public void deleteTable(String tableName) { try { HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { admin.disableTable(tableName);// 禁用表 admin.deleteTable(tableName);// 删除表 System.err.println("删除表成功!"); } else { System.err.println("删除的表不存在!"); } admin.close(); } catch (MasterNotRunningException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ZooKeeperConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Step 4:
/** * 查询表中所有行 * @param tablename */ public void scaner(String tablename) { try { HTable table =new HTable(conf, tablename); Scan s =new Scan();// s.addColumn(family, qualifier)// s.addColumn(family, qualifier) ResultScanner rs = table.getScanner(s); for (Result r : rs) { for(Cell cell:r.rawCells()){ System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); System.out.println("Timetamp:"+cell.getTimestamp()+" "); System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); } } } catch (IOException e) { e.printStackTrace(); } }
Step 5:
public void scanerByColumn(String tablename) { try { HTable table =new HTable(conf, tablename); Scan s =new Scan(); s.addColumn("cf".getBytes(), "201504052237".getBytes()); s.addColumn("cf".getBytes(), "201504052237".getBytes()); ResultScanner rs = table.getScanner(s); for (Result r : rs) { for(Cell cell:r.rawCells()){ System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); System.out.println("Timetamp:"+cell.getTimestamp()+" "); System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); } } } catch (IOException e) { e.printStackTrace(); } }
Step 6:
public static void main(String[] args) {// 创建表// String tableName="test";// String cfs[] = {"cf"};// dao.createTable(tableName,cfs);// 存入一条数据// Put put = new Put("bjsxt".getBytes());// put.add("cf".getBytes(), "name".getBytes(), "cai10".getBytes()) ;// dao.save(put, "test") ;// 插入多列数据// Put put = new Put("bjsxt".getBytes());// List<Put> list = new ArrayList<Put>();// put.add("cf".getBytes(), "addr".getBytes(), "shanghai1".getBytes()) ;// put.add("cf".getBytes(), "age".getBytes(), "30".getBytes()) ;// put.add("cf".getBytes(), "tel".getBytes(), "13889891818".getBytes()) ;// list.add(put) ;// dao.save(list, "test");// 插入单行数据// dao.insert("test", "testrow", "cf", "age", "35") ;// dao.insert("test", "testrow", "cf", "cardid", "12312312335") ;// dao.insert("test", "testrow", "cf", "tel", "13512312345") ;// List<Result> list = dao.getRows("test", "testrow",new String[]{"age"}) ;// for(Result rs : list)// {// for(Cell cell:rs.rawCells()){ // System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");// System.out.println("Timetamp:"+cell.getTimestamp()+" ");// System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");// System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");// System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");// }// }// Result rs = dao.getOneRow("test", "testrow");// System.out.println(new String(rs.getValue("cf".getBytes(), "age".getBytes())));// Result rs = dao.getOneRowAndMultiColumn("cell_monitor_table", "29448-513332015-04-05", new String[]{"201504052236","201504052237"});// for(Cell cell:rs.rawCells()){ // System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");// System.out.println("Timetamp:"+cell.getTimestamp()+" ");// System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");// System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");// System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");// }// dao.deleteTable("cell_monitor_table");// 创建表 String tableName="cell_monitor_table"; String cfs[] = {"cf"};// dao.createTable(tableName,cfs); }
Step 7:
public static void testRowFilter(String tableName){ try { HTable table =new HTable(conf, tableName); Scan scan = new Scan(); scan.addColumn(Bytes.toBytes("column1"),Bytes.toBytes("qqqq")); Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("laoxia157"))); scan.setFilter(filter1); ResultScanner scanner1 = table.getScanner(scan); for (Result res : scanner1) { System.out.println(res); } scanner1.close();// // Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("laoxia4\\d{2}"));// scan.setFilter(filter2);// ResultScanner scanner2 = table.getScanner(scan);// for (Result res : scanner2) {// System.out.println(res);// }// scanner2.close(); Filter filter3 = new RowFilter( CompareOp.EQUAL,new SubstringComparator("laoxia407")); scan.setFilter(filter3); ResultScanner scanner3 = table.getScanner(scan); for (Result res : scanner3) { System.out.println(res); } scanner3.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Step 8:
@Test public void testTrasaction(){ try{ HTableInterface table = null; table = hTablePool.getTable("t_test".getBytes());// Put put1 =new Put("002".getBytes());// put1.add("cf1".getBytes(), "name".getBytes(), "王五".getBytes());// table.put(put1); Put newput =new Put("001".getBytes()); newput.add("cf1".getBytes(), "like".getBytes(), "看书".getBytes()); boolean f= table.checkAndPut("001".getBytes(), "cf1".getBytes(), "age".getBytes(), "24".getBytes(), newput); System.out.println(f); }catch (Exception e){ e.printStackTrace(); } }
阅读全文
0 0
- HBase基本代码实现(二)
- HBase基本代码实现(一)
- HBase基本用法 (二)
- 【hbase】HBase基本数据操作代码实现详解
- HBase学习(二) HBase Shell 操作基本命令
- TabHost基本实现(二)
- Hbase笔记二之Hbase Shell基本操作
- Hbase分页查询代码实现
- Hbase基础及代码实现
- 二叉树学习(二):二叉树的基本操作及代码实现
- hbase 0.94一些基本操作测试代码
- HBase-1.2.4LruBlockCache实现分析(二)
- HBase系列(二)
- HBase(二)
- hbase总结(二)-hbase安装
- hbase总结(二)-hbase安装
- HBase(二)hbase shell常用语法
- HBase学习笔记(二)HBase架构
- javascript模块化之CommonJS、AMD、CMD、UMD、ES6
- JZOJ1094【SDOI2009】最优图像
- Retrofit 源码分析
- 树状数组(oj1978 & 1979)
- Android开发官方语言-Kotlin学习资源汇总
- HBase基本代码实现(二)
- IOS接入微信需要导入的库
- Android RecyclerView工作原理分析(上)
- Android中匿名共享内存demo
- 两层布局如何让下层布局不能点击
- 如何优雅的在Visual Studio中配置log4cplus
- getField 和 getDeclaredField 的区别
- 机器学习中的偏差和方差
- Oracle(创建序列)