concept Delta Lake in category spark

appears as: Delta Lake
Spark in Action, Second Edition

This is an excerpt from Manning's book Spark in Action, Second Edition.

In this section, you’ll look at how Spark ingests Parquet. Spark will easily ingest Parquet files natively: there is no need for an additional library or configuration. It is important to remember that Parquet is also the default format used by Spark and Delta Lake (in chapter 17).

Delta Lake is a database at the heart of the Spark infrastructure. You will first learn what Delta Lake is, why it is needed, and in which contexts it should be used. You will then build your first application to feed Delta Lake. You will finish with two smaller applications that consume data stored in the database.

Delta Lake was initially known as Databricks Delta and was available in the Databricks cloud offering. At Spark Summit in May 2019, Databricks open sourced Delta under an Apache license: it became Delta Lake ( https://delta.io/ ). You will not have to download anything, as Delta Lake simply installs as a Maven dependency, as you will see in section 17.2.2.

Figure 17.7 shows the data pipeline you are going to build. Additionally, you will build two applications that will consume the data from Delta Lake to perform some analytics: calculating the number of meetings per department (an administrative subdivision similar to the United States’ counties ) and a basic analysis of the types of organizers.

Figure 17.7 The data loader application takes a JSON file containing the events, cleanses the data, and stores it in Delta Lake. The two analytics applications can now take the data from there.
Listing 17.8 Data loader
package net.jgp.books.spark.ch17.lab200_feed_delta;
 
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.expr;
import static org.apache.spark.sql.functions.when;
 
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
 
public class FeedDeltaLakeApp {
...
     private void start() {
      SparkSession spark = SparkSession.builder() #1
            .appName( "Ingestion the 'Grand Débat' files to Delta Lake" ) #1
            .master( "local[*]" ) #1
            .getOrCreate(); #1
 
      StructType schema = DataTypes.createStructType( new StructField[] { #2
            DataTypes.createStructField(
                 "authorId" ,
                DataTypes. StringType ,
                 false ),
...
            DataTypes.createStructField( #3
                 "createdAt" , #3
                DataTypes. TimestampType , #3
                 false ), #3
...
 
      Dataset<Row> df = spark .read().format( "json" ) #4
            .schema( schema )
            .option( "timestampFormat" , "yyyy-MM-dd HH:mm:ss" ) #5
            .load( "data/france_grand_debat/20190302 EVENTS.json" );
 
       df = df
            .withColumn( "authorZipCode" , #6
col( "authorZipCode" ).cast(DataTypes. IntegerType )) #6
            .withColumn( "authorZipCode" , #6
when(col( "authorZipCode" ).$less(1000), null ) #6
                      .otherwise(col( "authorZipCode" ))) #6
            .withColumn( "authorZipCode" , #6
                when(col( "authorZipCode" ).$greater$eq(99999), null ) #6
                      .otherwise(col( "authorZipCode" ))) #6
            .withColumn( "authorDept" , expr( "int(authorZipCode / 1000)" )); #8
       df .show(25); #9
       df .printSchema(); #9
 
       df .write().format( "delta" ) #10
            .mode( "overwrite" ) #11
            .save( "/tmp/delta_grand_debat_events" ); #12
 
      System. out .println( df .count() + " rows updated." ); #13
    }
 }

As you noticed, the format used for writing the data is delta , but when saved on disk, the data is written in the efficient Apache Parquet file format, which you worked with in chapter 7. The data is saved on the worker node. The next step is to consume the data from Delta Lake.