Hadoop分布式缓存怎么使用

发布时间:2021-12-09 14:29:54 作者:iii
来源:亿速云 阅读:164

本篇内容介绍了“Hadoop分布式缓存怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1.前言

DistributedCache是hadoop框架提供的一种机制,可以将job指定的文件,在job执行前,先行分发到task执行的机器上,并有相关机制对cache文件进行管理。


DistributedCache 可将具体应用相关的、大尺寸的、只读的文件有效地分布放置。DistributedCache 是Map/Reduce框架提供的功能,能够缓存应用程序所需的文件 (包括文本,档案文件,jar文件等)。


Map-Redcue框架在作业所有任务执行之前会把必要的文件拷贝到slave节点上。 它运行高效是因为每个作业的文件只拷贝一次并且为那些没有文档的slave节点缓存文档。


DistributedCache 根据缓存文档修改的时间戳进行追踪。 在作业执行期间,当前应用程序或者外部程序不能修改缓存文件。


distributedCache可以分发简单的只读数据或文本文件,也可以分发复杂类型的文件例如归档文件和jar文件。归档文件(zip,tar,tgz和tar.gz文件)在slave节点上会被解档(un-archived)。 这些文件可以设置执行权限。


用户可以通过设置mapred.cache.{files|archives}来分发文件。 如果要分发多个文件,可以使用逗号分隔文件所在路径。


DistributedCache可在map/reduce任务中作为 一种基础软件分发机制使用。它可以被用于分发jar包和本地库(native libraries)。 

DistributedCache.addArchiveToClassPath(Path, Configuration)和 DistributedCache.addFileToClassPath(Path, Configuration) API能够被用于缓存文件和jar包,并把它们加入子jvm的classpath。也可以通过设置配置文档里的属性 mapred.job.classpath.{files|archives}达到相同的效果。缓存文件可用于分发和装载本地库。

添加缓存文件:

DistributedCache.addCacheFile(URI,conf)DistributedCache.addCacheArchive(URI,conf) DistributedCache.setCacheFiles(URIs,conf)DistributedCache.setCacheArchives(URIs,conf)
其中URI的形式是 hdfs目录格式

缓存Jar:

DistributedCache.addArchiveToClassPath(Path, Configuration)和 DistributedCache.addFileToClassPath(Path, Configuration) API能够被用于 缓存文件和jar包,并把它们加入子jvm的classpath。


DistributedCache.createSymlink(Configuration)方法让DistributedCache 在当前工作目录下创建到缓存文件的符号链接。或者通过设置配置文件属性mapred.create.symlink为yes。 分布式缓存会截取URI的片段作为链接的名字。 

例如,URI是 hdfs://namenode:port/lib.so.1#lib.so,则在task当前工作目录会有名为lib.so的链接,它会链接分布式缓存中的lib.so.1。

2.常见应用场景

(1)分发第三方库(jar,so等);
(2)分发算法需要的词典文件;
(3)分发程序运行需要的配置;
(4)分发多表数据join时小表数据简便处理等

3.注意事项

4.基本流程

5.应用实例


public class MapJoinByCache {
public static class MapJoiner extends Mapper<LongWritable,Text,Text,Text>    {   static Map<String,String> movies=new HashMap<String,String>();public void setup(Context context) {            try {                FileReader reader = new FileReader("movies.dat");                BufferedReader br = new BufferedReader(reader);String s1 = null;while ((s1 = br.readLine()) != null)                {                    System.out.println(s1);String[] splits= s1.split("::");                    String movieId=splits[0];String movieName =splits[1];                    movies.put(movieId, movieName);                                }                br.close();                reader.close();            } catch (Exception e) {                e.printStackTrace();            }        }private Text outKey=new Text();private Text outVal=new Text();     public void map(LongWritable key,Text value,Context context)throws IOException, InterruptedException        {if(value!=null||value.toString()!=null)            {String[] splits = value.toString().split("::");  String movieId =splits[1];String movieName= movies.get(movieId);                outKey.set(movieId);                outVal.set(movieName+"::"+value.toString());                context.write(outKey, outVal);            }        }    }public static class DirectReducer extends Reducer<Text,Text,NullWritable,Text>    {        NullWritable outKey=NullWritable.get();public void reduce(Text key,Iterable<Text> values,Context context)throws IOException, InterruptedException        {for(Text value :values)            {context.write(outKey, value);            }        }    }public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException, ClassNotFoundException {        Configuration conf =new Configuration();String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();        DistributedCache.createSymlink(conf);        DistributedCache.addCacheFile(new URI("hdfs://mylinux:9000/data/exam/movie/movies.dat#movies.dat"), conf);              Job job=new Job(conf);        job.setJobName("Join on Map Side");        job.setJarByClass(MapJoinByCache.class);        job.setMapperClass(MapJoiner.class);        job.setReducerClass(DirectReducer.class);        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));        job.setMapOutputKeyClass(Text.class);        job.setMapOutputValueClass(Text.class);        job.setOutputKeyClass(NullWritable.class);        job.setOutputValueClass(Text.class);        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));                System.exit(job.waitForCompletion(true) ? 0 : 1);    }

“Hadoop分布式缓存怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 基于redis分布式缓存实现
  2. 如何实现redis分布式缓存

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

hadoop

上一篇:Spark Shuffle和Hadoop Shuffle有哪些区别

下一篇:配置HDFS高可用后hive启动报错怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》