Using Pyspark how to convert Text file to CSV file - apache-spark

I am new learner for Pyspark. I got a requirement in my project to read JSON file with a schema and need to convert it to CSV file.
Can some one help me how to proceed this request using PYspark.

You can load JSON and write CSV with SparkSession.
spark = SparkSession.builder.master("local").appName("ETL").getOrCreate()
spark.read.json(path-to-txt)
spark.write.csv(path-to-csv)

Related

Read avro file with bytes schema in spark

I am trying to read some avro files into a Spark dataframe and have the below sitution:
The avro file schema is defined as
Schema(
org.apache.avro.Schema
.create(org.apache.avro.Schema.Type.BYTES),
"ByteBlob", "1.0");
The file has a nested json structure stored as a simple bytes schema in the avro file.
I can't seem to find a way to read this into a dataframe in spark. Any pointers on how I can read files like these?
Output from avro-tools:
hadoop jar avro-tools/avro-tools-1.10.2.jar getmeta /projects/syslog_paranoids/encrypted/dhr/complete/visibility/zeeklog/202207251345/1.0/202207251351/stg-prd-dhrb-edg-003.data.ne1.yahoo.com_1658690707314_zeeklog_1.0_202207251349_202207251349_6c64f2210c568092c1892d60b19aef36.6.avro
avro.schema "bytes"
avro.codec deflate
The tojson function within avro-tools is able to read the file properly and return a json output contained in the file.

Avro append a record with non-existent schema and save as an avro file?

I have just started using Avro and I'm using fastavro library in Python.
I prepared a schema and saved data with this one.
Now, I need to append new data (JSON response from an API call ) and save it with a non-existent schema to the same avro file.
How shall I proceed to add the JSON response with no predefined schema and save it to the same Avro file?
Thanks in advance.
Avro files, by definition, already have a schema within them.
You could read that schema first, then continue to append data, or you can read entire file into memory, then append your data, then overwrite the file.
Each option require you to convert the JSON into Avro (or at least a Python dict), though.

difference between spark read textFile and csv

I am trying to read a text file delimited by |. I am trying this
spark.read.format("com.databricks.spark.csv").option("header","true").option("delimiter", "|").option("inferSchema","true").csv("/tmp/file.txt").show()
I am only reading/seeing only the header but no data.
When I try the same with textFile, I am getting data but all in one column
spark.read.format("com.databricks.spark.csv").option("header","true").option("delimiter", "|").option("inferSchema","true").textFile("/tmp/file.txt").show()
Is there a way to read data via csv? I am using spark 2.4.4
The reason for the issue was the file is in UTF16 so I had to convert it and do run dostounix on it. Thanks for your advice. Apologies I really did not know that

Which file formats can I save a pyspark dataframe as?

I would like to save a huge pyspark dataframe as a Hive table. How can I do this efficiently? I am looking to use saveAsTable(name, format=None, mode=None, partitionBy=None, **options) from pyspark.sql.DataFrameWriter.saveAsTable.
# Let's say I have my dataframe, my_df
# Am I able to do the following?
my_df.saveAsTable('my_table')
My question is which formats are available for me to use and where can I find this information for myself? Is OrcSerDe an option? I am still learning about this. Thank you.
Following file formats are supported.
text
csv
ldap
json
parquet
orc
Referece: https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/DataFrameWriter.scala
So I was able to write the pyspark dataframe to a compressed Hive table by using a pyspark.sql.DataFrameWriter. To do this I had to do something like the following:
my_df.write.orc('my_file_path')
That did the trick.
https://spark.apache.org/docs/1.6.0/api/python/pyspark.sql.html#pyspark.sql.DataFrame.write
I am using pyspark 1.6.0 btw

How to load JSON(path saved in csv) with Spark?

I am new to Spark.
I can load the .json file in Spark. What if there are thousands of .json files in a folder. picture of .json files in the folder
And I have a csv file, which classifies the .json files with labels.picture of csv file
What should I do with Spark if I want to load and save the data.(for example.I want to load the first information in csv, but it is text information. But it gives the path of .json, and I want to load the .json, then save the output. So I will know the first Trusted label graph's json information.)
For the JSON:
jsonRDD = sql_context.read.json("path/to/json_folder/");
For CSV install spark-csv from here Databricks' spark-csv
csvRDD = sql_context.read.load("path/to/csv_folder/",format='com.databricks.spark.csv',header='true',inferSchema='true')

Resources