應用Maven搭建Hadoop開辟情況。本站提示廣大學習愛好者:(應用Maven搭建Hadoop開辟情況)文章只能為提供參考,不一定能成為您想要的結果。以下是應用Maven搭建Hadoop開辟情況正文
關於Maven的應用就不再煩瑣了,網上許多,而且這麼多年變更也不年夜,這裡僅引見怎樣搭建Hadoop的開辟情況。
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文件以下
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>my.hadoopstudy</groupId> <artifactId>hadoopstudy</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>hadoopstudy</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
3. 測試
3.1 起首我們可以測試一下hdfs的開辟,這裡假定應用上一篇Hadoop文章中的hadoop集群,類代碼以下
package my.hadoopstudy.dfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.InputStream;
import java.net.URI;
public class Test {
public static void main(String[] args) throws Exception {
String uri = "hdfs://9.111.254.189:9000/";
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), config);
// 列出hdfs上/user/fkong/目次下的一切文件和目次
FileStatus[] statuses = fs.listStatus(new Path("/user/fkong"));
for (FileStatus status : statuses) {
System.out.println(status);
}
// 在hdfs的/user/fkong目次下創立一個文件,並寫入一行文本
FSDataOutputStream os = fs.create(new Path("/user/fkong/test.log"));
os.write("Hello World!".getBytes());
os.flush();
os.close();
// 顯示在hdfs的/user/fkong下指定文件的內容
InputStream is = fs.open(new Path("/user/fkong/test.log"));
IOUtils.copyBytes(is, System.out, 1024, true);
}
}
3.2 測試MapReduce功課
測試代碼比擬簡略,以下:
package my.hadoopstudy.mapreduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import java.io.IOException;
public class EventCount {
public static class MyMapper extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text event = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
int idx = value.toString().indexOf(" ");
if (idx > 0) {
String e = value.toString().substring(0, idx);
event.set(e);
context.write(event, one);
}
}
}
public static class MyReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: EventCount <in> <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "event count");
job.setJarByClass(EventCount.class);
job.setMapperClass(MyMapper.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
運轉“mvn package”敕令發生jar包hadoopstudy-1.0-SNAPSHOT.jar,並將jar文件復制到hadoop裝置目次下
這裡假定我們須要剖析幾個日記文件中的Event信息來統計各類Event個數,所以創立一下目次和文件
/tmp/input/event.log.1
/tmp/input/event.log.2
/tmp/input/event.log.3
由於這裡只是要做一個列子,所以每一個文件內容可以都一樣,假設內容以下
JOB_NEW ...
JOB_NEW ...
JOB_FINISH ...
JOB_NEW ...
JOB_FINISH ...
然後把這些文件復制到HDFS上
$ bin/hdfs dfs -put /tmp/input /user/fkong/input
運轉mapreduce功課
$ bin/hadoop jar hadoopstudy-1.0-SNAPSHOT.jar my.hadoopstudy.mapreduce.EventCount /user/fkong/input /user/fkong/output
檢查履行成果
$ bin/hdfs dfs -cat /user/fkong/output/part-r-00000
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。