Spark DataSet - Data Frame

1 - About

The data frame is a dataset of rows (ie organized into named columns).

Technically, a data frame is an untyped view of a dataset.

A SparkDataFrame is a distributed collection of data organized into named columns.

It is conceptually equivalent to:

3 - Management / Operations

Operations available on Datasets follow the spark pattern.

3.1 - Creating

A dataframe a unified interface to reading/writing data in a variety of formats with Writer to JDBC, JSON, CSV, …

sources such as:

  • structured data files,
  • tables in Hive,
  • external databases,
  • or existing RDDs.
// a DataFrame is represented by a Dataset of Rows. 
// a type alias of Dataset[Row]
Dataset[Row]
// a DataFrame is represented by a Dataset of Rows. 
// represent a DataFrame in java
Dataset<Row>
 
// From a sqlContext: \
sqlContext.createDataFrame(RDD[Rows], Schema)
  • Python DataFrame. All Datasets in Python are Dataset[Row], and we call it DataFrame to be consistent with the data frame concept in Pandas and R
people = spark.read.parquet("...")
textFile = spark.read.text("README.md")

3.2 - Reading

df = sqlContext.read   
  .format("json")   
  .option("samplingRatio", "0.1")   
  .load("/home/michael/data.json") 

3.3 - Writing

df.write   
  .format("parquet")   
  .mode("append")   
  .partitionBy("year")   
  .saveAsTable("fasterData")
 

where:

3.4 - Etl (Read and Write)

ETL Using Custom Data Sources

sqlContext.read 
  .format("com.databricks.spark.jira") 
  .option("url", "https://issues.apache.org/jira/rest/api/latest/search") 
  .option("user", "marmbrus") 
  .option("password", "*******") 
  .option("query", """ 
    |project = SPARK AND  
    |component = SQL AND  
    |(status = Open OR status = "In Progress" OR status = Reopened)""".stripMargin) 
  .load() 
  .repartition(1) 
  .write 
  .format("parquet") 
  .saveAsTable("sparkSqlJira") 

where:

  • the load function creates a data frame
  • that is then saved

3.5 - Operations

It has various domain-specific-language (DSL) functions defined in: DataFrame (this class), Column, and functions such as:

  • group by,
  • order,
  • plus,….

Example:

people.col("age").plus(10);  // in Java

4 - Documentation / Reference

db/spark/dataset/dataframe.txt · Last modified: 2018/07/10 16:06 by gerardnico