PySpark and broadcast join example

Spark 1.3 doesn’t support broadcast joins using DataFrame. In Spark >= 1.5.0 you can use broadcast function to apply broadcast joins: from pyspark.sql.functions import broadcast data1.join(broadcast(data2), data1.id == data2.id) For older versions the only option is to convert to RDD and apply the same logic as in other languages. Roughly something like this: from pyspark.sql … Read more

Applying a Window function to calculate differences in pySpark

You can bring the previous day column by using lag function, and add additional column that does actual day-to-day return from the two columns, but you may have to tell spark how to partition your data and/or order it to do lag, something like this: from pyspark.sql.window import Window import pyspark.sql.functions as func from pyspark.sql.functions … Read more

Explode in PySpark

explode and split are SQL functions. Both operate on SQL Column. split takes a Java regular expression as a second argument. If you want to separate data on arbitrary whitespace you’ll need something like this: df = sqlContext.createDataFrame( [(‘cat \n\n elephant rat \n rat cat’, )], [‘word’] ) df.select(explode(split(col(“word”), “\s+”)).alias(“word”)).show() ## +——–+ ## | word| … Read more

Spark 2.0 missing spark implicits

There is no package called spark.implicits. With spark here it refers to SparkSession. If you are inside the REPL the session is already defined as spark so you can just type: import spark.implicits._ If you have defined your own SparkSession somewhere in your code, then adjust it accordingly: val mySpark = SparkSession .builder() .appName(“Spark SQL … Read more

Perform a typed join in Scala with Spark Datasets

Observation Spark SQL can optimize join only if join condition is based on the equality operator. This means we can consider equijoins and non-equijoins separately. Equijoin Equijoin can be implemented in a type safe manner by mapping both Datasets to (key, value) tuples, performing join based on keys, and reshaping the result: import org.apache.spark.sql.Encoder import … Read more

Why is Apache-Spark – Python so slow locally as compared to pandas?

Because: Apache Spark is a complex framework designed to distribute processing across hundreds of nodes, while ensuring correctness and fault tolerance. Each of these properties has significant cost. Because purely in-memory in-core processing (Pandas) is orders of magnitude faster than disk and network (even local) I/O (Spark). Because parallelism (and distributed processing) add significant overhead, … Read more

Read multiline JSON in Apache Spark

Spark >= 2.2 Spark 2.2 introduced wholeFile multiLine option which can be used to load JSON (not JSONL) files: spark.read .option(“multiLine”, true).option(“mode”, “PERMISSIVE”) .json(“/path/to/user.json”) See: SPARK-18352 – Parse normal, multi-line JSON files (not just JSON Lines). SPARK-20980 – Rename the option wholeFile to multiLine for JSON and CSV. Spark < 2.2 Well, using JSONL formated … Read more