访问hadoop文件系统中的文件

时间:2013-11-18 22:11:56

标签: java hadoop mapreduce hdfs

我必须访问hadoop文件系统中的几个文件,例如/user/.../data/somefile.txt 我不知道如何访问这些文件。我有一个如下所示的代码,但这不起作用。所以我尝试了“hdfs:// user / ....”,“hdfs:// localhost:50070 / user / ...”或者以某种方式使用URI(尽管我真的不知道这是如何工作的)。

我为此任务提供了hadoop版本1.2.1,我在虚拟机和eclipse(没有hadoop插件)中使用ubuntu。 我之前从未使用过hadoop,所以如果你能帮助我会很棒。

     JobConf conf = new JobConf(TotalWordCount.class); 
     conf.setJobName("wordcount"); 

     conf.setOutputKeyClass(Text.class); 
     conf.setOutputValueClass(IntWritable.class); 

     conf.setMapperClass(Map.class); 
     conf.setCombinerClass(Reduce.class); 
     conf.setReducerClass(Reduce.class); 

     conf.setInputFormat(TextInputFormat.class); 
     conf.setOutputFormat(TextOutputFormat.class); 

     FileInputFormat.setInputPaths(conf, new Path("/user/.../data/textfile.txt")); 

    FileOutputFormat.setOutputPath(conf, new Path("/user/.../output"));

    LineProcessor.initializeStopWords();

    JobClient.runJob(conf); 

运行上面的代码我得到一个像这样的错误:

ERROR security.UserGroupInformation: PriviledgedActionException as:ds2013 cause:org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file:/user/.../data/textfile.txt
Exception in thread "main" org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file:/user/.../data/textfile.txt

我也试过像

这样的东西
 DistributedCache.addCacheFile((new Path("/user/.../data/textfile.txt")).toUri(), conf);
 Path[] paths = DistributedCache.getLocalCacheFiles(conf);
 Path cachePath = paths[0];
 BufferedReader stopListReader = new BufferedReader(new FileReader(cachePath.toString()));

但它无法找到文件。

Exception in thread "main" java.io.FileNotFoundException: File /user/.../data/textfile.txt does not exist.

2 个答案:

答案 0 :(得分:0)

谢谢你们的帮助。问题是你根本无法像我一样在eclipse中运行程序。当我使用终端运行jar时,它会找到路径。

答案 1 :(得分:0)

原因: 实际上,当你直接在eclipse中运行你的工作时,它可以作为本地模式工作,这意味着应用程序将尝试在你的客户端机器中查找文件。

<强>解决方案: 为了确保应用程序可以远程工作,您需要扩展java类:Configured.java并实现Tool,如下所示:

public class SimpleMapperMain extends Configured implements Tool {
    public int run(String[] args) throws Exception {
        //your code here
    }
    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new SimpleMapperMain(),args);
        System.exit(res); 
    }
}

注意:
1.确保你的配置xml文件,如hdfs-site.xml,core-site.xml等包含在类路径中,在你的情况下,它们应该放在maven项目的src / main / resources中。
2.如果涉及到权限问题,请将您当前的用户更改为用户:hdfs,然后再次运行该应用程序,问题应该消失。

如果还有其他问题,请随时问我。