- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2021 05:56 AM
Python Function:
def read_schema(arg):
d_types = {
"varchar":StringType(),
"integer":IntegerType(),
"timestamp":TimestampType(),
"double":DoubleType(),
"date":DateType(),
"decimal":DecimalType()
}
split_values= arg.split(",")
sch= StructType()
for i in split_values:
x=i.split("|")
sch.add(x[0],d_types[x[1]],True)
return sch
Sample schema Input file:
id|integer,
cust_nr|varchar,
bus_name|varchar,
bus_tym|varchar,
bus_desciprion|varchar
The code need to be implemented in the pyspark job:
textRDD1 = sc.textFile("Schema_file.txt")
llist = textRDD1.collect()
listToStr = ''.join([str(elem) for elem in llist])
sch = read_schema(listToStr)
df= spark.read.csv(path='Table_PATH',schema=sch, header=False, sep='|')
df.show()