2012-11-16 4 views
0

감속기를 사용하지 않고 hbase를 읽고 쓰고 싶습니다.감속기가없는 Hbase 읽기/쓰기

"Apache HBase ™ 참조 설명서"의 예제를 따르지만 예외가 있습니다. 여기

내 코드입니다 : 그런데

public class CreateHbaseIndex { 
static final String SRCTABLENAME="sourceTable"; 
static final String SRCCOLFAMILY="info"; 
static final String SRCCOL1="name"; 
static final String SRCCOL2="email"; 
static final String SRCCOL3="power"; 

static final String DSTTABLENAME="dstTable"; 
static final String DSTCOLNAME="index"; 
static final String DSTCOL1="key"; 
public static void main(String[] args) { 
    System.out.println("CreateHbaseIndex Program starts!..."); 
    try { 
     Configuration config = HBaseConfiguration.create(); 
     Scan scan = new Scan(); 
     scan.setCaching(500); 
     scan.setCacheBlocks(false); 
     scan.addColumn(Bytes.toBytes(SRCCOLFAMILY), Bytes.toBytes(SRCCOL1));//info:name 
     HBaseAdmin admin = new HBaseAdmin(config); 
     if (admin.tableExists(DSTTABLENAME)) { 
      System.out.println("table Exists."); 
     } 
     else{ 
      HTableDescriptor tableDesc = new HTableDescriptor(DSTTABLENAME); 
      tableDesc.addFamily(new HColumnDescriptor(DSTCOLNAME)); 
      admin.createTable(tableDesc); 
      System.out.println("create table ok."); 
     } 
     Job job = new Job(config, "CreateHbaseIndex"); 
     job.setJarByClass(CreateHbaseIndex.class); 
     TableMapReduceUtil.initTableMapperJob(
       SRCTABLENAME, // input HBase table name 
       scan, // Scan instance to control CF and attribute selection 
       HbaseMapper.class, // mapper 
       ImmutableBytesWritable.class, // mapper output key 
       Put.class, // mapper output value 
       job); 
     job.waitForCompletion(true); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    System.out.println("Program ends!..."); 
} 

public static class HbaseMapper extends TableMapper<ImmutableBytesWritable, Put> { 
    private HTable dstHt; 
    private Configuration dstConfig; 
    @Override 
    public void setup(Context context) throws IOException{ 
     dstConfig=HBaseConfiguration.create(); 
     dstHt = new HTable(dstConfig,SRCTABLENAME); 
    } 

    @Override 
    public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException { 
     // this is just copying the data from the source table... 
     context.write(row, resultToPut(row,value)); 
    } 

    private static Put resultToPut(ImmutableBytesWritable key, Result result) throws IOException { 
     Put put = new Put(key.get()); 
     for (KeyValue kv : result.raw()) { 
      put.add(kv); 
     } 
     return put; 
    } 

    @Override 
    protected void cleanup(Context context) throws IOException, InterruptedException { 
     dstHt.close(); 
     super.cleanup(context); 
    } 
} 
} 

, "souceTable는"이렇게 있습니다 :

key name email 
1 peter [email protected] 
2 sam  [email protected] 

"dstTable는"이렇게 될 것이다 :

key value 
peter 1 
sam 2 

I 이 분야의 초보자이고 도움이 필요합니다. Thx ~

+0

감속기는 물건을 쓰는 곳입니다. 감속기에서 hbase에 쓰고 싶지 않은 이유는 무엇입니까? –

+0

@ChrisGerken 매퍼가 필요한 모든 것을 끝낼 수 있다고 생각합니다. – zhoutall

답변

0

HBase에 쓰기 위해 감속기가 필요하지 않지만 감속기가 도움이 될 수있는 경우가 있습니다. 색인을 작성하는 경우 두 명의 맵퍼가 같은 행을 작성하려고하는 상황이 발생할 수 있습니다. 다른 열 한정자에 쓰는 것을 신중히하지 않는 한 경쟁 조건으로 인해 한 업데이트를 다른 업데이트로 덮어 쓸 수 있습니다. HBase는 행 레벨 잠금을 수행하지만 응용 프로그램 논리에 결함이 있으면 도움이되지 않습니다.

예외를 보지 않고 원본 테이블의 키 - 값 쌍을 인덱스 테이블에 쓰려고하는데 열 패밀리가 존재하지 않기 때문에 실패한 것 같습니다.

0

이 코드에서는 출력 형식을 지정하지 않습니다. 다음과 같은 코드 또한

job.setOutputFormatClass(TableOutputFormat.class); 

    job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, 
      DSTTABLENAME); 

를 추가 할 필요가, 우리가 새로운 구성을 만들 안되는 설정, 우리는 상황에서 동일한 구성을 사용해야합니다.