不了解分布式路径中的路径

egmofgnx  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(367)

从下面的代码我不明白两件事: DistributedCache.addcachefile(new URI ('/.dat'), job.getconfiguration()) 我不明白uri路径必须存在于hdfs中。如果我错了,请纠正我。
那是什么 p.getname().equals() 从以下代码:

  1. public class MyDC {
  2. public static class MyMapper extends Mapper < LongWritable, Text, Text, Text > {
  3. private Map < String, String > abMap = new HashMap < String, String > ();
  4. private Text outputKey = new Text();
  5. private Text outputValue = new Text();
  6. protected void setup(Context context) throws
  7. java.io.IOException, InterruptedException {
  8. Path[] files = DistributedCache.getLocalCacheFiles(context.getConfiguration());
  9. for (Path p: files) {
  10. if (p.getName().equals("abc.dat")) {
  11. BufferedReader reader = new BufferedReader(new FileReader(p.toString()));
  12. String line = reader.readLine();
  13. while (line != null) {
  14. String[] tokens = line.split("\t");
  15. String ab = tokens[0];
  16. String state = tokens[1];
  17. abMap.put(ab, state);
  18. line = reader.readLine();
  19. }
  20. }
  21. }
  22. if (abMap.isEmpty()) {
  23. throw new IOException("Unable to load Abbrevation data.");
  24. }
  25. }
  26. protected void map(LongWritable key, Text value, Context context)
  27. throws java.io.IOException, InterruptedException {
  28. String row = value.toString();
  29. String[] tokens = row.split("\t");
  30. String inab = tokens[0];
  31. String state = abMap.get(inab);
  32. outputKey.set(state);
  33. outputValue.set(row);
  34. context.write(outputKey, outputValue);
  35. }
  36. }
  37. public static void main(String[] args)
  38. throws IOException, ClassNotFoundException, InterruptedException {
  39. Job job = new Job();
  40. job.setJarByClass(MyDC.class);
  41. job.setJobName("DCTest");
  42. job.setNumReduceTasks(0);
  43. try {
  44. DistributedCache.addCacheFile(new URI("/abc.dat"), job.getConfiguration());
  45. } catch (Exception e) {
  46. System.out.println(e);
  47. }
  48. job.setMapperClass(MyMapper.class);
  49. job.setMapOutputKeyClass(Text.class);
  50. job.setMapOutputValueClass(Text.class);
  51. FileInputFormat.addInputPath(job, new Path(args[0]));
  52. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  53. job.waitForCompletion(true);
  54. }
  55. }
n53p2ov0

n53p2ov01#

分布式缓存的思想是在任务节点开始执行之前使一些静态数据对其可用。
文件必须存在于hdfs中,这样就可以将其添加到分布式缓存(每个任务节点)
getlocalcachefile基本上获取该任务节点中存在的所有缓存文件。由 if (p.getName().equals(".dat")) { 您正在获取应用程序要处理的相应缓存文件。
请参考以下文件:
https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html#distributedcache
https://hadoop.apache.org/docs/r1.2.1/api/org/apache/hadoop/filecache/distributedcache.html#getlocalcachefiles(org.apache.hadoop.conf.configuration)

xmjla07d

xmjla07d2#

distributedcache是一个api,用于在内存中添加一个文件或一组文件,无论map reduce是否工作,它都可用于每个数据节点。使用distributedcache的一个例子是Map端连接。
distributedcache.addcachefile(new uri('/.dat'),job.getconfiguration())将在缓存区域中添加.dat文件。缓存中可以有n个文件,p.getname().equals(“.dat”)将检查所需的文件。hdfs中的每条路径都将在路径[]下进行map reduce处理。例如:

  1. FileInputFormat.addInputPath(job, new Path(args[0]));
  2. FileOutputFormat.setOutputPath(job, new Path(args[1]));

第一个路径(args[0])是传递的第一个参数(输入文件位置),而jar执行和路径(args[1])是传递输出文件位置的第二个参数。一切都作为路径数组。
同样,当您将任何文件添加到缓存时,它将在您应该使用下面的代码检索的路径数组中进行排列。
path[]files=distributedcache.getlocalcachefiles(context.getconfiguration());
它将返回缓存中存在的所有文件,并通过p.getname().equals()方法返回文件名。

相关问题