main函数里:
Configuration conf = new Configuration();
Job job = new Job(conf, "kmeans job");
URI centersFile = new URI(
"hdfs://Master1Hadoop/user/image/centers/centersfile#cf"); // 设定要读入文件的信息 //centersfile为要分布式缓存的数据,#cf是为它起个别名
job.addCacheFile(centersFile); // 加入Cache,新API
Class xxx extends Mapper里:
public void setup(Context context) throws IOException, InterruptedException {
URI[] caches = context.getCacheFiles();
if (caches == null || caches.length <= 0) {
System.out.println("centers file does not exist");
System.exit(1);
}
BufferedReader br = new BufferedReader(new FileReader("./cf")); // 读取文件,这里的cf正是main里起得别名
String line;
while ((line = br.readLine()) != null) { // 每次读一行
System.out.println(line);
}
br.close;
}