I am trying to read a simple text file into a Spark RDD and I see that there are two ways of doing so :
from pyspark.sql import SparkSession
spark = SparkSession.builder.master("local[*]").getOrCreate()
sc = spark.sparkContext
textRDD1 = sc.textFile("hobbit.txt")
textRDD2 = spark.read.text('hobbit.txt').rdd
then I look into the data and see that the two RDDs are structured differently
textRDD1.take(5)
['The king beneath the mountain',
'The king of carven stone',
'The lord of silver fountain',
'Shall come unto his own',
'His throne shall be upholden']
textRDD2.take(5)
[Row(value='The king beneath the mountain'),
Row(value='The king of carven stone'),
Row(value='The lord of silver fountain'),
Row(value='Shall come unto his own'),
Row(value='His throne shall be upholden')]
Based on this, all subsequent processing has to be changed to reflect the presence of the 'value'
My questions are
- What is the implication of using these two ways of reading a text file?
- Under what circumstances should we use which method?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…