메뉴 건너뛰기

Cloudera, BigData, Semantic IoT, Hadoop, NoSQL

Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.


* 호출하는 쪽에서 사용하는 예시

if(paging) {
   // pageNumber를 100개 이내로 제한한다.
   if(pageNumber <= 0 || pageNumber >= 100) throw new NotProperRangeException("pageNumber is not in proper ranges(0 < pageNumber < 100)");
   rows=StoreUtil.listScanWithPaging(conn, tableName,Bytes.toBytes(startRow),Bytes.toBytes(stopRow), pageNumber, pageSize);
  } else {
   // maxRows를 10000개 이내로 제한한다.
   if(maxRows <= 0 || maxRows >= 10000) throw new NotProperRangeException("maxRows is not in proper ranges(0 < maxRows < 10000)");
   rows=StoreUtil.listScan(conn, tableName,Bytes.toBytes(startRow),Bytes.toBytes(stopRow),maxRows);
  }



------------------------------StoreUtil.java에 포함되는 메서드중 일부 --------------------------------

 // scan with  paging
 static public List<Map<String, byte[]>> listScanWithPaging(HConnection conn, String tableName, byte[] startRow, byte[] stopRow, int pageNumber, int pageSize) throws Exception {
  HTableInterface table = null;
  try {
   table=conn.getTable(Bytes.toBytes(tableName));
   Scan scan=new Scan(startRow);
   if(stopRow!=null)
    scan.setStopRow(stopRow);

   ResultScanner scanner = table.getScanner(scan);
   
   List<Map<String, byte[]>> rows=new LinkedList<Map<String, byte[]>>();
   int skipCnt = 1;
   int rowCnt = 1;
   
   // 잘못된 값이 들어오면 0건 return
   if(pageNumber <= 0 || pageSize <= 0) return rows;
   
   if(pageNumber == 1) pageNumber = 2;
   
   try {
    for (Result rs : scanner) {
     if(skipCnt++ <= ((pageNumber-1) * pageSize)) {
      //System.out.println("skipCnt  == > ["+(skipCnt-1)+"]");
      continue;
     } else {
      //System.out.println("includeCnt  == > ["+(skipCnt-1)+"]");
     }
     
     // 지정한 수만큼 row를 뽑아냄
     if(rowCnt++ > pageSize) break;
     
     Map<String, byte[]> m=new LinkedHashMap<String, byte[]>();
     m.put("rowId", rs.getRow());
           NavigableMap<byte[], NavigableMap<byte[], byte[]>> familyQualifierMap = rs.getNoVersionMap();
           for (byte[] familyBytes : familyQualifierMap.keySet()) {
               NavigableMap<byte[], byte[]> qualifierMap = familyQualifierMap.get(familyBytes);
               for (byte[] qualifier : qualifierMap.keySet())
                m.put(Bytes.toString(qualifier), qualifierMap.get(qualifier));
           }
     rows.add(m);
    }
   } finally {
    scanner.close();
   }
   return rows;
  } finally {
   if(table!=null)table.close();
  }
 }
 
 // count
 static public long getCount(HConnection conn, String tableName, String startRow, String stopRow) throws Exception {
  HTableInterface table = null;
  try {
   if(startRow == null || startRow.equals("")) throw new NullPointerException("startRow is null or '' ");
   if(stopRow == null || stopRow.equals("")) throw new NullPointerException("stopRow is null or '' ");
   
   table=conn.getTable(Bytes.toBytes(tableName));
   Scan scan=new Scan(Bytes.toBytes(startRow));
   if(stopRow!=null)
    scan.setStopRow(Bytes.toBytes(stopRow));

   ResultScanner scanner = table.getScanner(scan);
   
   long cnt=0L;
   try {
    for (Result rs = scanner.next(); rs != null; rs = scanner.next()) {
        cnt++;
    }   
   } finally {
    scanner.close();
   }
   return cnt;
  } finally {
   if(table!=null)table.close();
  }
 }

번호 제목 날짜 조회 수
29 HBase shell로 작업하기 2013.03.15 5926
28 하둡 분산 파일 시스템을 기반으로 색인하고 검색하기 2013.03.15 5690
27 HBase 설치하기 – Fully-distributed 2013.03.12 3795
26 HBASE Client API : 기본 기능 정리 file 2013.04.01 3697
25 Hbase Shell 명령 정리 2013.04.01 3301
24 org.apache.hadoop.hbase.PleaseHoldException: Master is initializing 2013.03.15 2879
23 HBase 설치하기 – Pseudo-distributed file 2013.03.12 2822
22 HBase, BigTable, Cassandra Schema Design file 2013.03.15 2699
21 hbase shell에서 컬럼값 검색하기(SingleColumnValueFilter이용) 2014.04.25 2603
20 hbase에 필요한 jar들 2013.04.01 2189
19 hbase shell 필드 검색 방법 2015.05.24 2124
18 HBase 0.98.12(1.2.5) for hadoop2 설치-5대에 완전분산모드 (HDFS HA상테) 2015.04.29 1242
17 Current heap configuration for MemStore and BlockCache exceeds the threshold required for successful cluster operation 2017.07.18 1158
16 column family삭제시 Column family 'delete' does not exist오류 발생하는 경우 2014.04.14 1032
15 secureCRT에서 backspace키가 작동하지 않는 경우 해결방법 2015.05.11 840
14 SASL configuration failed: javax.security.auth.login.LoginException: java.lang.NullPointerException 오류 해결방법 2015.04.02 803
13 Flume을 이용한 데이타 수집시 HBase write 성능 튜닝 file 2016.10.31 724
12 scan의 startrow, stoprow지정하는 방법 2015.04.08 559
11 Hadoop의 Datanode를 Decommission하고 나서 HBase의 regionservers파일에 해당 노드명을 지웠는데 여전히 "Dead regionser"로 표시되는 경우 처리 2018.01.25 494
» Hbase API를 이용하여 scan시 페이징을 고려하여 목록을 가져올때 사용할 수 있는 로직의 예시를 보여줌 2017.04.26 460
위로