MapReduce使用DistributedCache
来源:互联网 发布:手机版解压软件 编辑:程序博客网 时间:2024/05/22 15:13
会有这样一种场景:有个非常大的文件需要和一个很小的文件去关联,即:一张大表去关联一张小表。
如果是在Hive里可以用MapJoin的方式,但是这种方式不是很灵活虽然也支持不等值连接,例如:有这样一个需求,有一个很大的日志文件,这个文件中有个字段是用户的IP(这里的IP是长整形),有一个很小的文件记录了IP范围对应的地域信息,我需要知道这些用户的地域分布信息,如果用MapJoin的话需要用大于和小于去比较,这种方式经测试发现效率极低。
于是想到了DistributedCache的方式,DistributedCache翻译过来就是分布式缓存,把小文件载入到内存中,在MapReduce的时候直接和内存中的小文件进行关联即可。实现思路:
1、使用DistributedCache将保存IP地域信息的文件加载至内存;
2、在map里每处理一条日志就在内存中查找符合这个用户IP的地域(这里使用二分查找,效率还不错)。
Map类的代码如下:
public static class ConfigStatistic2Mapper extendsMapper<LongWritable, Text, Text, IntWritable> {ArrayList<String> ipLocationList = new ArrayList<String>();@SuppressWarnings("deprecation")protected void setup(Context context) throws IOException,InterruptedException {Configuration conf = context.getConfiguration();Path[] catchFiles = DistributedCache.getLocalCacheFiles(conf);String ipLocation = catchFiles[0].toString();@SuppressWarnings("resource")//读取缓存文件,并将文件的内容保存在ipLocationList 这个List对象中BufferedReader bReader = new BufferedReader(new FileReader(ipLocation));String line = "";while ((line = bReader.readLine()) != null) {ipLocationList.add(line);}}/** * 通过二分查找查询ip的地域 * * @param ip 长整形 * @param ipTable 记录其实IP、结束IP以及这个IP端对应的地域信息 * @return start_ip,end_ip,country,district,city,netservice,1isp 并用‘\001’分隔 */public String getLocation(Long ip, List<String> ipTable) {int begin = 0, end = 0;for (end = ipTable.size() - 1; begin != end;) {int midIndex = (end + begin) / 2;int mid = (end - begin);Long midValue = Long.parseLong(ipTable.get(midIndex).split("\001")[0]);if (ip == midValue) {return ipTable.get(midIndex);}if (ip > midValue) {begin = midIndex;} else {end = midIndex;}if (mid <= 1) {break;}}return (ipTable.get(begin));}protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {......//获取IP对应的地域信息String location = getLocation(Long.parseLong(ip),ipLocationList);......}}
需要在configuration中设置需要分布式缓存的文件,代码如下:
public int run(String[] args) throws Exception {......Configuration conf = getConf();DistributedCache.addCacheFile(new Path("hdfs://std4:8020/user/hive/warehouse/block.db/ip_location/part-m-00000").toUri(), conf);......}就贴了主要用到DistributedCache的代码,其他的还是按照普通的Mapreduce编写即可,经测试日志文件有1.7亿多条记录,用时8分钟左右(7个节点的集群),性能还不错。
0 0
- MapReduce使用DistributedCache
- 在mapreduce任务中使用distributedCache
- 在mapreduce任务中使用distributedCache
- MapReduce基础开发之十一DistributedCache使用
- DistributedCache使用
- hadoop distributedcache使用
- hadoop DistributedCache的使用
- Hadoop DistributedCache使用案例
- DistributedCache的使用
- Hadoop使用DistributedCache进行join
- Hadoop DistributedCache使用及原理
- Hadoop DistributedCache使用及原理
- Hadoop DistributedCache使用及原理
- Hadoop DistributedCache使用及原理
- Hadoop DistributedCache的使用解释
- 如何使用Hadoop的DistributedCache
- Hadoop DistributedCache使用及原理
- Hadoop DistributedCache使用及原理
- CocosBuilder开发之1
- 用Echarts制作各种图表 自己的源代码
- 在felx中打开一个新的网页
- 安卓环境的配置
- 第九周 项目3 实现分数类中的运算符重载
- MapReduce使用DistributedCache
- Voronoi图的代码
- 为Django写我的第一篇blog—Python之Django安装(Windows)
- Linux服务器web相关内核参数注解及TIME_WAIT状态的连接过多解决办法
- TCP/IP和Socket的关系
- hdu 3251 Being a Hero(最小割)
- Qt获取QTextEdit中的内容
- 如何解决更新证书、或者创建SSL证书的时候显示“无法生成证书请求,拒绝访问”等提示信息
- Opencv2.X以上Mat类型与IplImage*的转换