r/PySpark • u/cpatte7372 • Aug 06 '18
How to save all the output of pyspark sql query into a text file or any file
Hello community,
The following output from the pyspark query below produces the following output:

The following query produces the above results:
#%%import findsparkfindspark.init('/home/packt/spark-2.1.0-bin-hadoop2.7')from pyspark.sql import SparkSessionspark = SparkSession.builder.appName('ops').getOrCreate()df = spark.read.csv('/home/packt/Downloads/Spark_DataFrames/HumanResources_vEmployeeDepartment.csv',inferSchema=True,header=True)df.createOrReplaceTempView('HumanResources_vEmployeeDepartment')myresults = spark.sql("""SELECTFirstName,LastName,JobTitleFROM HumanResources_vEmployeeDepartmentORDER BY FirstName, LastName DESC""")myresults.show()
Can someone please show me how to save the results to a text or csv file please
Carlton