使用Maven開發Hadoop

環境爲Hadoop2.5.2(如何搭建環境教程),在pom.xml中加入如下配置文件。java

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>2.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-hdfs</artifactId>
    <version>2.5.2</version>
</dependency>

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.5.2</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>

測試HDFSshell

public class HdfsTest
{
    public static void main( String[] args ) throws IOException {
        String uri = "hdfs://192.168.1.112:9000/";
        Configuration config = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), config);
        //列出目錄全部文件
        FileStatus[] statuses = fs.listStatus(new Path("/data"));
        for (FileStatus status : statuses) {
            System.out.println(status);
        }
        //建立新文件
        FSDataOutputStream os = fs.create(new Path("/data/hdfs_test.txt"));
        os.write("測試HDFS第一條\r\n".getBytes());
        os.write("測試HDFS第二條\r\n".getBytes());
        os.flush();
        os.close();
        //讀取文件
        InputStream is = fs.open(new Path("/data/hdfs_test.txt"));
        IOUtils.copyBytes(is, System.out, 1024, true);
    }
}

測試Map/Reduce
apache

實例:將多個文件裏面的內容去掉重複行。
服務器

思路:把數據行當作map/reduce的key來處理便可。value能夠爲空。app

代碼實現以下:maven

package com.zhm;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;

/**
 * Created by zhm on 2015/7/8.
 */
public class MapReduceTest {
    public static class MyMapper extends Mapper<Object, Text, Text, Text> {
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            //將文本行放入key
            context.write(value,new Text(""));
        }
    }

    public static class MyReducer extends Reducer<Text,Text,Text,Text> {
        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //輸出key
            context.write(key, new Text(""));
        }
    }

    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: MapReduceTest <in> <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "MapReduceTest");
        job.setJarByClass(MapReduceTest.class);
        job.setMapperClass(MyMapper.class);
        job.setCombinerClass(MyReducer.class);
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

運行:mvn clean package 打好jar包。並上傳至服務器的hadoop安裝目錄中。oop

在服務器上建立須要統計去重的文件。測試

mkdir /tmp/mapred

cd /tmp/mapred
vi file1.txt
#輸入如下內容
192.168.1.1
192.168.1.2
192.168.1.4

vi file2.txt
#輸入如下內容
192.168.1.3
192.168.1.2
192.168.1.5

vi file3.txt
#輸入如下內容
192.168.1.1
192.168.1.3
192.168.1.4

#清空hdfs目錄,tmp目錄不要刪除。主要是測試方便,也能夠不刪除目錄,只要將文件指定一個新的測試目錄就行。
hdfs dfs -rm -r -f -skipTrash /目錄名

#將建立好的文件上傳至HDFS
 hdfs dfs -put /tmp/mapred /input
 
#進入hadoop安裝主目錄
hadoop jar maven_hadoop-1.0-SNAPSHOT.jar com.zhm.MapReduceTest /input /output

#查看結果
hdfs dfs -cat /output/part-r-00000
#結果以下:
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
相關文章
相關標籤/搜索