import org.apache.spark.sql.SparkSession
import java.net.URLDecoder
import org.apache.hadoop.hdfs.DistributedFileSystem
import org.apache.hadoop
object SparkMapreduce {
def main(args: Array[String]) {
val session = SparkSession.builder().master("local[2]").getOrCreate();
val sc = session.sparkContext;
val path = SparkMapreduce.getClass.getResource("readme.md");
val pathStr = path.getFile;
println(pathStr);
val textFile = sc.textFile("file://" + pathStr)
val count = textFile.count()
println(count);
val linesWithSpark = textFile.filter(line => {
//println(line);
line.contains("UnderConstruction")
})
//把 collect去除后不会打印,说明了 transform和action的区别
linesWithSpark.collect().map { println(_) }
val url = SparkMapreduce.getClass.getResource("分类预测.csv");
//中文会被编码,此处反编码
val predictionPath = URLDecoder.decode(url.getPath, "utf-8");
val predictionDF = "file://" + predictionPath;
val predictionRDD = sc.textFile(predictionDF, 3)
//我们将文本内容打印出来。
predictionRDD.collect().map { println(_) }
}
}