在 Mapreduce 中,我想知道有什么方法可以使用 java 使用 2 个 csv 文件
In Mapreduce, I am wondering there is some way to use 2 csv files using java
基本上,我正在尝试使用 2 个 csv 文件,因此第一个输入 csv 文件将由用户输入,但第二个 csv 文件是我刚刚在映射器代码中定义的,所以当映射器保持 运行,在没有循环的情况下在映射器 class 中定义的第二个 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();
}
}
}
我相信你需要这样的东西
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
基本上,我正在尝试使用 2 个 csv 文件,因此第一个输入 csv 文件将由用户输入,但第二个 csv 文件是我刚刚在映射器代码中定义的,所以当映射器保持 运行,在没有循环的情况下在映射器 class 中定义的第二个 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();
}
}
}
我相信你需要这样的东西
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