【发布时间】:2018-03-26 19:38:43
【问题描述】:
下面的代码是mapreduce 中的Mapper 类。我要编写的代码是读取CSV 文件并在每行中存储两列数据(第1 列表示userId,第6 列表示书籍CheckOutDateTime)到HashMap。我认为我在StubMapper 类中的getMapFromCSV 函数代码似乎是错误的。有人可以启发我吗?在底部,我将输出用于错误。感谢大家的任何帮助和建议。
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.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class StubMapper extends Mapper<LongWritable, Text, Text, MinMaxCountTuple> {
private Text outUserId = new Text();
private MinMaxCountTuple outTuple = new MinMaxCountTuple();
private final static SimpleDateFormat frmt =
new SimpleDateFormat("yyyy-MM--dd'T'HH:mm:ss.SSS");
public static HashMap<String, String> getMapFromCSV(String filePath) throws IOException
{
HashMap<String, String> words = new HashMap<String, String>();
BufferedReader in = new BufferedReader(new FileReader(filePath));
String line;
//= in.readLine())
while ((line = in.readLine()) != null) {
String columns[] = line.split("\t");
if (!words.containsKey(columns[1])) {
words.put(columns[1], columns[6]);
}
}
//in.close();
return words;
}
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
HashMap<String, String> parsed = getMapFromCSV(value.toString());
//String columns[] = value.toString().split("\t");
String strDate = parsed.get("CheckoutDateTime");
//String userId = columns[1];
//String strDate = columns[6];
String userId = parsed.get("BibNumber");
try {
Date creationDate = frmt.parse(strDate);
outTuple.setMin(creationDate);
outTuple.setMax(creationDate);
outTuple.setCount(1);
outUserId.set(userId);
context.write(outUserId, outTuple);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
并显示我无法弄清楚的跟随错误。我认为问题似乎发生在 StubMapper 类中的 getMapFromCSV 函数中。
该函数的参数将具有CSV 属性的信息。我试图存储到HashMap 中的是键和值作为一对。但是,我不知道如何改变。请说明您是否知道我可以如何解决它。
java.io.FileNotFoundException: Code,Description,Code Type,Format Group,Format Subgroup,Category Group,Category Subgroup (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at StubMapper.getMapFromCSV(StubMapper.java:27)
at StubMapper.map(StubMapper.java:50)
at StubMapper.map(StubMapper.java:14)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:140)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:673)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:331)
at org.apache.hadoop.mapred.Child$4.run(Child.java:268)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1408)
at org.apache.hadoop.mapred.Child.main(Child.java:262)
【问题讨论】:
-
添加了有关代码问题的详细信息。请检查。
标签: java csv mapreduce bigdata