分享

mapreduce configruation传递参数到context上下文

lzw 发表于 2013-12-30 22:27:17 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 20643
本帖最后由 lzw 于 2013-12-30 22:40 编辑

最近练习了一下hadoop mapreduce 如何通过configuration传递参数到mapreduce context 上下文中。设置confgiruation连接hbase参数,将mapreduce统计结果直接存入hbase,参数以后可以通过properties文件读取,下面是配置configruation:
  1. // TODO Auto-generated method stub
  2.                 Configuration conf = new Configuration();
  3.                 conf.set("hbase_host", "192.168.0.101"); // 设置上下文链接hbaseIP
  4.                
  5.                 Job job = new Job(conf,"OutputHbase");
  6.                 //TableMapReduceUtil.addDependencyJars(job);
  7.                 job.setJarByClass(OutputHbase.class);
  8.                 job.setMapperClass(Map.class);
  9.                 job.setReducerClass(Reduce.class);
  10.                
  11.                 job.setOutputKeyClass(Text.class);
  12.                 job.setOutputValueClass(Text.class);
  13.                 FileInputFormat.addInputPath(job, new Path(args[0]));
  14.                 FileOutputFormat.setOutputPath(job, new Path(args[1]));
  15.                 System.exit(job.waitForCompletion(true)?0:1);
复制代码
下面是mapreuce从context上下文中获取hbase链接IP
  1. public static class Map extends Mapper<Object,Text,Text,Text>{
  2.                 private Text outKey = new Text();
  3.                 private Text outVal = new Text();
  4.                
  5.                 public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
  6.                         String[] valueSplitted = value.toString().split(",");
  7.                         if(valueSplitted.length == 3){
  8.                                 String brand = valueSplitted[0];
  9.                     String model = valueSplitted[1];
  10.                     String size = valueSplitted[2];
  11.                                 outKey.set(brand);
  12.                                 outVal.set(model + "," + size);
  13.                                 context.write(outKey, outVal);
  14.                         }
  15.                 }
  16.         }
  17.        
  18.         public static class Reduce extends Reducer<Text,Text,Text,Text>{
  19.                 private HTablePool pool = null;
  20.                 private HTableInterface testHTable = null;
  21.                 private List<Put> testListPut = new ArrayList<Put>();
  22.                
  23.                 @Override
  24.                 public void setup(Context context){
  25. //                        logger.info("hbase_host----" + context.getConfiguration().get("hbase_host"));
  26. //                        logger.info("maxSize----" + context.getConfiguration().get("maxSize"));
  27.                         Configuration jobConf = context.getConfiguration();
  28.                        
  29.                          Configuration conf = HBaseConfiguration.create();
  30.                          
  31.                          conf.set("hbase.zookeeper.quorum", jobConf.get("hbase_host")); //从context上下文中获取连接hbase地址
  32.                      pool = new HTablePool(conf, 10);
  33.                      testHTable = pool.getTable("TestCars");
  34.                 }
  35.                
  36.                 @Override
  37.                 public void reduce(Text key,Iterable<Text> values,Context context)throws IOException,InterruptedException{
  38.                         String brand = key.toString();
  39.                
  40.                         for(Text tx : values){
  41.                                 String[] valueSplitted = tx.toString().split(",");
  42.                                 if(valueSplitted.length == 2){
  43.                                         String model = valueSplitted[0];
  44.                             String size = valueSplitted[1];
  45.                            
  46.                             byte[] putKey = Bytes.toBytes(brand+","+model);
  47.                             byte[] putFmaily = Bytes.toBytes("Car");
  48.                             Put put = new Put(putKey);
  49.                            
  50.                             byte[] putQ = Bytes.toBytes("brand");
  51.                             byte[] putVal = Bytes.toBytes(brand);
  52.                             put.add(putFmaily,putQ,putVal);
  53.                            
  54.                             putQ = Bytes.toBytes("model");
  55.                             putVal = Bytes.toBytes(model);
  56.                             put.add(putFmaily,putQ,putVal);
  57.                            
  58.                             putQ = Bytes.toBytes("size");
  59.                             putVal = Bytes.toBytes(size);
  60.                             put.add(putFmaily,putQ,putVal);
  61.                             testListPut.add(put);
  62.                                 }
  63.                         }// End for
  64.                        
  65.                 testHTable.put(testListPut);
  66.                         testHTable.flushCommits();
  67.                 }
  68.                
  69.                 @Override
  70.                 public void cleanup(Context context)throws IOException{
  71.                         if(null != testHTable){
  72.                                 testHTable.close();
  73.                         }
  74.                        
  75.                         if(null != pool){
  76.                                 pool.close();
  77.                         }
  78.                 }
  79.         }
  80.        
复制代码
下面是连接hbase查询代码:
  1. import org.apache.hadoop.hbase.HBaseConfiguration;
  2. import org.apache.hadoop.hbase.client.HTableInterface;
  3. import org.apache.hadoop.hbase.client.HTablePool;
  4. import org.apache.hadoop.hbase.client.Result;
  5. import org.apache.hadoop.hbase.client.ResultScanner;
  6. import org.apache.hadoop.hbase.client.Scan;
  7. import org.apache.hadoop.hbase.util.Bytes;
  8. public class ConnectionHbase {
  9.         private static HTablePool pool = null;
  10.         /**
  11.          * @param args
  12.          */
  13.         public static void main(String[] args) {
  14.                 ConnectionHbase hbase = new ConnectionHbase();
  15.                 hbase.run();
  16.         }
  17.         public void run() {
  18.                 // TODO Auto-generated method stub
  19.                 Configuration conf = HBaseConfiguration.create();
  20.                 HTableInterface testHTable = null;
  21.                 conf.set("hbase.zookeeper.quorum", "192.168.0.101");
  22.                 pool = new HTablePool(conf, 10);
  23.                 testHTable = pool.getTable("TestCars");
  24.                 Scan scan = new Scan();
  25.                 try {
  26.                         ResultScanner res = testHTable.getScanner(scan);
  27.                         for(Result rs : res){
  28.                                 System.out.println(Bytes.toString(rs.getRow()));
  29.                         }
  30.                 } catch (IOException e) {
  31.                         // TODO Auto-generated catch block
  32.                         e.printStackTrace();
  33.                 }
  34.         }
  35. }
复制代码
下面为查询出结果:
  1. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
  2. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:host.name=robinliu-PC
  3. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:java.version=1.6.0_32-ea
  4. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Sun Microsystems Inc.
  5. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:java.home=C:\Program Files\Java\jdk1.6.0_32\jre
  6. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:java.class.path=D:\job\developing\duplicate\target\classes;D:\soft\dev\maven\repos\org\apache\hbase\hbase\0.94.6\hbase-0.94.6.jar;D:\soft\dev\maven\repos\com\yammer\metrics\metrics-core\2.1.2\metrics-core-2.1.2.jar;D:\soft\dev\maven\repos\com\google\guava\guava\11.0.2\guava-11.0.2.jar;D:\soft\dev\maven\repos\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;D:\soft\dev\maven\repos\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;D:\soft\dev\maven\repos\commons-configuration\commons-configuration\1.6\commons-configuration-1.6.jar;D:\soft\dev\maven\repos\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;D:\soft\dev\maven\repos\commons-digester\commons-digester\1.8\commons-digester-1.8.jar;D:\soft\dev\maven\repos\commons-beanutils\commons-beanutils\1.7.0\commons-beanutils-1.7.0.jar;D:\soft\dev\maven\repos\commons-beanutils\commons-beanutils-core\1.8.0\commons-beanutils-core-1.8.0.jar;D:\soft\dev\maven\repos\com\github\stephenc\high-scale-lib\high-scale-lib\1.1.1\high-scale-lib-1.1.1.jar;D:\soft\dev\maven\repos\commons-codec\commons-codec\1.4\commons-codec-1.4.jar;D:\soft\dev\maven\repos\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;D:\soft\dev\maven\repos\commons-io\commons-io\2.1\commons-io-2.1.jar;D:\soft\dev\maven\repos\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;D:\soft\dev\maven\repos\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;D:\soft\dev\maven\repos\log4j\log4j\1.2.16\log4j-1.2.16.jar;D:\soft\dev\maven\repos\org\apache\avro\avro\1.5.3\avro-1.5.3.jar;D:\soft\dev\maven\repos\org\xerial\snappy\snappy-java\1.0.3.2\snappy-java-1.0.3.2.jar;D:\soft\dev\maven\repos\org\apache\avro\avro-ipc\1.5.3\avro-ipc-1.5.3.jar;D:\soft\dev\maven\repos\org\jboss\netty\netty\3.2.4.Final\netty-3.2.4.Final.jar;D:\soft\dev\maven\repos\org\apache\velocity\velocity\1.7\velocity-1.7.jar;D:\soft\dev\maven\repos\org\apache\zookeeper\zookeeper\3.4.5\zookeeper-3.4.5.jar;D:\soft\dev\maven\repos\org\apache\thrift\libthrift\0.8.0\libthrift-0.8.0.jar;D:\soft\dev\maven\repos\org\jruby\jruby-complete\1.6.5\jruby-complete-1.6.5.jar;D:\soft\dev\maven\repos\org\mortbay\jetty\jetty\6.1.26\jetty-6.1.26.jar;D:\soft\dev\maven\repos\org\mortbay\jetty\jetty-util\6.1.26\jetty-util-6.1.26.jar;D:\soft\dev\maven\repos\org\mortbay\jetty\jsp-2.1\6.1.14\jsp-2.1-6.1.14.jar;D:\soft\dev\maven\repos\org\mortbay\jetty\jsp-api-2.1\6.1.14\jsp-api-2.1-6.1.14.jar;D:\soft\dev\maven\repos\org\mortbay\jetty\servlet-api-2.5\6.1.14\servlet-api-2.5-6.1.14.jar;D:\soft\dev\maven\repos\org\codehaus\jackson\jackson-core-asl\1.8.8\jackson-core-asl-1.8.8.jar;D:\soft\dev\maven\repos\org\codehaus\jackson\jackson-mapper-asl\1.8.8\jackson-mapper-asl-1.8.8.jar;D:\soft\dev\maven\repos\org\codehaus\jackson\jackson-jaxrs\1.8.8\jackson-jaxrs-1.8.8.jar;D:\soft\dev\maven\repos\org\codehaus\jackson\jackson-xc\1.8.8\jackson-xc-1.8.8.jar;D:\soft\dev\maven\repos\org\slf4j\slf4j-api\1.4.3\slf4j-api-1.4.3.jar;D:\soft\dev\maven\repos\org\slf4j\slf4j-log4j12\1.4.3\slf4j-log4j12-1.4.3.jar;D:\soft\dev\maven\repos\tomcat\jasper-compiler\5.5.23\jasper-compiler-5.5.23.jar;D:\soft\dev\maven\repos\tomcat\jasper-runtime\5.5.23\jasper-runtime-5.5.23.jar;D:\soft\dev\maven\repos\org\jamon\jamon-runtime\2.3.1\jamon-runtime-2.3.1.jar;D:\soft\dev\maven\repos\com\google\protobuf\protobuf-java\2.4.0a\protobuf-java-2.4.0a.jar;D:\soft\dev\maven\repos\com\sun\jersey\jersey-core\1.8\jersey-core-1.8.jar;D:\soft\dev\maven\repos\com\sun\jersey\jersey-json\1.8\jersey-json-1.8.jar;D:\soft\dev\maven\repos\org\codehaus\jettison\jettison\1.1\jettison-1.1.jar;D:\soft\dev\maven\repos\com\sun\xml\bind\jaxb-impl\2.2.3-1\jaxb-impl-2.2.3-1.jar;D:\soft\dev\maven\repos\com\sun\jersey\jersey-server\1.8\jersey-server-1.8.jar;D:\soft\dev\maven\repos\asm\asm\3.1\asm-3.1.jar;D:\soft\dev\maven\repos\javax\xml\bind\jaxb-api\2.1\jaxb-api-2.1.jar;D:\soft\dev\maven\repos\javax\activation\activation\1.1\activation-1.1.jar;D:\soft\dev\maven\repos\stax\stax-api\1.0.1\stax-api-1.0.1.jar;D:\soft\dev\maven\repos\org\apache\hadoop\hadoop-core\1.0.4\hadoop-core-1.0.4.jar;D:\soft\dev\maven\repos\xmlenc\xmlenc\0.52\xmlenc-0.52.jar;D:\soft\dev\maven\repos\org\apache\commons\commons-math\2.1\commons-math-2.1.jar;D:\soft\dev\maven\repos\commons-net\commons-net\1.4.1\commons-net-1.4.1.jar;D:\soft\dev\maven\repos\commons-el\commons-el\1.0\commons-el-1.0.jar;D:\soft\dev\maven\repos\net\java\dev\jets3t\jets3t\0.7.1\jets3t-0.7.1.jar;D:\soft\dev\maven\repos\net\sf\kosmosfs\kfs\0.3\kfs-0.3.jar;D:\soft\dev\maven\repos\hsqldb\hsqldb\1.8.0.10\hsqldb-1.8.0.10.jar;D:\soft\dev\maven\repos\oro\oro\2.0.8\oro-2.0.8.jar;D:\soft\dev\maven\repos\org\eclipse\jdt\core\3.1.1\core-3.1.1.jar
  7. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:java.library.path=C:\Program Files\Java\jdk1.6.0_32\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Perl64\site\bin;C:\Perl64\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files (x86)\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Program Files\Java\jdk1.6.0_32\bin;C:\apache-maven-3.1.0\bin;.
  8. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\robinliu\AppData\Local\Temp\
  9. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
  10. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 7
  11. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
  12. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:os.version=6.1
  13. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:user.name=robinliu
  14. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\robinliu
  15. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Client environment:user.dir=D:\job\developing\duplicate
  16. 13/12/30 22:12:30 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.0.101:2181 sessionTimeout=180000 watcher=hconnection
  17. 13/12/30 22:12:30 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 7068@robinliu-PC
  18. 13/12/30 22:12:30 INFO zookeeper.ClientCnxn: Opening socket connection to server hadoop.master/192.168.0.101:2181. Will not attempt to authenticate using SASL (无法定位登录配置)
  19. 13/12/30 22:12:30 INFO zookeeper.ClientCnxn: Socket connection established to hadoop.master/192.168.0.101:2181, initiating session
  20. 13/12/30 22:12:30 INFO zookeeper.ClientCnxn: Session establishment complete on server hadoop.master/192.168.0.101:2181, sessionid = 0x14343c460920014, negotiated timeout = 180000
  21. Acura,Integra
  22. Acura,Legend
  23. Audi,100
  24. Audi,90
  25. BMW,535i
  26. Buick,Century
  27. Buick,LeSabre
  28. Buick,Riviera
  29. Buick,Roadmaster
  30. Cadillac,DeVille
  31. Cadillac,Seville
复制代码
程序中仅仅查询出来rowkey值






来自群组: hadoop技术组
欢迎加入about云群9037177932227315139327136 ,云计算爱好者群,亦可关注about云腾讯认证空间||关注本站微信

没找到任何评论,期待你打破沉寂

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

本版积分规则

关闭

推荐上一条 /2 下一条