分享

Hadoop MapReduce统计结果直接输出hbase

lzw 发表于 2013-12-10 23:19:20 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 36722
本帖最后由 lzw 于 2013-12-10 23:18 编辑

MapReduce统计结果直接输出hbase,我使用的是hadoop1.0.4版本和hbase 0.94版本,hadoop和hbase安装伪分布式。1.hadoop安装这里就不讲了。
2.hbase安装我这里将一下。
首页解压habase安装包到/home/hadoop目录。
配置hosts文件如下:
  1. 192.168.0.101   hadoop.master
复制代码
配置hbase-site.xml,配置内容如下:
  1. <configuration>
  2. <property>
  3.    <name>hbase.rootdir</name>
  4.    <value>hdfs://hadoop.master:9000/hbase</value>
  5. </property>
  6. <property>
  7.    <name>hbase.cluster.distributed</name>
  8.    <value>true</value>
  9. </property>
  10. <property>
  11.   <name>hbase.zookeeper.quorum</name>
  12.   <value>hadoop.master</value>
  13. </property>
  14. <property>
  15.   <name>hbase.zookeeper.property.dataDir</name>
  16.   <value>/home/hadoop/zookeeper</value>
  17. </property>
  18. <property>
  19.   <name>hbase.regionserver.handler.count</name>
  20.   <value>100</value>
  21. </property>
  22. <property>
  23.   <name>hbase.hregion.max.filesize</name>
  24.   <value>8589934592</value>
  25. </property>
  26. <property>
  27.   <name>hfile.block.cache.size</name>
  28.   <value>0.3</value>
  29. </property>
  30. <property>
  31.   <name>dfs.replication</name>
  32.   <value>1</value>
  33. </property>
  34. </configuration>
复制代码
hbase-site.xml配置完后,在配置hbase-env.sh,我只把其中配置的如下显示:
  1. export JAVA_HOME=/usr/jdk1.6.0_22
  2. export HBASE_OPTS="-XX:+UseConcMarkSweepGC"
  3. export HBASE_MANAGES_ZK=true
复制代码
上面最后一项一定要打开。设置zookeeper管理hbase。

最后配置regionservers 如下:
  1. hadoop.master
复制代码
注意:如果需要在本地连接hbase,需要关闭防火墙,执行命令  /sbin/service iptables stop

接下来启动hbase,创建表:TestCars,列族Car:
准备数据:
  1. Acura,Integra,Small
  2. Acura,Legend,Midsize
  3. Audi,90,Compact
  4. Audi,100,Midsize
  5. BMW,535i,Midsize
  6. Buick,Century,Midsize
  7. Buick,LeSabre,Large
  8. Buick,Roadmaster,Large
  9. Buick,Riviera,Midsize
  10. Cadillac,DeVille,Large
  11. Cadillac,Seville,Midsize
复制代码
将数据上传hadoop文件系统:
  1. hadoop fs -copyfromLocal /home/hadoop/Car.txt /home/hadoop/input
复制代码
在运行mapreduce时需要将hbase-0.94.6.jar 、zookeeper-3.4.5.jar、protobuf-java-2.4.0a.jar添加到hadoop lib目录下,或者另一种方式在执行mapreduce时,导入前面三个包。
下面是实现的具体代码:
  1. package com.duplicate;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.hbase.HBaseConfiguration;
  8. import org.apache.hadoop.hbase.client.HTableInterface;
  9. import org.apache.hadoop.hbase.client.HTablePool;
  10. import org.apache.hadoop.hbase.client.Put;
  11. import org.apache.hadoop.hbase.util.Bytes;
  12. import org.apache.hadoop.io.Text;
  13. import org.apache.hadoop.mapreduce.Job;
  14. import org.apache.hadoop.mapreduce.Mapper;
  15. import org.apache.hadoop.mapreduce.Reducer;
  16. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  17. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. public class OutputHbase {
  21.         private static Logger logger = LoggerFactory.getLogger(OutputHbase.class);
  22.         
  23.         public static class Map extends Mapper<Object,Text,Text,Text>{
  24.                 private Text outKey = new Text();
  25.                 private Text outVal = new Text();
  26.                
  27.                 public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
  28.                         String[] valueSplitted = value.toString().split(",");
  29.                         if(valueSplitted.length == 3){
  30.                                 String brand = valueSplitted[0];
  31.                     String model = valueSplitted[1];
  32.                     String size = valueSplitted[2];
  33.                                 outKey.set(brand);
  34.                                 outVal.set(model + "," + size);
  35.                                 context.write(outKey, outVal);
  36.                         }
  37.                 }
  38.         }
  39.         
  40.         public static class Reduce extends Reducer<Text,Text,Text,Text>{
  41.                 private HTablePool pool = null;
  42.                 private HTableInterface testHTable = null;
  43.                 private List<Put> testListPut = new ArrayList<Put>();
  44.                
  45.                 @Override
  46.                 public void setup(Context context){
  47.                          Configuration conf = HBaseConfiguration.create();
  48.                          conf.set("hbase.zookeeper.quorum", "192.168.0.101");
  49.                      pool = new HTablePool(conf, 10);
  50.                      testHTable = pool.getTable("TestCars");
  51.                 }
  52.                
  53.                 @Override
  54.                 public void reduce(Text key,Iterable<Text> values,Context context)throws IOException,InterruptedException{
  55.                         String brand = key.toString();
  56.                
  57.                         for(Text tx : values){
  58.                                 String[] valueSplitted = tx.toString().split(",");
  59.                                 if(valueSplitted.length == 2){
  60.                                         String model = valueSplitted[0];
  61.                             String size = valueSplitted[1];
  62.                            
  63.                             byte[] putKey = Bytes.toBytes(brand+","+model);
  64.                             byte[] putFmaily = Bytes.toBytes("Car");
  65.                             Put put = new Put(putKey);
  66.                            
  67.                             byte[] putQ = Bytes.toBytes("brand");
  68.                             byte[] putVal = Bytes.toBytes(brand);
  69.                             put.add(putFmaily,putQ,putVal);
  70.                            
  71.                             putQ = Bytes.toBytes("model");
  72.                             putVal = Bytes.toBytes(model);
  73.                             put.add(putFmaily,putQ,putVal);
  74.                            
  75.                             putQ = Bytes.toBytes("size");
  76.                             putVal = Bytes.toBytes(size);
  77.                             put.add(putFmaily,putQ,putVal);
  78.                             testListPut.add(put);
  79.                                 }
  80.                         }// End for
  81.                         
  82.                 testHTable.put(testListPut);
  83.                         testHTable.flushCommits();
  84.                 }
  85.                
  86.                 @Override
  87.                 public void cleanup(Context context)throws IOException{
  88.                         if(null != testHTable){
  89.                                 testHTable.close();
  90.                         }
  91.                         
  92.                         if(null != pool){
  93.                                 pool.close();
  94.                         }
  95.                 }
  96.         }
  97.         
  98.         /**
  99.          * @param args
  100.          * @throws IOException
  101.          * @throws ClassNotFoundException
  102.          * @throws InterruptedException
  103.          */
  104.         public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  105.                 // TODO Auto-generated method stub
  106.                 Configuration conf = new Configuration();
  107.         
  108.                
  109.                 Job job = new Job(conf,"OutputHbase");
  110.                 //TableMapReduceUtil.addDependencyJars(job);
  111.                 job.setJarByClass(OutputHbase.class);
  112.                 job.setMapperClass(Map.class);
  113.                 job.setReducerClass(Reduce.class);
  114.                
  115.                 job.setOutputKeyClass(Text.class);
  116.                 job.setOutputValueClass(Text.class);
  117.                 FileInputFormat.addInputPath(job, new Path(args[0]));
  118.                 FileOutputFormat.setOutputPath(job, new Path(args[1]));
  119.                 System.exit(job.waitForCompletion(true)?0:1);
  120.         }
  121. }
复制代码
执行方式:
  1. hadoop jar /home/hadoop/dedup.jar com.duplicate.OutputHbase /home/hadoop/input/* /home/hadoop/output
复制代码
查看结果:两种方式一种直接在hbase客户查看,另一种是用程序直接读出来:
hbase客户端查询:
  1. scan 'TestCars','Car';
复制代码
java代码查询,我下面只查询了主键key值:
  1. package com.duplicate.local;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.client.HTableInterface;
  6. import org.apache.hadoop.hbase.client.HTablePool;
  7. import org.apache.hadoop.hbase.client.Result;
  8. import org.apache.hadoop.hbase.client.ResultScanner;
  9. import org.apache.hadoop.hbase.client.Scan;
  10. import org.apache.hadoop.hbase.util.Bytes;
  11. public class ConnectionHbase {
  12.         private static HTablePool pool = null;
  13.         /**
  14.          * @param args
  15.          */
  16.         public static void main(String[] args) {
  17.                 ConnectionHbase hbase = new ConnectionHbase();
  18.                 hbase.run();
  19.         }
  20.         public void run() {
  21.                 // TODO Auto-generated method stub
  22.                 Configuration conf = HBaseConfiguration.create();
  23.                 HTableInterface testHTable = null;
  24.                 conf.set("hbase.zookeeper.quorum", "192.168.0.101");
  25.                 pool = new HTablePool(conf, 10);
  26.                 testHTable = pool.getTable("TestCars");
  27.                 Scan scan = new Scan();
  28.                 try {
  29.                         ResultScanner res = testHTable.getScanner(scan);
  30.                         for(Result rs : res){
  31.                                 System.out.println(Bytes.toString(rs.getRow()));
  32.                         }
  33.                 } catch (IOException e) {
  34.                         // TODO Auto-generated catch block
  35.                         e.printStackTrace();
  36.                 }
  37.         }
  38. }
复制代码
欢迎加入about云群9037177932227315139327136 ,云计算爱好者群,亦可关注about云腾讯认证空间||关注本站微信

已有(2)人评论

跳转到指定楼层
MatrixPlus 发表于 2017-3-3 17:13:23
言简意赅的好例子
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条