简体   繁体   中英

Hadoop Map Reduce - Read HDFS File - FileAlreadyExists error

I am new to Hadoop. I am trying to read an existing file on HDFS using the below code. The configuration seem file and the file path is correct as well. -

public static class Map extends Mapper<LongWritable, Text, Text, Text> {

    private static Text f1, f2, hdfsfilepath;
    private static HashMap<String, ArrayList<String>> friendsData = new HashMap<>();

    public void setup(Context context) throws IOException {
      Configuration conf = context.getConfiguration();
      Path path = new Path("hdfs://cshadoop1" + conf.get("hdfsfilepath"));
      FileSystem fs = FileSystem.get(path.toUri(), conf);
      if (fs.exists(path)) {
        BufferedReader br = new BufferedReader(
            new InputStreamReader(fs.open(path)));
        String line;
        line = br.readLine();
        while (line != null) {
          StringTokenizer str = new StringTokenizer(line, ",");
          String friend = str.nextToken();
          ArrayList<String> friendDetails = new ArrayList<>();
          while (str.hasMoreTokens()) {
            friendDetails.add(str.nextToken());
          }
          friendsData.put(friend, friendDetails);
        }
      }
    }

    public void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {
      for (String k : friendsData.keySet()) {
        context.write(new Text(k), new Text(friendsData.get(k).toString()));
      }
    }
  }

I am getting the below exception when I run the code -

Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://cshadoop1/socNetData/userdata/userdata.txt already exists
        at org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.checkOutputSpecs(FileOutputFormat.java:146)
        at org.apache.hadoop.mapreduce.JobSubmitter.checkSpecs(JobSubmitter.java:458)
        at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:343)

I am just trying to read an existing file. Any ideas what I am missing here? Appreciate any help.

Exception tells you that your output directory already exists but it should not. Delete it or change its name.

Moreover the name of your output directory 'userdata.txt' looks like the name of a file. So check you are not mistaken in your input/output directories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM