Mac版本的intellij14 开发hadoop程序环境搭建

1. 使用《Hadoop 权威指南》中的代码范例,三段代码如下:

package com.wozaizuo.count;

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable>
{
    private static final int MISSING = 9999;
    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
    {
        String line = value.toString();
        String year = line.substring(15, 19);
        int airTemperature;
        if (line.charAt(87) == '+')
        {
            airTemperature = Integer.parseInt(line.substring(88, 92));
        }
        else
        {
            airTemperature = Integer.parseInt(line.substring(87, 92));
        }
        
        String quality = line.substring(92, 93);
        if (airTemperature != MISSING && quality.matches("[01459]"))
        {
            context.write(new Text(year), new IntWritable(airTemperature));
        }
    }
}

 

package com.wozaizuo.count;

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> 
{
    @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws Exception
    {
        int maxValue = Integer.MIN_VALUE;
        for (IntWritable value : values) 
        {
            maxValue = Math.max(maxValue, value.get());
        }
        context.write(key, new IntWritable(maxValue));
    }
}

 

package com.wozaizuo.count;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MaxTemperature
{
    public static void main(String[] args) throws Exception
    {
        if (args.length != 2) {
            System.err.println("Usage: MaxTemperature <input path> <output path>");
            System.exit(-1);
        }
        Job job = new Job();
//job.setJarByClass(WordCount.class);  job.setJarByClass(MaxTemperature.
class); job.setJobName("Max temperature"); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(MaxTemperatureMapper.class); job.setReducerClass(MaxTemperatureReducer.class); job.setOutputKeyClass(Text.class); //注1 job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } }

 注意:此处注销了 job.setJarByClass(WordCount.class); 这句代码,是为了在下午中使用另外一种方式运行本程序。

 

2.下载 hadoop-2.6.4,无需安装,无需配置环境。因为在调试程序过程中使用的单机版模式,并非使用HDFS文件系统。

 

 

3.创建一个空的「hadoop_test」project,然后将第1步中的三个类放入hadoop_test里

注意:贴入代码之后,intellj会报错,找不着类。是因为无法找到对应的hadoop包。需要导入步骤2中箭头标记的文件夹里的hadoop jar包。

按照以上4张截图的方式,导入hadoop需要的jar包。在成功导入hadoop jar包之后,intellj里的错误提示会自动消失。

 

3.创建一个空的jar,这个jar在之后运行hadoop程序的时候会用到。

Name:maxtemperature 这个名字可以自定义。

Type:选择JAR

 

4.配置运行条件

如果没有这个选项,则直接在 「MaxTemperature」这个类下运行程序,报错之后就会出现截图中的选项。

Main class: org.apache.hadoop.util.Runjar

Program arguments: 一共四个参数(截图中四行,每行代表一个参数值)

参数1:之前创建的空jar

参数2:要运行的main函数所在的类,上文注释了一行代码就是为了在这里演示这种运行配置方式

参数3:输入的目录路径

参数4:输出的目录路径(不要提前创建,系统自动创建)

 

5.运行结果如下图所示

 

posted @ 2016-07-04 16:00  一瞳孔  阅读(374)  评论(0)    收藏  举报