我有一个Mapper类,如下:
public static class MyMapper extends Mapper<LongWritable,Text, LongWritable,Text>{
String line;
String[] strList;
String outputKey;
@Override
public void map(LongWritable key, Text value, Context c) throws IOException, InterruptedException{
line = value.toString();
strList = line.split(",");
int sVid = Integer.parseInt(strList[1]);
int dVid = Integer.parseInt(strList[3]);
if(strList.length != 5) //handle faulty inputs
return;
c.write(new LongWritable(sVid),value);
c.write(new LongWritable(dVid),value);
}
}
在Hadoop中,使用Job调用Mapper类:
Configuration conf = new Configuration();
Job j = new Job(conf, "Adjacency Generator Job");
j.setMapperClass(AdjMapper.class);
如何在Spark中实现这一目标?如何调用map()
功能?我创建了一个RDD文件,如下所示:
JavaRDD<String> file = sc.textFile("C:\\myFile");
感谢您的帮助。谢谢。
好像您正在读取CSV。自Spark 2.x起,Spark本身就支持这些功能。
不要使用SparkContext使用SparkSession
仅打印数据框,您需要这样的东西
spark.csv("C:\\myFile").show()