Java 使用-files参数将文件传递给Hadoop
我有一个在本地正确执行的MapReduce程序 它使用mapper类的setup()方法中名为new-positions.csv的文件来填充内存中的哈希表:Java 使用-files参数将文件传递给Hadoop,java,hadoop,Java,Hadoop,我有一个在本地正确执行的MapReduce程序 它使用mapper类的setup()方法中名为new-positions.csv的文件来填充内存中的哈希表: public void setup(Context context) throws IOException, InterruptedException { newPositions = new Hashtable<String, Integer>(); File file = new File(
public void setup(Context context) throws IOException, InterruptedException {
newPositions = new Hashtable<String, Integer>();
File file = new File("new-positions.csv");
Scanner inputStream = new Scanner(file);
String line = null;
String firstline = inputStream.nextLine();
while(inputStream.hasNext()){
line = inputStream.nextLine();
String[] splitLine = line.split(",");
Integer id = Integer.valueOf(splitLine[0].trim());
// String firstname = splitLine[1].trim();
// String surname = splitLine[2].trim();
String[] emails = new String[4];
for (int i = 3; i < 7; i++) {
emails[i-3] = splitLine[i].trim();
}
for (String email : emails) {
if (!email.equals("")) newPositions.put(email, id);
}
// String position = splitLine[7].trim();
inputStream.close();
}
}
它执行得很好,但是当它到达映射器时,我们得到:
Error: java.io.FileNotFoundException: new-positions.csv (No such file or directory)
这个文件肯定是本地存在的,我们肯定是从该目录中执行的
我们遵循Hadoop中给出的指南:最终指南(第四版),第。从274年开始,我们看不出我们的程序和参数在结构上有什么不同
这可能与Hadoop配置有关吗?我们知道有一些变通方法,比如将文件复制到HDFS,然后从那里执行,但我们需要理解为什么“-files”参数没有按预期工作
编辑:下面是来自驱动程序类的一些代码,这也可能是问题的根源:
公共int运行(字符串[]args)引发IOException、InterruptedException、ClassNotFoundException{
如果(参数长度!=5){
打印用途(此“”);
返回1;
}
Configuration config = getConf();
FileSystem fs = FileSystem.get(config);
Job job = Job.getInstance(config);
job.setJarByClass(this.getClass());
FileInputFormat.addInputPath(job, new Path(args[3]));
// Delete old output if necessary
Path outPath = new Path(args[4]);
if (fs.exists(outPath))
fs.delete(outPath, true);
FileOutputFormat.setOutputPath(job, new Path(args[4]));
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
job.setMapOutputKeyClass(EdgeWritable.class);
job.setMapOutputValueClass(NullWritable.class);
job.setMapperClass(MailReaderMapper.class);
job.setReducerClass(MailReaderReducer.class);
job.setJar("MR2.jar");
boolean status = job.waitForCompletion(true);
return status ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new Reader2(), args);
System.exit(exitCode);
}
假设您的“new positions.csv”存在于文件夹:H:/HDP/
中,那么您需要将此文件作为以下文件传递:
file:///H:/HDP/new-positions.csv
您需要使用文件://
限定路径,以指示它是本地文件系统路径。此外,您还需要传递完全限定路径
这对我来说非常合适
例如,我传递本地文件myini.ini
,如下所示:
jar-hadoop-mapreduce-examples-2.4.0.2.1.5.0-2060.jar teragen-files”file:///H:/HDP/hadoop-2.4.0.2.1.5.0-2060/share/hadoop/common/myini.ini”-Dmapreduce.job.maps=10 10737418/usr/teraout/
我想Manjunath Ballur给了你一个正确的答案,但是你传递的URI,file:///home/local/xxx360/FinalProject/new-positions.csv
可能无法从Hadoop工作计算机解析
该路径看起来像机器上的绝对路径,但哪台机器包含home
?向该路径添加服务器,我认为它可能会工作
或者,如果使用单数的-file
,看起来Hadoop将复制该文件,而不是像使用-files
那样创建符号链接
请参阅文档。我看不出您的代码有任何错误。 从我的工作代码(技术上与您的相同)中,当我将
-
添加到文件名时,我还得到了java.io.FileNotFoundException
。请删除-
,然后重试:
File file = new File("newpositions.csv");
新命令如下所示:hadoop jar MR2.jar Reader2-filesfile:///home/local/xxx360/FinalProject/new-positions.csv InputDataset OutputFolder…我在尝试访问“new positions.csv”时也会遇到同样的错误在Java程序中。它可能是我们Hadoop配置中的某个东西吗?用双引号给出整个路径直到不起作用-我想知道问题是否在我的驱动程序类中。我将用附加信息编辑主要问题。
File file = new File("newpositions.csv");
hadoop jar MR2.jar Reader2 -files newpositions.csv InputDataset OutputFolder