使用Maven搭建Hadoop开发环境

文章来自:http://blog.csdn.net/kongxx/article/details/42339581

关于Maven的使用就不再啰嗦了,网上很多,并且这么多年变化也不大,这里仅介绍怎么搭建Hadoop的开发环境。

1. 首先创建工程

  1. mvn archetype:generate -DgroupId=my.hadoopstudy -DartifactId=hadoopstudy -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false  


2. 然后在pom.xml文件里添加hadoop的依赖包hadoop-common, hadoop-client, hadoop-hdfs,添加后的pom.xml文件如下

  1. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"  
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>my.hadoopstudy</groupId>  
  5.     <artifactId>hadoopstudy</artifactId>  
  6.     <packaging>jar</packaging>  
  7.     <version>1.0-SNAPSHOT</version>  
  8.     <name>hadoopstudy</name>  
  9.     <url>http://maven.apache.org</url>  
  10.    
  11.     <dependencies>  
  12.         <dependency>  
  13.             <groupId>org.apache.hadoop</groupId>  
  14.             <artifactId>hadoop-common</artifactId>  
  15.             <version>2.5.1</version>  
  16.         </dependency>  
  17.         <dependency>  
  18.             <groupId>org.apache.hadoop</groupId>  
  19.             <artifactId>hadoop-hdfs</artifactId>  
  20.             <version>2.5.1</version>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>org.apache.hadoop</groupId>  
  24.             <artifactId>hadoop-client</artifactId>  
  25.             <version>2.5.1</version>  
  26.         </dependency>  
  27.    
  28.         <dependency>  
  29.             <groupId>junit</groupId>  
  30.             <artifactId>junit</artifactId>  
  31.             <version>3.8.1</version>  
  32.             <scope>test</scope>  
  33.         </dependency>  
  34.     </dependencies>  
  35. </project>  


3. 测试
3.1 首先我们可以测试一下hdfs的开发,这里假定使用上一篇Hadoop文章中的hadoop集群,类代码如下

  1. package my.hadoopstudy.dfs;  
  2.    
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FSDataOutputStream;  
  5. import org.apache.hadoop.fs.FileStatus;  
  6. import org.apache.hadoop.fs.FileSystem;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IOUtils;  
  9.    
  10. import java.io.InputStream;  
  11. import java.net.URI;  
  12.    
  13. public class Test {  
  14.     public static void main(String[] args) throws Exception {  
  15.         String uri = "hdfs://9.111.254.189:9000/";  
  16.         Configuration config = new Configuration();  
  17.         FileSystem fs = FileSystem.get(URI.create(uri), config);  
  18.    
  19.         // 列出hdfs上/user/fkong/目录下的所有文件和目录  
  20.         FileStatus[] statuses = fs.listStatus(new Path("/user/fkong"));  
  21.         for (FileStatus status : statuses) {  
  22.             System.out.println(status);  
  23.         }  
  24.    
  25.         // 在hdfs的/user/fkong目录下创建一个文件,并写入一行文本  
  26.         FSDataOutputStream os = fs.create(new Path("/user/fkong/test.log"));  
  27.         os.write("Hello World!".getBytes());  
  28.         os.flush();  
  29.         os.close();  
  30.    
  31.         // 显示在hdfs的/user/fkong下指定文件的内容  
  32.         InputStream is = fs.open(new Path("/user/fkong/test.log"));  
  33.         IOUtils.copyBytes(is, System.out, 1024true);  
  34.     }  
  35. }  


3.2 测试MapReduce作业
测试代码比较简单,如下:

  1. package my.hadoopstudy.mapreduce;  
  2.    
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.Path;  
  5. import org.apache.hadoop.io.IntWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Job;  
  8. import org.apache.hadoop.mapreduce.Mapper;  
  9. import org.apache.hadoop.mapreduce.Reducer;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  12. import org.apache.hadoop.util.GenericOptionsParser;  
  13.    
  14. import java.io.IOException;  
  15.    
  16. public class EventCount {  
  17.    
  18.     public static class MyMapper extends Mapper<Object, Text, Text, IntWritable>{  
  19.         private final static IntWritable one = new IntWritable(1);  
  20.         private Text event = new Text();  
  21.    
  22.         public void map(Object key, Text value, Context context) throws IOException, InterruptedException {  
  23.             int idx = value.toString().indexOf(" ");  
  24.             if (idx > 0) {  
  25.                 String e = value.toString().substring(0, idx);  
  26.                 event.set(e);  
  27.                 context.write(event, one);  
  28.             }  
  29.         }  
  30.     }  
  31.    
  32.     public static class MyReducer extends Reducer<Text,IntWritable,Text,IntWritable> {  
  33.         private IntWritable result = new IntWritable();  
  34.    
  35.         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
  36.             int sum = 0;  
  37.             for (IntWritable val : values) {  
  38.                 sum += val.get();  
  39.             }  
  40.             result.set(sum);  
  41.             context.write(key, result);  
  42.         }  
  43.     }  
  44.    
  45.     public static void main(String[] args) throws Exception {  
  46.         Configuration conf = new Configuration();  
  47.         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  48.         if (otherArgs.length < 2) {  
  49.             System.err.println("Usage: EventCount <in> <out>");  
  50.             System.exit(2);  
  51.         }  
  52.         Job job = Job.getInstance(conf, "event count");  
  53.         job.setJarByClass(EventCount.class);  
  54.         job.setMapperClass(MyMapper.class);  
  55.         job.setCombinerClass(MyReducer.class);  
  56.         job.setReducerClass(MyReducer.class);  
  57.         job.setOutputKeyClass(Text.class);  
  58.         job.setOutputValueClass(IntWritable.class);  
  59.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  60.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  61.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  62.     }  
  63. }  

运行“mvn package”命令产生jar包hadoopstudy-1.0-SNAPSHOT.jar,并将jar文件复制到hadoop安装目录下

这里假定我们需要分析几个日志文件中的Event信息来统计各种Event个数,所以创建一下目录和文件

  1. /tmp/input/event.log.1  
  2. /tmp/input/event.log.2  
  3. /tmp/input/event.log.3  


因为这里只是要做一个列子,所以每个文件内容可以都一样,假如内容如下

  1. JOB_NEW ...  
  2. JOB_NEW ...  
  3. JOB_FINISH ...  
  4. JOB_NEW ...  
  5. JOB_FINISH ...  


然后把这些文件复制到HDFS上

  1. $ bin/hdfs dfs -put /tmp/input /user/fkong/input  


运行mapreduce作业

  1. $ bin/hadoop jar hadoopstudy-1.0-SNAPSHOT.jar my.hadoopstudy.mapreduce.EventCount /user/fkong/input /user/fkong/output  


查看执行结果

  1. $ bin/hdfs dfs -cat /user/fkong/output/part-r-00000 

猜你喜欢

转载自ximeng1234.iteye.com/blog/2206476
今日推荐