Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在reduce任务中读取与我在map任务(Mapreduce)中读取的相同的输入文件_Java_Hadoop_Mapreduce - Fatal编程技术网

Java 如何在reduce任务中读取与我在map任务(Mapreduce)中读取的相同的输入文件

Java 如何在reduce任务中读取与我在map任务(Mapreduce)中读取的相同的输入文件,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,我一直在尝试读取reducer中的文件,它也是映射程序的输入。那么有什么方法可以在reducer中访问该文件吗?映射器和reducer也有受保护的void设置(上下文上下文上下文)抛出IOException、InterruptedException{}只是为了扩展和覆盖此方法,它可以帮助您首先读取文件 顺便说一句,您还可以设置一个全局变量,以便在mapper中读取并在reducer中使用。 最好使用job.addCacheArchive()method驱动程序类将文件添加到分布式缓存中

我一直在尝试读取reducer中的文件,它也是映射程序的输入。那么有什么方法可以在reducer中访问该文件吗?

映射器和reducer也有
受保护的void设置(上下文上下文上下文)抛出IOException、InterruptedException{}
只是为了扩展和覆盖此方法,它可以帮助您首先读取文件

顺便说一句,您还可以设置一个全局变量,以便在mapper中读取并在reducer中使用。
最好使用
job.addCacheArchive()
method

驱动程序类将文件添加到分布式缓存中

    public class MapReduceDriver extends Configured implements Tool
    {
        public static void main(String[] args) throws Exception 
        {
            int exitCode = ToolRunner.run(new Configuration(), new MapReduceDriver(), args);        
            System.exit(exitCode);
        }

        @Override
        public int run(String[] args) throws Exception 
        {
            Configuration conf = getConf();     
            conf.set("myPath", "/home/hdfs_path");
            ....... 
        }
    }
映射器类

    public class MapReduceMapper extends Mapper<LongWritable, Text, Text, Text>
    {
        public void map(LongWritable key, Text value, Context context)
        {           
            Configuration conf = context.getConfiguration();
            String myPathStr = conf.get("myPath");
            Path myPath = new Path(myPathStr);
            //code to read from the Path
        }
    }
公共类MapReduceMapper扩展了Mapper
{
公共void映射(可长写键、文本值、上下文)
{           
conf=context.getConfiguration();
字符串myPathStr=conf.get(“myPath”);
路径myPath=新路径(myPathStr);
//要从路径读取的代码
}
}
减速器

    public class MapReduceReducer extends Reducer<Text, Text, Text,Text> 
    {
        public void reduce(Text key, Iterable<Text> values, Context context)
        {
            Configuration conf = context.getConfiguration();
            String myPathStr = conf.get("myPath");
            Path myPath = new Path(myPathStr);
            //code to read from the path
        }
    }
公共类MapReducer扩展了Reducer
{
公共void reduce(文本键、Iterable值、上下文)
{
conf=context.getConfiguration();
字符串myPathStr=conf.get(“myPath”);
路径myPath=新路径(myPathStr);
//要从路径读取的代码
}
}

是否尝试覆盖设置方法?要获取上下文/文件系统访问权限?