6.4hadoop idea本地运行器测试

1.1  本地运行器进行本地测试

写一个MapReduce驱动程序,执行job,实现tool接口,所以可以通过hadoop的命令行去设置为本地运行模式。实现tool的run函数,在run函数中创建job执行任务,输出结果。

1.1.1         本地任务执行器定义

package Temperature;

import javafx.scene.text.Text;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;

import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.jobcontrol.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class MaxTemperatureDrive extends Configured implements Tool {

    public int run(String[] var1) throws Exception
    {
        if (var1.length!=2)
        {
            return -1;
        }
        JobConf conf=new JobConf(MaxTemperatureJob.class);//hadoop会根据类名去找jar包
        conf.setJobName("Max temperature");
        FileInputFormat.addInputPath(conf,new Path(var1[0]));//输入文件:单个文件或者目录
        FileOutputFormat.setOutputPath(conf,new Path(var1[1]));//输出路径,hadoop新建,不能存在。避免误覆盖
        conf.setMapperClass(TemperatureMapper.class);
        conf.setReducerClass(MaxTempertureReduce.class);
        conf.setOutputKeyClass(Text.class);//reduce的输出类型,map一致时默认,不一致,map也需要指定
        conf.setOutputValueClass(IntWritable.class);
        JobClient.runJob(conf);
        return 0;
    }
    public static void main(String[] args) throws Exception {
        int exitCode= ToolRunner.run(new MaxTemperatureDrive(),args);
        System.exit(exitCode);
    }
}

 

编译文件,编译生成jar文件,通过hadoop的命令行参数,设置本地模式,main函数的参数参入输入输出路径,run函数执行job任务输出结果。

指定用本地模式有两种方法-conf Hadoop-local.xml或者用 –jt local

方法1:指定配置文件、输入路径、输出路径

%mvn compile

%exprot HADOOP_CLASSPATH=target/classes/

%hadoop v2.MaxTempertureDrive –conf conf/hadoop-local.xml input/ncdc/micro output

方法2指定文件系统、本地模式、输入路径、输出路径。将mapreduce.framework.name的指针设置为local,则使用本地作业运行器运行作业。

%hadoop v2.MaxTempertureDrive –fs file:/// -jt local input/ncdc/micro output

 

1.1.2         idea直接调试运行MaxTemperatureDrive

采用hadoop命令运行程序时,没法单步调试。可以直接创建测试类在idea中调试运行。

(1)创建测试类

package Temperature;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import static org.junit.Assert.assertThat;


public class MaxTemperatureDriveTest {
    @Test
    public void test() throws  Exception
    {
        Configuration conf =new Configuration();
        conf.set("fs.defaultFS","file:///");//设置默认文件系统
        conf.set("mapreduce.framework.name","local");//设置本地运行器模式
        conf.setInt("mapreduce.task.io.sort.mb",1);
        //在工程目录D:\Project\Hadoop\下创建输入input/ncdc/micro和输出目录output
        Path input =new Path("input/ncdc/micro");//输入路径
        Path output=new Path("output");//输出路径
        //删除上一次的输出,避免重复
        FileSystem fs=FileSystem.getLocal(conf);
        fs.delete(output,true);
        MaxTemperatureDrive driver=new MaxTemperatureDrive();
        driver.setConf(conf);//设置driver配置
        //传入输入路径和输出路径,调用run函数运行测试
       int exitcode= driver.run(new String[] {input.toString(),output.toString()});
       System.out.print(exitcode);
    }
}

 

(2)项目路径创建输入路径和输出路径,在输入路径中写入测试数据的txt文件

(3)调试运行程序

自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

https://www.cnblogs.com/bclshuai/p/11380657.html

posted @ 2019-12-15 10:26  一字千金  阅读(403)  评论(0编辑  收藏  举报