1.读文件
通过 sc.textFile(“file://")方法来读取文件到rdd中。
val lines = sc.textFile("file://")//文件地址或者HDFS文件路径
本地地址
"file:///home/hadoop/spark-1.6.0-bin-hadoop2.6/examples/src/main/resources/people.json"
HDFS文件地址
"hdfs://112.74.21.122:9000/user/hive/warehouse/hive_test"
2.保存文件
通过sc.saveAsTextFile("file://")把 rdd 内容保存到文件
rdd.saveAsTextFile("file:///home/writeout.txt");//把rdd写入/home/writeout.txt
但是我们打开/home文件夹,发现writeout并不是txt文件而是一个文件夹,我们打开文件夹,结构如下
我们保存错了嘛?没有,这时正常的。part-00000代表的是分区,如果有多个分区,会有多个part-xxxxxx的文件。
如果我们要再次读取这个保存的文件并不需要一个一个分区读取,直接读取就可以了,spark会自动加载所有分区数据。
val rdd = sc.textFile(