[英]Error showing to read CSV file for mapreduce program in java
下面的代碼是mapreduce
Mapper
類。 我正在嘗試編寫的代碼是讀取CSV
文件並在每行中存儲兩列數據(第1列表示userId
,第6列顯示書的CheckOutDateTime
)到HashMap
。 我想,我的功能碼getMapFromCSV
在StubMapper
類似乎是錯誤的。 有人可以啟發我嗎? 在底部,我將輸出顯示為錯誤。 謝謝大家的幫助和建議。
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)
錯誤出現在此行:
BufferedReader in = new BufferedReader(new FileReader(filePath));
filePath
的值 filePath
您缺少mapreduce
重要概念。 問題在於下面的線
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// Below is the problematic line
HashMap<String, String> parsed = getMapFromCSV(value.toString());
也許您假設Text value
是CSV filename
,因此嘗試從文件中獲取該值。
它不是那樣工作的。 輸入到映射器的“ Text value
是CSV文件中的一行。
假設您的CSV格式如下:
Code,Description,Code Type,Format Group,Format Subgroup,Category Group,Category Subgroup
111,sample description,codeType1,IN,....
您的代碼應類似於
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
if(value.toString().startWith("Code,Description")){
// Skip header line (first line) of CSV
return;
}
String data[] = value.toString().split(",", -1);
String code= data[0];
String codeType = data[2];
....
....
and so one
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.