Can we automate spark SQL query generation from AVRO schema? - apache-spark

I'm working on project where everyday I need to deal with tons of AVRO files. To extract the data from AVRO I use sparkSQL. To achieve this first I need to printSchema and then I need to select the fields to see the data. I want to automate this process. Given any input AVRO I want to write a script which will automatically generated SparkSQL query(considering the struct and arrays in avsc file). I'm okay to write a script in Java or Python.
-- Sample input AVRO
root
|-- identifier: struct (nullable = true)
| |-- domain: string (nullable = true)
| |-- id: string (nullable = true)
| |-- version: long (nullable = true)
alternativeIdentifiers: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- identifier: struct (nullable = true)
| | | | |-- domain: string (nullable = true)
| | | | |-- id: string (nullable = true)
-- Output I'm expecting
SELECT identifier.domain, identifier.id, identifier.version

You can use something like this to generate list of columns based on the schema:
import org.apache.spark.sql.types.{StructField, StructType}
def getStructFieldName(f: StructField, baseName: String = ""): Seq[String] = {
val bname = if (baseName.isEmpty) "" else baseName + "."
f.dataType match {
case StructType(s) =>
s.flatMap(x => getStructFieldName(x, bname + f.name))
case _ => Seq(bname + f.name)
}
}
Then it could be used on the real dataframe, like this:
val data = spark.read.json("some_data.json")
val cols = data.schema.flatMap(x => getStructFieldName(x))
as result, we're getting the sequence of strings, that we can use either to do a select:
import org.apache.spark.sql.functions.col
data.select(cols.map(col): _*)
or we can generate a comma-separated list that we can use in the spark.sql:
spark.sql(s"select ${cols.mkString(", ")} from table")

Related

AnalysisException: CSV data source does not support array<struct<

I am at work and I need immediate help please
I have a parquet file and I need to convert it to csv. could u please help me?
error:
AnalysisException: CSV data source does not support array<struct<company:string,dateRange:string,description:string,location:string,title:string>> data type.
I have never worked with this format so I can't even print schema. sorry
printshema:
root
|-- _id: string (nullable = true)
|-- Locale: string (nullable = true)
|-- workExperience: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- company: string (nullable = true)
| | |-- dateRange: string (nullable = true)
| | |-- description: string (nullable = true)
| | |-- location: string (nullable = true)
| | |-- title: string (nullable = true)
The parquet schema can be flattened using explode:
df=spark.read.parquet(...)
flattened_df = df.withColumn("tmp", F.explode("workExperience")) \
.selectExpr("_id", "Locale", "tmp.*")
flattened_df.write.csv(...)
You can't save a dataframe which contains column with array/struct type to CSV. You need to cast the column to string before writing.
df.withColumn('workExperience', col('workExperience').cast('string')).write.csv('path')

PySpark Add new object in nested field if not exist

Schema
root
|-- userId: string (nullable = true)
|-- languageknowList: array (nullable = true)
| |-- element: struct (containsNull = false)
| | |-- code: string (nullable = false)
| | |-- description: string (nullable = false)
| | |-- name: string (nullable = false)
The df has userId and languageknownList. Every user should know English, so English language is not present in languageknowList I have to add.
English
code: 10
description: English Language
name: English
Any one please help me.
You can create a new array of structs column and concat to the existing column:
import pyspark.sql.functions as F
english = F.struct(F.lit('10').alias('code'),
F.lit('English Language').alias('description'),
F.lit('English').alias('name')
)
df2 = df.withColumn(
'languageknowList',
F.when(
~F.array_contains(F.col('languageknowList'), english),
F.concat(
F.col('languageknowList'),
F.array(english)
)
).otherwise(
F.col('languageknowList')
)
)

Way to concatenate Array of structs

I have a column that contains array of structs. It looks like this:
|-- Network: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- Code: string (nullable = true)
| | |-- Signal: string (nullable = true)
This is just a small sample, there are many more columns inside the struct than this. Is there a way to take the arrays in the column for each row, concatenate them and make them into one string? For example, we could have something like this:
[["example", 2], ["example2", 3]]
Is there a way to make into:
"example2example3"?
Assuming having a dataframe df with the following schema:
df.printSchema
df with sample data:
df.show(false)
You need to first explode the Network array to select the struct elements Code and signal.
var myDf = df.select(explode($"Network").as("Network"))
Then you need to concat the two columns using the concat() function and then pass the output to the collect_list() function which will aggregate all rows into one row of type array<string>
myDf = myDf.select(collect_list(concat($"Network.code",$"Network.signal")).as("data"))
Finally, you need to concat into the required format which can be done using concat_ws() function which takes two arguments, the first being the separator to be placed between two string and the second argument being a column with array<string> type which is our output from our previous step. As per your use case, we don't need any separator to be placed between two concatenates strings hence we keep the separator argument as an empty quote.
myDf = myDf.select(concat_ws("",$"data").as("data"))
All the above steps can be done in one line
myDf= myDf.select(explode($"Network").as("Network")).select(concat_ws("",collect_list(concat($"Network.code",$"Network.signal"))).as("data")).show(false)
If you want the output directly into a String variable then use:
val myStr = myDf.first.get(0).toString
print(myStr)
There is a library called spark-hats (Github, small article) that you might find very useful in these situations.
With its use, you can map the array easily and output the concatenation next to the elements or even somewhere else if you provide a fully qualified name.
Setup
import org.apache.spark.sql.functions._
import za.co.absa.spark.hats.Extensions._
scala> df.printSchema
root
|-- info: struct (nullable = true)
| |-- drivers: struct (nullable = true)
| | |-- carName: string (nullable = true)
| | |-- carNumbers: string (nullable = true)
| | |-- driver: string (nullable = true)
|-- teamName: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- team1: string (nullable = true)
| | |-- team2: string (nullable = true)
scala> df.show(false)
+---------------------------+------------------------------+
|info |teamName |
+---------------------------+------------------------------+
|[[RB7, 33, Max Verstappen]]|[[Redbull, rb], [Monster, mt]]|
+---------------------------+------------------------------+
Command you are looking for
scala> val dfOut = df.nestedMapColumn(inputColumnName = "teamName", outputColumnName = "nextElementInArray", expression = a => concat(a.getField("team1"), a.getField("team2")) )
dfOut: org.apache.spark.sql.DataFrame = [info: struct<drivers: struct<carName: string, carNumbers: string ... 1 more field>>, teamName: array<struct<team1:string,team2:string,nextElementInArray:string>>]
Output
scala> dfOut.printSchema
root
|-- info: struct (nullable = true)
| |-- drivers: struct (nullable = true)
| | |-- carName: string (nullable = true)
| | |-- carNumbers: string (nullable = true)
| | |-- driver: string (nullable = true)
|-- teamName: array (nullable = true)
| |-- element: struct (containsNull = false)
| | |-- team1: string (nullable = true)
| | |-- team2: string (nullable = true)
| | |-- nextElementInArray: string (nullable = true)
scala> dfOut.show(false)
+---------------------------+----------------------------------------------------+
|info |teamName |
+---------------------------+----------------------------------------------------+
|[[RB7, 33, Max Verstappen]]|[[Redbull, rb, Redbullrb], [Monster, mt, Monstermt]]|
+---------------------------+----------------------------------------------------+

how to check if an array has colums in a schema?

I have a schema and I would like to check the array if it has columns inside before exploding it. my schema looks like this
|-- CaseNumber: string (nullable = true)
|-- Interactions: struct (nullable = true)
| |-- EmailInteractions: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- CreatedBy: string (nullable = true)
| | | |-- CreatedOn: string (nullable = true)
| | | |-- Direction: string (nullable = true)
| |-- PhoneInteractions: array (nullable = true)
| | |-- element: string (containsNull = true)
| |-- WebInteractions: array (nullable = true)
| | |-- element: string (containsNull = true)
|-- EntityAction: string (nullable = true)
I would like to check if "EmailInteractions" has elements under it before I run the job that will explode it,
I have edited the question for clarity
1. check if email interactions array exist and check if it has columns, if both true, explode the array and finish, if one of the conditions is false, pass to step 2
2.check if phone interactions array exist and check if it has columns, if both true, explode the array and finish, if one of the conditions is false, pass to step 3
3.check if web interactions exist and check if it has columns, if both true, explode the array and finish, if one of the conditions is false, finish
I am new to coding and data bricks, please help on this.
This is one way of doing it. Unfortunately information under StructField is not searchable easily, I converted it to a string and search the filed by field name or keyword "StructField" to know that it contains a field.
val jsonWithNull = """{"a": 123,"b":null,"EmailInteractions":[{"CreatedBy":"test"}]}"""
val jsonWithoutNull = """{"a": 123,"b":3,"EmailInteractions":[{"CreatedBy":"test1"}]}"""
import spark.implicits._
val df = spark.read.json(Seq(jsonWithNull,jsonWithoutNull).toDS)
df.printSchema()
val field = df.schema.filter { f =>
if (f.dataType.typeName == "array" && f.toString().contains("CreatedBy")){
true
}
else{
false
}
}
println(field)
// check for field value being not null then explode
Result List(StructField(EmailInteractions,ArrayType(StructType(StructField(CreatedBy,StringType,true)),true),true))

Spark LuceneRDD for JSON data

Can we use LuceneRDD to Index JSON data.I tried to Index JSON format data using LuceneRDD, but it doesn't show correct result
Code:
read.filter($"influencer" === "markpantoni").show(truncate = false)
val luceneRDD = LuceneRDD(read)
val influencerName = "markpantoni"
val result= luceneRDD.termQuery("influencer", "markpantoni",1)
result.take(1).foreach(println)
read dataframe scheme:
root
|-- influencer: string (nullable = true)
|-- matches: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- influencer: string (nullable = true)
| | |-- totalNumberOfOverlaps: string (nullable = true)
Result:
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
|influencer |matches |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
|markpantoni|[[chefsymon,4], [TheSchott,3], [RyanJohansen19,2], [builtincbus,1], [AAAOhio,1], [RMHCofCentralOH,1], [NASA,1], [CityScene,1], [daytonpulse,1], [wexarts,1]]|
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
[score: 5.685845/docId: 1/doc: Text fields:influencer:[markpantoni]]
[score: 5.685845/docId: 1/doc: Text fields:influencer:[markpantoni]]

Resources