hadoop-HBase-map/reduce调用HBase及Counter

hbase(main):008:0> describe 'users'
DESCRIPTION                                                            ENABLED                               
 {NAME => 'users', FAMILIES => [{NAME => 'info', BLOOMFILTER => 'NONE' true                                  
 , REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', M                                       
 IN_VERSIONS => '0', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEM                                       
 ORY => 'false', BLOCKCACHE => 'true'}]}                                                                     
1 row(s) in 0.0610 seconds



hbase(main):009:0> scan 'users'
ROW                          COLUMN+CELL                                                                     
 id01                        column=info:email, timestamp=1413963413002, value=shakespeare@xx.com                 
 id01                        column=info:name, timestamp=1413963413002, value=shakespeare                           
 id01                        column=info:password, timestamp=1413963413002, value=123456                     
 id01                        column=info:user, timestamp=1413963413002, value=id01                           
 id02                        column=info:email, timestamp=1413963413014, value=111111@qq.com                
 id02                        column=info:name, timestamp=1413963413014, value=111111                     
 id02                        column=info:password, timestamp=1413963413014, value=abcdef                    
 id02                        column=info:user, timestamp=1413963413014, value=id02                           
2 row(s) in 0.0540 seconds




package test.hbase.inaction.example3_5;



import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;


import test.hbase.inaction.example2_4.UsersDAO;


public class CountShakespeare {


public static class Map extends TableMapper<Text, LongWritable> {

public static enum Counters { ROWS, SHAKESPEAREAN };

public static String KEY_WORD = "shakespeare";

protected void map(ImmutableBytesWritable rowkey, Result result, Context context) {
byte[] b = result.getColumnLatest(UsersDAO.INFO_FAM, UsersDAO.EMAIL_COL).getValue();
String msg = Bytes.toString(b);
if (msg != null && !msg.isEmpty()) {
context.getCounter(Counters.ROWS).increment(1);
if (msg.indexOf(KEY_WORD) != -1) {
context.getCounter(Counters.SHAKESPEAREAN).increment(1L);
}
}
}
}


//  953  /home/hadoop/hbase-0.92.1/bin/stop-hbase.sh 
//  954  /home/u/hadoop-1.0.1/bin/stop-all.sh 
//  956  /home/u/hadoop-1.0.1/bin/start-all.sh 
//  959  /home/u/hadoop-1.0.1/bin/hadoop dfsadmin -safemode leave
//  960  /home/hadoop/hbase-0.92.1/bin/start-hbase.sh
//  961  /home/hadoop/hbase-0.92.1/bin/hbase shell
//  962  jps 
 
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = HBaseConfiguration.create();

conf = HBaseConfiguration.create();
// 必须指定hbase.zookeeper.quorum属性,机器名或IP,亦可在cfg中声明
conf.set("hbase.zookeeper.quorum", "hadoop-PowerEdge-R410");
// conf.set("hbase.master", "172.10.10.144:60000");

// conf.set("fs.default.name", "hdfs://172.10.10.144:9000/");
// conf.set("hadoop.job.user", "hadoop");
conf.set("mapred.job.tracker", "172.10.10.144:9001");

// conf.set("tmpjars", "/user/hbase-0.92.1.jar");
// System.out.println(conf.get("tmpjars"));

Job job = new Job(conf, "CountShakespeare Job");
job.setJarByClass(CountShakespeare.class);
Scan scan = new Scan();
scan.addColumn(UsersDAO.INFO_FAM, UsersDAO.EMAIL_COL);
String tabName = Bytes.toString(UsersDAO.TABLE_NAME);
TableMapReduceUtil.initTableMapperJob(tabName, scan, CountShakespeare.Map.class, ImmutableBytesWritable.class, Result.class, job);
job.setOutputFormatClass(NullOutputFormat.class);
job.setNumReduceTasks(0);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值