将df.show()的内容另存为pyspark中的字符串

时间:2018-10-05 11:03:25

标签: pyspark

from pyspark.sql import SparkSession

sc = SparkSession.builder.getOrCreate()
prsn = sc.read.format("csv").option("delimiter", ",").option("header", "true").option("inferSchema", "true").load("C:/Users/amit.suar/IdeaProjects/LearningPyspark/prsn.csv")

prsn.show()

+-------------------------+------------------------+---+-----------+-----------------------------+
|PERSON_MEDIA_CONSUMER_KEY|PERSON_MEDIA_CONSUMER_ID|AGE|GENDER_CODE|EDUCATION_LEVEL_CATEGORY_CODE|
+-------------------------+------------------------+---+-----------+-----------------------------+
|                      101|                 3285854| 15|          1|                            1|
|                      102|                 2313090| 25|          1|                            3|
|                      103|                 2295854| 33|          2|                            6|
|                      104|                 2295854| 33|          2|                            6|
|                      105|                 2471554| 26|          2|                            4|
|                      106|                 2471554| 26|          2|                            4|
+-------------------------+------------------------+---+-----------+-----------------------------+

我想将此输出捕获为变量中的字符串。如何实现?

1 个答案:

答案 0 :(得分:0)

有内部/私有函数返回与.show()打印相同的字符串:

# Return dataframe as a table of first n records (20 by default)
dataframe._jdf.showString(n, 20)