基本上,我正在尝试使用2个csv文件,因此第一个输入csv文件将由用户输入,但第二个csv文件我刚刚在mapper代码中定义,所以当映射器继续运行时,有一些方法,在没有一些循环的mapper类中定义的第二个csv文件中获取一些值?我这样做的原因是我想使用第二个csv文件中的值和第一个映射器中的两个值来制作映射器的一些关键值。 非常感谢您的帮助和时间。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class StubMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private Text outkey = new Text();
//private MinMaxCountTuple outTuple = new MinMaxCountTuple();
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
try {
String csvFile = "/workspace/project/src/subject.csv";
if(csvFile.toString().startsWith("BibNumber"))
{
return;
}
String subject[] = csvFile.toString().split(",");
String BookName = subject[1];
if(value.toString().startsWith("BibNumber"))
{
return;
}
String data[] = value.toString().split(",");
String BookType = data[2];
String DateTime = data[5];
SimpleDateFormat frmt = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
Date creationDate = frmt.parse(DateTime);
frmt.applyPattern("dd-MM-yyyy");
String dateTime = frmt.format(creationDate);
outkey.set(BookName + ", " + BookType + ", " + dateTime);
//outUserId.set(userId);
context.write(outkey, new IntWritable(1));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
我相信你需要这样的东西
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final String csvFile = "/workspace/project/src/subject.csv";
private List<String> csvData = new ArrayList<>();
protected void setup(Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
FileSystem fs = FileSystem.get(conf);
Path p = new Path(csvFile); // this file must be on HDFS
// read csvFile from fs
// store CSV records in some field
}
在map方法中,您循环遍历csvData