ytasfeb15 ytasfeb15 - 1 year ago 136
Python Question

Exporting text files to PostgreSQL using Spark - Automation

I'm trying export text files to Postgres database using Spark. I am using below piece of code to export individual text files. I have close to 200 text files in the same folder and every text file has same structure. Unfortunately year value is not part of my input file hence I'm hard coding it.

I wish to upload all these files at a time but don't know how to do it , anyone has any suggestions please?

from pyspark.sql import SQLContext, Row
sqlContext = SQLContext(sc)

lines = sc.textFile("/aaaa/bbbb/DataFile/t-1870.txt")
splits = l: l.split(","))
raw_data = b: Row(name=b[0], gender=b[1],count=int(b[2]),year=int(1870)))

schemaBabies = sqlContext.createDataFrame(raw_data)

df = sqlContext.sql("select * from raw_data")


df.write.jdbc(url = pgurl ,table = "EDW.raw_data",properties=properties)

Answer Source

Lets assume your data looks like this:

import csv
import tempfile 
import os

out = tempfile.mkdtemp()
data = [
    ("1870", [("Jane Doe", "F", 3)]),
    ("1890", [("John Doe", "M", 1)]),

for year, rows in data: 
    with open(os.path.join(out, "t-{0}.txt".format(year)), "w") as fw:

Start PySpark session or submit script passing correct spark-csv to --packages argument and load data with specified schema:

from pyspark.sql.types import *

schema = StructType([
    StructField("name", StringType(), True),
    StructField("gender", StringType(), True),
    StructField("count", LongType(), True)

df = ("com.databricks.spark.csv")

Extract year from the file name and write:

from pyspark.sql.functions import input_file_name, regexp_extract

df_with_year = (df.withColumn(
    regexp_extract(input_file_name(), "[1-2][0-9]{3}", 0).cast("int")))
## +--------+------+-----+----+
## |    name|gender|count|year|
## +--------+------+-----+----+
## |John Doe|     M|    1|1890|
## |Jane Doe|     F|    3|1870|
## +--------+------+-----+----+


Important: In Spark < 2.0 this approach depends on not passing data around between Python and JVM. It won't work with Python UDFs or

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download