一个计算HBase表中行数的MapReduce程序

public  class RowCounter{

 static  final String  NAME="rowcounter";

static class RowCounterMapper  extends TableMapper<ImmutableBytesWritable ,Result>{

public  static  enum Counters {ROWS}

@overrideion 

public  void  map(ImmutableBytesWritable row, Result values, Context contet ){

for(KeyVlaue  value : values.list()){

  if(value.getValue().length >0){

     context.getCounter(Counters.ROWS).increments(1);

break

}}}}

public static Job  createSubmitatableJob(Configuration conf, String [] args)

  String tableName = args[0];

Job job= new Job(conf, NAME +"_"+tableName);

job.setJarByClASS(RowConter.class)

StringBuilder sb= new StringBuilder();

final int  columnoffset= 1;

for(int i=columnoffset; i<args.length;i++){

   if(i > columnoffset){

sb.append(" ");

}

 sb.append(args[i)

}

Scan scan = new Scan();

scan.setFilter (new FirstKeyOnlyFilter());

if(sb.length>0){

   for (String columnName : sb.toString().split(" "){

    String [] fields = columnName.split(" :");

if(filds.length ==1 ){

 scan.addFamily(Bytes.toBytes(fields[0])

else{

  scan.addColmn(Bytes.toBytes(fields[0],Bytes.toBytes(fields[1]);}}

//Second argument is the table name

job.setOutputFormatClass(NullOutputFormat.class);

TableMapRecueUtils.intiTableMapperJob(tableName, scan, RowCounterMapper.class,ImmutableBytesWritable.calss,Result.class,job);

job..setNmReduceTasks(0);

return job;

}

public static void main(String[] args) {

Configuration conf = HBaseConfiguration.create();

String[]  otherArgs = new GenericOptionsParser(conf, args).getRemainningArgs();

if(otherArgs.length<1){

 System.err.println("Usage:RowConter   talbenam[e [<clolmn1 >< column2>..]

  System.exit(-1);

job job= createSubmittableJob(conf, otherArgs);

System.exit(job.waitForCompletion(true) :0?1);

}


page= 509 hadoop权威指南







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值