Map[Reduce] 的 setup 中读取 HDFS 文件夹信息
2015-12-31 14:50
316 查看
有时候,我们想在 Map 或者 Reduce 执行前读取一些数据信息(量相对比较小),又不想通过另一个 Map 来读取,就可以在 该Map 的 setup 中来完成该操作。
相关定义
1.在HDFS上某一文件夹下存放用户信息列表:
/user/data/userinfo/part-00000
/user/data/userinfo/part-00001
...
/user/data/userinfo/part-00020
2.每行数据格式:
userid + \t + [其他参数]
/**
* Map.
*/
public static class TestMapper extends
Mapper<Text, Text, NullWritable, NullWritable>{
/** the list of userId. */
ArrayList<Integer> userIdList = null;
/** the path of userId. */
private String userIdFilePath = "";
/*
* 读取指定HDFS路径下所有文件的数据.
*/
public void readUserIdInfo(Context context) throws IOException{
if(userIdList == null){
userIdList = new ArrayList<Integer>();
}else if(!userIdList.isEmpty()){
userIdList.clear();
}
FileSystem fs = FileSystem.get(context.getConfiguration());
// 读取文件列表
Path filePath =new Path(userIdFilePath);
FileStatus stats[] = fs.listStatus(filePath);
String s = "";
// 依次处理每个文件
for(int i = 0; i < stats.length; ++i){
Path inFile =new Path(stats[i].getPath().toString());
FSDataInputStream fin = fs.open(inFile);
BufferedReader input = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
// 处理当前文件数据
while ((s = input.readLine()) != null) {
String[] items = s.split("\t");
// 暂时只需要 userId 字段
int userId = Integer.parseInt(items[0]);
if(!userIdList.contains(userId)){
userIdList.add(userId);
}
}
// 释放
if (input != null) {
input.close();
input = null;
}
if (fin != null) {
fin.close();
fin = null;
}
}
/* 测试信息
System.out.println("userid count" + userIdList.size());
*/
}
@Override
protected void setup(Context context) throws IOException {
// 读取数据
userIdFilePath = context.getConfiguration().get("userIdFilePath");
readUserIdInfo(context);
}
@Override
protected void cleanup(Context context) throws IOException {
// 释放数据
if(!userIdList.isEmpty()){
userIdList.clear();
}
}
@Override
public void map(Text key, Text value, Context context)
throws IOException, InterruptedException {
// map 操作
}
}
说明
由于每个map [reduce] 执行前后,都会执行一次对应的 setup、cleanup 函数。
如果map 数为20,上述 HDFS 数据会被重复读取20次。
相关定义
1.在HDFS上某一文件夹下存放用户信息列表:
/user/data/userinfo/part-00000
/user/data/userinfo/part-00001
...
/user/data/userinfo/part-00020
2.每行数据格式:
userid + \t + [其他参数]
/**
* Map.
*/
public static class TestMapper extends
Mapper<Text, Text, NullWritable, NullWritable>{
/** the list of userId. */
ArrayList<Integer> userIdList = null;
/** the path of userId. */
private String userIdFilePath = "";
/*
* 读取指定HDFS路径下所有文件的数据.
*/
public void readUserIdInfo(Context context) throws IOException{
if(userIdList == null){
userIdList = new ArrayList<Integer>();
}else if(!userIdList.isEmpty()){
userIdList.clear();
}
FileSystem fs = FileSystem.get(context.getConfiguration());
// 读取文件列表
Path filePath =new Path(userIdFilePath);
FileStatus stats[] = fs.listStatus(filePath);
String s = "";
// 依次处理每个文件
for(int i = 0; i < stats.length; ++i){
Path inFile =new Path(stats[i].getPath().toString());
FSDataInputStream fin = fs.open(inFile);
BufferedReader input = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
// 处理当前文件数据
while ((s = input.readLine()) != null) {
String[] items = s.split("\t");
// 暂时只需要 userId 字段
int userId = Integer.parseInt(items[0]);
if(!userIdList.contains(userId)){
userIdList.add(userId);
}
}
// 释放
if (input != null) {
input.close();
input = null;
}
if (fin != null) {
fin.close();
fin = null;
}
}
/* 测试信息
System.out.println("userid count" + userIdList.size());
*/
}
@Override
protected void setup(Context context) throws IOException {
// 读取数据
userIdFilePath = context.getConfiguration().get("userIdFilePath");
readUserIdInfo(context);
}
@Override
protected void cleanup(Context context) throws IOException {
// 释放数据
if(!userIdList.isEmpty()){
userIdList.clear();
}
}
@Override
public void map(Text key, Text value, Context context)
throws IOException, InterruptedException {
// map 操作
}
}
说明
由于每个map [reduce] 执行前后,都会执行一次对应的 setup、cleanup 函数。
如果map 数为20,上述 HDFS 数据会被重复读取20次。
相关文章推荐
- Ruby 之 class 中的 private、 protected、public
- Ruby定义私有方法(private)的两种办法
- C#获取文件夹及文件的大小与占用空间的方法
- 用批处理实现的创建带日期的文件夹的代码
- win2003文件夹权限设置脚本代码
- 计算机信息处理
- win2003 服务器 文件夹 权限分配
- 解析MYSQL显示表信息的方法
- C#编程实现获取文件夹中所有文件的文件名
- PowerShell统计文件夹下文件个数的方法
- C#遍历文件夹后上传文件夹中所有文件错误案例分析
- C#获取进程或线程相关信息的方法
- C#监控文件夹并自动给图片文件打水印的方法
- VBS 显示“选择文件或文件夹”对话框的代码
- C#实现获取文件夹大小的方法
- C#递归实现显示文件夹及所有文件并计算其大小的方法
- Linux系统下php获得系统分区信息的方法
- php类中private属性继承问题分析
- C#实现为类和函数代码自动添加版权注释信息的方法
- BAT批处理之文件与文件夹操作代码(附xcopy命令详解)