DistributedCache的使用方法 制定文件到链接不用关心实际路径
来源:互联网 发布:男士加绒牛仔裤淘宝网 编辑:程序博客网 时间:2024/06/01 09:45
http://www.cnblogs.com/xuxm2007/archive/2011/06/30/2094397.html
Hadoop的分布式缓存机制使得一个job的所有map或reduce可以访问同一份文件。在任务提交后,hadoop将由-files和-archive选项指定的文件复制到HDFS上(JobTracker的文件系统)。在任务运行前,TaskTracker从JobTracker文件系统复制文件到本地磁盘作为缓存,这样任务就可以访问这些文件。对于job来说,它并不关心文件是从哪儿来的。在使用DistributedCache时,对于本地化文件的访问,通常使用Symbolic Link来访问,这样更方便。通过 URI hdfs://namenode/test/input/file1#myfile 指定的文件在当前工作目录中被符号链接为myfile。这样job里面可直接通过myfile来访问文件,而不用关心该文件在本地的具体路径。
示例如下:
在这个程序中,我们创建了一个符号链接,即god.txt指向HDFS上的文件/test/file/file.1。这样程序里就可以直接打开god.txt进行文件读取,而不用关心HDFS上的文件/test/file/file.1本地化后的真正路径。
import
java.io.BufferedReader;
import
java.io.FileReader;
import
java.io.IOException;
import
java.net.URI;
import
java.util.StringTokenizer;
import
org.apache.hadoop.conf.Configuration;
import
org.apache.hadoop.filecache.DistributedCache;
import
org.apache.hadoop.fs.Path;
import
org.apache.hadoop.io.IntWritable;
import
org.apache.hadoop.io.Text;
import
org.apache.hadoop.mapreduce.Job;
import
org.apache.hadoop.mapreduce.Mapper;
import
org.apache.hadoop.mapreduce.Reducer;
import
org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import
org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import
org.apache.hadoop.util.GenericOptionsParser;
public
class
WordCount202 {
public
static
void
UseDistributedCacheBySymbolicLink()
throws
Exception {
FileReader reader =
new
FileReader(
"god.txt"
);
BufferedReader br =
new
BufferedReader(reader);
String s1 =
null
;
while
((s1 = br.readLine()) !=
null
) {
System.out.println(s1);
}
br.close();
reader.close();
}
public
static
class
TokenizerMapper
extends
Mapper<Object, Text, Text, IntWritable> {
private
final
static
IntWritable one =
new
IntWritable(
1
);
private
Text word =
new
Text();
protected
void
setup(Context context)
throws
IOException,
InterruptedException {
System.out.println(
"Now, use the distributed cache and syslink"
);
try
{
UseDistributedCacheBySymbolicLink();
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
void
map(Object key, Text value, Context context)
throws
IOException, InterruptedException {
StringTokenizer itr =
new
StringTokenizer(value.toString());
while
(itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public
static
class
IntSumReducer
extends
Reducer<Text, IntWritable, Text, IntWritable> {
private
IntWritable result =
new
IntWritable();
public
void
reduce(Text key, Iterable<IntWritable> values,
Context context)
throws
IOException, InterruptedException {
int
sum =
0
;
for
(IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public
static
void
main(String[] args)
throws
Exception {
Configuration conf =
new
Configuration();
String[] otherArgs =
new
GenericOptionsParser(conf, args)
.getRemainingArgs();
if
(otherArgs.length !=
2
) {
System.err.println(
"Usage: wordcount <in> <out>"
);
System.exit(
2
);
}
DistributedCache.createSymlink(conf);
String path =
"/test/file/file.1"
;
Path filePath =
new
Path(path);
String uriWithLink = filePath.toUri().toString() +
"#"
+
"god.txt"
;
DistributedCache.addCacheFile(
new
URI(uriWithLink), conf);
Job job =
new
Job(conf,
"word count"
);
job.setJarByClass(WordCount202.
class
);
job.setMapperClass(TokenizerMapper.
class
);
job.setCombinerClass(IntSumReducer.
class
);
job.setReducerClass(IntSumReducer.
class
);
job.setOutputKeyClass(Text.
class
);
job.setOutputValueClass(IntWritable.
class
);
FileInputFormat.addInputPath(job,
new
Path(otherArgs[
0
]));
FileOutputFormat.setOutputPath(job,
new
Path(otherArgs[
1
]));
System.exit(job.waitForCompletion(
true
) ?
0
:
1
);
}
}
程序运行的结果是在jobtracker中可以看到打印后的/test/file/file.1文件的内容。
如果程序中要用到很多小文件,那么使用Symbolic Link将非常方便。
- DistributedCache的使用方法 制定文件到链接不用关心实际路径
- 将制定文件路径下的文件内容合并到一个文件
- vs2013制定加载动态链接库的路径
- DistributedCache的使用方法(hadoop0.20.2)
- Hadoop的分布式缓存DistributedCache使用方法
- 获取IE的实际文件路径
- 怎样筛选制定路径下的目录文件
- 制定路径下所有文件里的目标字符串替换
- python实现,按文件名搜索制定路径下的文件
- 接口的实际应用--制定标准
- 拷贝文件到制定目录
- 用分布式缓存文件存储数据到本地 DistributedCache
- 保存tomcat输出的日志到制定文件
- 重新制定tab【frametab】的url路径,并且立即跳转到该路径
- 如何将JBOSS AS 7.1.1的访问路径制定到根路径下
- errorPage的实际使用方法
- JAVA 读取 制定路径的 XML 文件 和 获取 服务器路径
- C# 获取制定文件所在路径
- 什么是4G
- 模板当中使用变量
- JSP标准标签库
- 数据结构复习之[循环队列]
- 算法导论15.1-3
- DistributedCache的使用方法 制定文件到链接不用关心实际路径
- 在SMARTY中使用变量(一)
- apk反编译及安装位置的更改
- qt/qte qtopia编译问题集锦
- ACM 算法艺术与信息学竞赛 1.2.2 钓鱼 Gone Fishing
- Percona 5.5.16与官方MySQL5.5.16的简单TPS对比
- smarty(四)
- 无线通信协议小感
- php循环创建文件夹