Ejemplo n.º 1
0
  public static void main(String[] args) throws Exception {
    //		FileReader reader = new FileReader(new File("C:\\Users\\Thor\\Desktop\\test.txt"));
    //		BufferedReader br = new BufferedReader(reader);
    //		String data = "";
    //		while(null != br.readLine()){
    //			data += br.readLine();
    //		}
    //		br.close();
    /**
     * 查询所有表名 List<HTableDescriptor> tableList = hbase.getALLTable(); if(null != tableList &&
     * !tableList.isEmpty()){ for (HTableDescriptor tableDesc : tableList) {
     * System.out.println(tableDesc.getNameAsString()); } }
     */

    /**
     * 创建表 HTableDescriptor table = new HTableDescriptor(TableName.valueOf("test"));
     * table.addFamily(new HColumnDescriptor(Bytes.toBytes("cf1"))); hbase.createTable(table);
     */

    /** 往表中添加一条记录 */
    // 一个PUT代表一行,构造函数传入的是RowKey
    //		String.valueOf(System.currentTimeMillis());
    //		Put put = new Put(Bytes.toBytes("aa"));
    //		put.addColumn("info".getBytes(), "url".getBytes(), "www.baidu.com".getBytes());
    //		put.addColumn("info".getBytes(), "content".getBytes(), "test".getBytes());
    //		put.addColumn("info".getBytes(), "createTime".getBytes(), "2016-06-06 16:50:21".getBytes());
    //		HBaseUtil.insertOneRecord("spider_distributed", put);
    //		String.valueOf(System.currentTimeMillis());
    /*List<Put> puts = new ArrayList<Put>();
    //多增加一行
    Put put_2 = new Put((String.valueOf(System.currentTimeMillis())).getBytes());
    put_2.addColumn("test_1".getBytes(), null, "这是第二行第一列的数据".getBytes());
    put_2.addColumn("test_2".getBytes(), null, "这是第二行第二列的数据".getBytes());
    put_2.addColumn("test_3".getBytes(), null, "这是第二行第三列的数据".getBytes());
    //多增加一行
    Put put_3 = new Put((String.valueOf(System.currentTimeMillis())).getBytes());
    put_3.addColumn("test_1".getBytes(), null, "这是第三行第一列的数据".getBytes());
    put_3.addColumn("test_2".getBytes(), null, "这是第三行第二列的数据".getBytes());
    put_3.addColumn("test_3".getBytes(), null, "这是第三行第三列的数据".getBytes());
    puts.add(put_2);
    puts.add(put_3);
    hbase.addRecords("test", puts);*/
    //		HColumnDescriptor family = new HColumnDescriptor("test");
    //		HBaseUtil.addColumnFamily("table1", family);
    //		Put put = new Put(Bytes.toBytes("rk1"));
    //		put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("age"), Bytes.toBytes("25"));
    //		HBaseUtil.insertOneRecord("table1", put);
    Result result2 = HBaseUtil.queryByRowKey("cf2".getBytes());
    System.out.println(result2);

    /**
     * 查询表 PaginationResult<List<Result>> result = HBaseUtil.queryTableByPagination("table1", new
     * Pagination(5, 1)); if(null != result){ for(Result rs : result.getR()){
     * System.out.println("该表RowKey为:"+new String(rs.getRow())); for(Cell cell : rs.rawCells()){
     * System.out.println("列簇为:"+new String(CellUtil.cloneFamily(cell)));
     * System.out.println("列修饰符为:"+new String(CellUtil.cloneQualifier(cell)));
     * System.out.println("值为:"+new String(CellUtil.cloneValue(cell))); }
     * System.out.println("=========================================="); } }else{
     * System.out.println("no data"); }
     */

    /**
     * 协处理器 alter 'mytable', METHOD =>
     * 'table_att','coprocessor'=>'|org.apache.hadoop.hbase.coprocessor.AggregateImplementation||'
     *
     * <p>AggregationClient aggregationClient = new AggregationClient(conf); Scan scan = new Scan();
     * try { Long rowCount = aggregationClient.rowCount(TableName.valueOf("spider_distributed"), new
     * LongColumnInterpreter(), scan); aggregationClient.close();
     * System.out.println(rowCount.intValue()); } catch (Throwable e) { e.printStackTrace(); }
     */

    /**
     * 分页 Scan scan = new Scan(); scan.setMaxResultSize(5l); // MUST_PASS_ALL(条件 AND)
     * MUST_PASS_ONE(条件OR) FilterList filterList = new
     * FilterList(FilterList.Operator.MUST_PASS_ALL); int pageSize = 5; int currentPage = 1; int
     * nowPageSize = pageSize + 1; Filter filter1 = new PageFilter(pageSize);
     * filterList.addFilter(filter1); scan.setFilter(filterList);
     * scan.setStartRow(Bytes.toBytes("f24ff009cbbcc2ca1bd15c22fefded14")); Connection conn =
     * ConnectionFactory.createConnection(conf); HTable table = (HTable)
     * conn.getTable(TableName.valueOf("spider_distributed")); ResultScanner scanner =
     * table.getScanner(scan); for (Result result : scanner) { System.out.println(new
     * String(result.getRow())); for (Cell cell : result.rawCells()) { System.out.println("列族:"+new
     * String(CellUtil.cloneQualifier(cell))+">>>"+new String(CellUtil.cloneValue(cell))); } }
     */
  }
Ejemplo n.º 2
0
 /**
  * 查询一张表中所有记录 分页
  *
  * @param tableName
  * @return
  * @throws ParamIsNullException 参数为空
  */
 public static PaginationResult<List<Result>> queryTableByPagination(
     String tableName, Pagination pagination) throws ParamIsNullException {
   if (null == tableName) {
     throw new ParamIsNullException("tableName不能为空");
   }
   Connection conn = null;
   HTable table = null;
   ResultScanner scann = null;
   PaginationResult<List<Result>> result = new PaginationResult<List<Result>>();
   List<Result> resultList = new ArrayList<Result>();
   int currentPage = 1;
   try {
     conn = ConnectionFactory.createConnection(conf);
     table = (HTable) conn.getTable(TableName.valueOf(tableName));
     int pagesize = 10;
     Scan scan = new Scan();
     if (null != pagination) {
       pagesize = pagination.getPagesize();
       if (null != pagination.getNextPageRowKey() && pagination.getNextPageRowKey().length > 0) {
         scan.setStartRow(pagination.getNextPageRowKey());
       }
       currentPage = pagination.getCurrentPage();
     }
     // MUST_PASS_ALL(条件 AND) MUST_PASS_ONE(条件OR)
     FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
     Filter filter = new PageFilter(pagesize);
     filterList.addFilter(filter);
     scan.setFilter(filterList);
     scann = table.getScanner(scan);
     for (Result rs : scann) {
       resultList.add(rs);
     }
     /** 设置Pagination */
     Pagination page = new Pagination();
     Result row = resultList.get(resultList.size() - 1 > -1 ? resultList.size() - 1 : 0);
     page.setNextPageRowKey(row.getRow());
     page.setPagesize(pagesize);
     page.setTotalRows(
         null == HBaseUtil.getRowCount(tableName)
             ? 0
             : HBaseUtil.getRowCount(tableName).intValue());
     page.setCurrentPage(currentPage);
     result.setR(resultList);
     result.setPagination(page);
   } catch (Exception e) {
     logger.error("获取Hbase连接发生异常, errMsg:{}", e.getMessage());
   } finally {
     if (null != scann) {
       scann.close();
     }
     if (null != table) {
       try {
         table.close();
       } catch (IOException e) {
         logger.error("HTable close exception, errMsg:{}", e.getMessage());
       }
     }
     if (null != conn) {
       try {
         conn.close();
       } catch (IOException e) {
         logger.error("Connection close exception, errMsg:{}", e.getMessage());
       }
     }
   }
   return result;
 }