Skip to content
Snippets Groups Projects
sql-programming-guide.md 56.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • displayTitle: Spark SQL and DataFrame Guide
    title: Spark SQL and DataFrames
    
    ---
    
    * This will become a table of contents (this text will be scraped).
    {:toc}
    
    # Overview
    
    Spark SQL is a Spark module for structured data processing. It provides a programming abstraction called DataFrames and can also act as a distributed query engine.
    
    A DataFrame is a distributed collection of data organized into named columns. It is conceptually equivalent to a table in a relational database or a data frame in R/Python, but with richer optimizations under the hood. DataFrames can be constructed from a wide array of sources such as: structured data files, tables in Hive, external databases, or existing RDDs.
    
    The DataFrame API is available in [Scala](api/scala/index.html#org.apache.spark.sql.DataFrame), [Java](api/java/index.html?org/apache/spark/sql/DataFrame.html), and [Python](api/python/pyspark.sql.html#pyspark.sql.DataFrame).
    
    All of the examples on this page use sample data included in the Spark distribution and can be run in the `spark-shell` or the `pyspark` shell.
    
    <div class="codetabs">
    <div data-lang="scala"  markdown="1">
    
    
    The entry point into all functionality in Spark SQL is the
    
    [SQLContext](api/scala/index.html#org.apache.spark.sql.SQLContext) class, or one of its
    
    descendants.  To create a basic SQLContext, all you need is a SparkContext.
    
    
    {% highlight scala %}
    val sc: SparkContext // An existing SparkContext.
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    
    // this is used to implicitly convert an RDD to a DataFrame.
    import sqlContext.implicits._
    
    <div data-lang="java" markdown="1">
    
    The entry point into all functionality in Spark SQL is the
    [SQLContext](api/java/index.html#org.apache.spark.sql.SQLContext) class, or one of its
    descendants.  To create a basic SQLContext, all you need is a SparkContext.
    
    
    {% highlight java %}
    
    JavaSparkContext sc = ...; // An existing JavaSparkContext.
    
    SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
    
    <div data-lang="python"  markdown="1">
    
    The entry point into all relational functionality in Spark is the
    
    [SQLContext](api/python/pyspark.sql.SQLContext-class.html) class, or one
    
    of its decedents.  To create a basic SQLContext, all you need is a SparkContext.
    
    {% highlight python %}
    from pyspark.sql import SQLContext
    
    {% endhighlight %}
    
    
    </div>
    </div>
    
    In addition to the basic SQLContext, you can also create a HiveContext, which provides a
    superset of the functionality provided by the basic SQLContext. Additional features include
    the ability to write queries using the more complete HiveQL parser, access to Hive UDFs, and the
    
    ability to read data from Hive tables.  To use a HiveContext, you do not need to have an
    
    existing Hive setup, and all of the data sources available to a SQLContext are still available.
    
    HiveContext is only packaged separately to avoid including all of Hive's dependencies in the default
    Spark build.  If these dependencies are not a problem for your application then using HiveContext
    
    is recommended for the 1.3 release of Spark.  Future releases will focus on bringing SQLContext up
    to feature parity with a HiveContext.
    
    The specific variant of SQL that is used to parse queries can also be selected using the
    
    `spark.sql.dialect` option.  This parameter can be changed using either the `setConf` method on
    a SQLContext or by using a `SET key=value` command in SQL.  For a SQLContext, the only dialect
    available is "sql" which uses a simple SQL parser provided by Spark SQL.  In a HiveContext, the
    default is "hiveql", though "sql" is also available.  Since the HiveQL parser is much more complete,
    
    this is recommended for most use cases.
    
    ## Creating DataFrames
    
    With a `SQLContext`, applications can create `DataFrame`s from an <a href='#interoperating-with-rdds'>existing `RDD`</a>, from a Hive table, or from <a href='#data-sources'>data sources</a>.
    
    As an example, the following creates a `DataFrame` based on the content of a JSON file:
    
    <div class="codetabs">
    <div data-lang="scala"  markdown="1">
    {% highlight scala %}
    val sc: SparkContext // An existing SparkContext.
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    val df = sqlContext.jsonFile("examples/src/main/resources/people.json")
    
    // Displays the content of the DataFrame to stdout
    df.show() 
    {% endhighlight %}
    
    </div>
    
    <div data-lang="java" markdown="1">
    {% highlight java %}
    JavaSparkContext sc = ...; // An existing JavaSparkContext.
    SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
    
    DataFrame df = sqlContext.jsonFile("examples/src/main/resources/people.json");
    
    // Displays the content of the DataFrame to stdout
    df.show();
    {% endhighlight %}
    
    </div>
    
    <div data-lang="python"  markdown="1">
    {% highlight python %}
    from pyspark.sql import SQLContext
    sqlContext = SQLContext(sc)
    
    df = sqlContext.jsonFile("examples/src/main/resources/people.json")
    
    # Displays the content of the DataFrame to stdout
    df.show()
    {% endhighlight %}
    
    </div>
    </div>
    
    
    ## DataFrame Operations
    
    DataFrames provide a domain-specific language for structured data manipulation in [Scala](api/scala/index.html#org.apache.spark.sql.DataFrame), [Java](api/java/index.html?org/apache/spark/sql/DataFrame.html), and [Python](api/python/pyspark.sql.html#pyspark.sql.DataFrame).
    
    Here we include some basic examples of structured data processing using DataFrames:
    
    
    <div class="codetabs">
    <div data-lang="scala"  markdown="1">
    {% highlight scala %}
    val sc: SparkContext // An existing SparkContext.
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    // Create the DataFrame
    val df = sqlContext.jsonFile("examples/src/main/resources/people.json")
    
    // Show the content of the DataFrame
    df.show()
    // age  name   
    // null Michael
    // 30   Andy   
    // 19   Justin 
    
    // Print the schema in a tree format
    df.printSchema()
    // root
    // |-- age: long (nullable = true)
    // |-- name: string (nullable = true)
    
    // Select only the "name" column
    df.select("name").show()
    // name   
    // Michael
    // Andy   
    // Justin 
    
    // Select everybody, but increment the age by 1
    df.select("name", df("age") + 1).show()
    // name    (age + 1)
    // Michael null     
    // Andy    31       
    // Justin  20       
    
    // Select people older than 21
    df.filter(df("name") > 21).show()
    // age name
    // 30  Andy
    
    // Count people by age
    df.groupBy("age").count().show()
    // age  count
    // null 1
    // 19   1
    // 30   1
    {% endhighlight %}
    
    </div>
    
    <div data-lang="java" markdown="1">
    {% highlight java %}
    val sc: JavaSparkContext // An existing SparkContext.
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    // Create the DataFrame
    DataFrame df = sqlContext.jsonFile("examples/src/main/resources/people.json");
    
    // Show the content of the DataFrame
    df.show();
    // age  name   
    // null Michael
    // 30   Andy   
    // 19   Justin 
    
    // Print the schema in a tree format
    df.printSchema();
    // root
    // |-- age: long (nullable = true)
    // |-- name: string (nullable = true)
    
    // Select only the "name" column
    df.select("name").show();
    // name   
    // Michael
    // Andy   
    // Justin 
    
    // Select everybody, but increment the age by 1
    df.select("name", df.col("age").plus(1)).show();
    // name    (age + 1)
    // Michael null     
    // Andy    31       
    // Justin  20       
    
    // Select people older than 21
    df.filter(df("name") > 21).show();
    // age name
    // 30  Andy
    
    // Count people by age
    df.groupBy("age").count().show();
    // age  count
    // null 1
    // 19   1
    // 30   1
    {% endhighlight %}
    
    </div>
    
    <div data-lang="python"  markdown="1">
    {% highlight python %}
    from pyspark.sql import SQLContext
    sqlContext = SQLContext(sc)
    
    # Create the DataFrame
    df = sqlContext.jsonFile("examples/src/main/resources/people.json")
    
    # Show the content of the DataFrame
    df.show()
    ## age  name   
    ## null Michael
    ## 30   Andy   
    ## 19   Justin 
    
    # Print the schema in a tree format
    df.printSchema()
    ## root
    ## |-- age: long (nullable = true)
    ## |-- name: string (nullable = true)
    
    # Select only the "name" column
    df.select("name").show()
    ## name   
    ## Michael
    ## Andy   
    ## Justin 
    
    # Select everybody, but increment the age by 1
    df.select("name", df.age + 1).show()
    ## name    (age + 1)
    ## Michael null     
    ## Andy    31       
    ## Justin  20       
    
    # Select people older than 21
    df.filter(df.name > 21).show()
    ## age name
    ## 30  Andy
    
    # Count people by age
    df.groupBy("age").count().show()
    ## age  count
    ## null 1
    ## 19   1
    ## 30   1
    
    {% endhighlight %}
    
    </div>
    </div>
    
    
    ## Running SQL Queries Programmatically
    
    The `sql` function on a `SQLContext` enables applications to run SQL queries programmatically and returns the result as a `DataFrame`.
    
    <div class="codetabs">
    <div data-lang="scala"  markdown="1">
    {% highlight scala %}
    val sqlContext = ...  // An existing SQLContext
    val df = sqlContext.sql("SELECT * FROM table")
    {% endhighlight %}
    </div>
    
    <div data-lang="java" markdown="1">
    {% highlight java %}
    val sqlContext = ...  // An existing SQLContext
    val df = sqlContext.sql("SELECT * FROM table")
    {% endhighlight %}
    </div>
    
    <div data-lang="python"  markdown="1">
    {% highlight python %}
    from pyspark.sql import SQLContext
    sqlContext = SQLContext(sc)
    df = sqlContext.sql("SELECT * FROM table")
    {% endhighlight %}
    </div>
    </div>
    
    
    Spark SQL supports two different methods for converting existing RDDs into DataFrames.  The first
    
    method uses reflection to infer the schema of an RDD that contains specific types of objects.  This
    
    reflection based approach leads to more concise code and works well when you already know the schema
    
    while writing your Spark application.
    
    The second method for creating DataFrames is through a programmatic interface that allows you to
    
    construct a schema and then apply it to an existing RDD.  While this method is more verbose, it allows
    
    you to construct DataFrames when the columns and their types are not known until runtime.
    
    ### Inferring the Schema Using Reflection
    
    <div class="codetabs">
    
    <div data-lang="scala"  markdown="1">
    
    
    The Scala interface for Spark SQL supports automatically converting an RDD containing case classes
    
    to a DataFrame.  The case class
    
    defines the schema of the table.  The names of the arguments to the case class are read using
    reflection and become the names of the columns. Case classes can also be nested or contain complex
    
    types such as Sequences or Arrays. This RDD can be implicitly converted to a DataFrame and then be
    
    registered as a table.  Tables can be used in subsequent SQL statements.
    
    // sc is an existing SparkContext.
    
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    // this is used to implicitly convert an RDD to a DataFrame.
    import sqlContext.implicits._
    
    
    // Define the schema using a case class.
    
    // Note: Case classes in Scala 2.10 can support only up to 22 fields. To work around this limit,
    
    // you can use custom classes that implement the Product interface.
    
    case class Person(name: String, age: Int)
    
    // Create an RDD of Person objects and register it as a table.
    val people = sc.textFile("examples/src/main/resources/people.txt").map(_.split(",")).map(p => Person(p(0), p(1).trim.toInt))
    
    people.registerTempTable("people")
    
    
    // SQL statements can be run by using the sql methods provided by sqlContext.
    
    val teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
    
    // The results of SQL queries are DataFrames and support all the normal RDD operations.
    
    // The columns of a row in the result can be accessed by ordinal.
    teenagers.map(t => "Name: " + t(0)).collect().foreach(println)
    {% endhighlight %}
    
    
    </div>
    
    <div data-lang="java"  markdown="1">
    
    
    Spark SQL supports automatically converting an RDD of [JavaBeans](http://stackoverflow.com/questions/3295496/what-is-a-javabean-exactly)
    
    into a DataFrame.  The BeanInfo, obtained using reflection, defines the schema of the table.
    
    Currently, Spark SQL does not support JavaBeans that contain
    
    nested or contain complex types such as Lists or Arrays.  You can create a JavaBean by creating a
    class that implements Serializable and has getters and setters for all of its fields.
    
    {% highlight java %}
    
    public static class Person implements Serializable {
      private String name;
      private int age;
    
    
      public String getName() {
    
      public void setName(String name) {
    
      public int getAge() {
    
      public void setAge(int age) {
    
    A schema can be applied to an existing RDD by calling `createDataFrame` and providing the Class object
    
    for the JavaBean.
    
    {% highlight java %}
    
    // sc is an existing JavaSparkContext.
    
    SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
    
    
    // Load a text file and convert each line to a JavaBean.
    
    JavaRDD<Person> people = sc.textFile("examples/src/main/resources/people.txt").map(
    
      new Function<String, Person>() {
        public Person call(String line) throws Exception {
          String[] parts = line.split(",");
    
          Person person = new Person();
          person.setName(parts[0]);
          person.setAge(Integer.parseInt(parts[1].trim()));
    
          return person;
        }
      });
    
    // Apply a schema to an RDD of JavaBeans and register it as a table.
    
    DataFrame schemaPeople = sqlContext.createDataFrame(people, Person.class);
    
    schemaPeople.registerTempTable("people");
    
    
    // SQL can be run over RDDs that have been registered as tables.
    
    DataFrame teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
    
    // The results of SQL queries are DataFrames and support all the normal RDD operations.
    
    // The columns of a row in the result can be accessed by ordinal.
    List<String> teenagerNames = teenagers.map(new Function<Row, String>() {
      public String call(Row row) {
        return "Name: " + row.getString(0);
      }
    }).collect();
    
    {% endhighlight %}
    
    </div>
    
    
    <div data-lang="python"  markdown="1">
    
    
    Spark SQL can convert an RDD of Row objects to a DataFrame, inferring the datatypes.  Rows are constructed by passing a list of
    
    key/value pairs as kwargs to the Row class. The keys of this list define the column names of the table,
    
    and the types are inferred by looking at the first row.  Since we currently only look at the first
    
    row, it is important that there is no missing data in the first row of the RDD. In future versions we
    
    plan to more completely infer the schema by looking at more data, similar to the inference that is
    performed on JSON files.
    
    
    {% highlight python %}
    
    # sc is an existing SparkContext.
    
    from pyspark.sql import SQLContext, Row
    
    # Load a text file and convert each line to a Row.
    
    lines = sc.textFile("examples/src/main/resources/people.txt")
    parts = lines.map(lambda l: l.split(","))
    
    people = parts.map(lambda p: Row(name=p[0], age=int(p[1])))
    
    # Infer the schema, and register the DataFrame as a table.
    
    schemaPeople = sqlContext.inferSchema(people)
    
    schemaPeople.registerTempTable("people")
    
    # SQL can be run over DataFrames that have been registered as a table.
    
    teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
    
    
    # The results of SQL queries are RDDs and support all the normal RDD operations.
    teenNames = teenagers.map(lambda p: "Name: " + p.name)
    
    for teenName in teenNames.collect():
      print teenName
    
    {% endhighlight %}
    
    </div>
    
    
    ### Programmatically Specifying the Schema
    
    <div class="codetabs">
    
    <div data-lang="scala"  markdown="1">
    
    
    When case classes cannot be defined ahead of time (for example,
    the structure of records is encoded in a string, or a text dataset will be parsed
    
    and fields will be projected differently for different users),
    
    a `DataFrame` can be created programmatically with three steps.
    
    
    1. Create an RDD of `Row`s from the original RDD;
    2. Create the schema represented by a `StructType` matching the structure of
    
    `Row`s in the RDD created in Step 1.
    
    3. Apply the schema to the RDD of `Row`s via `createDataFrame` method provided
    
    by `SQLContext`.
    
    For example:
    {% highlight scala %}
    // sc is an existing SparkContext.
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    // Create an RDD
    val people = sc.textFile("examples/src/main/resources/people.txt")
    
    // The schema is encoded in a string
    val schemaString = "name age"
    
    // Import Spark SQL data types and Row.
    import org.apache.spark.sql._
    
    // Generate the schema based on the string of schema
    val schema =
      StructType(
        schemaString.split(" ").map(fieldName => StructField(fieldName, StringType, true)))
    
    // Convert records of the RDD (people) to Rows.
    val rowRDD = people.map(_.split(",")).map(p => Row(p(0), p(1).trim))
    
    // Apply the schema to the RDD.
    
    val peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema)
    
    // Register the DataFrames as a table.
    peopleDataFrame.registerTempTable("people")
    
    
    // SQL statements can be run by using the sql methods provided by sqlContext.
    val results = sqlContext.sql("SELECT name FROM people")
    
    
    // The results of SQL queries are DataFrames and support all the normal RDD operations.
    
    // The columns of a row in the result can be accessed by ordinal.
    results.map(t => "Name: " + t(0)).collect().foreach(println)
    {% endhighlight %}
    
    
    </div>
    
    <div data-lang="java"  markdown="1">
    
    
    When JavaBean classes cannot be defined ahead of time (for example,
    the structure of records is encoded in a string, or a text dataset will be parsed and
    
    fields will be projected differently for different users),
    
    a `DataFrame` can be created programmatically with three steps.
    
    
    1. Create an RDD of `Row`s from the original RDD;
    2. Create the schema represented by a `StructType` matching the structure of
    
    `Row`s in the RDD created in Step 1.
    
    3. Apply the schema to the RDD of `Row`s via `createDataFrame` method provided
    
    by `SQLContext`.
    
    
    For example:
    {% highlight java %}
    // Import factory methods provided by DataType.
    
    import org.apache.spark.sql.types.DataType;
    
    // Import StructType and StructField
    
    import org.apache.spark.sql.types.StructType;
    import org.apache.spark.sql.types.StructField;
    
    // Import Row.
    
    import org.apache.spark.sql.Row;
    
    
    // sc is an existing JavaSparkContext.
    
    SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
    
    
    // Load a text file and convert each line to a JavaBean.
    JavaRDD<String> people = sc.textFile("examples/src/main/resources/people.txt");
    
    // The schema is encoded in a string
    String schemaString = "name age";
    
    // Generate the schema based on the string of schema
    List<StructField> fields = new ArrayList<StructField>();
    for (String fieldName: schemaString.split(" ")) {
      fields.add(DataType.createStructField(fieldName, DataType.StringType, true));
    }
    StructType schema = DataType.createStructType(fields);
    
    // Convert records of the RDD (people) to Rows.
    JavaRDD<Row> rowRDD = people.map(
      new Function<String, Row>() {
        public Row call(String record) throws Exception {
          String[] fields = record.split(",");
          return Row.create(fields[0], fields[1].trim());
        }
      });
    
    // Apply the schema to the RDD.
    
    DataFrame peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema);
    
    // Register the DataFrame as a table.
    peopleDataFrame.registerTempTable("people");
    
    
    // SQL can be run over RDDs that have been registered as tables.
    
    DataFrame results = sqlContext.sql("SELECT name FROM people");
    
    // The results of SQL queries are DataFrames and support all the normal RDD operations.
    
    // The columns of a row in the result can be accessed by ordinal.
    List<String> names = results.map(new Function<Row, String>() {
      public String call(Row row) {
        return "Name: " + row.getString(0);
      }
    }).collect();
    
    {% endhighlight %}
    
    </div>
    
    <div data-lang="python"  markdown="1">
    
    
    When a dictionary of kwargs cannot be defined ahead of time (for example,
    the structure of records is encoded in a string, or a text dataset will be parsed and
    fields will be projected differently for different users),
    
    a `DataFrame` can be created programmatically with three steps.
    
    
    1. Create an RDD of tuples or lists from the original RDD;
    2. Create the schema represented by a `StructType` matching the structure of
    tuples or lists in the RDD created in the step 1.
    
    3. Apply the schema to the RDD via `createDataFrame` method provided by `SQLContext`.
    
    
    For example:
    {% highlight python %}
    # Import SQLContext and data types
    from pyspark.sql import *
    
    # sc is an existing SparkContext.
    sqlContext = SQLContext(sc)
    
    # Load a text file and convert each line to a tuple.
    lines = sc.textFile("examples/src/main/resources/people.txt")
    parts = lines.map(lambda l: l.split(","))
    people = parts.map(lambda p: (p[0], p[1].strip()))
    
    # The schema is encoded in a string.
    schemaString = "name age"
    
    fields = [StructField(field_name, StringType(), True) for field_name in schemaString.split()]
    schema = StructType(fields)
    
    # Apply the schema to the RDD.
    
    schemaPeople = sqlContext.createDataFrame(people, schema)
    
    # Register the DataFrame as a table.
    
    schemaPeople.registerTempTable("people")
    
    
    # SQL can be run over DataFrames that have been registered as a table.
    
    results = sqlContext.sql("SELECT name FROM people")
    
    # The results of SQL queries are RDDs and support all the normal RDD operations.
    names = results.map(lambda p: "Name: " + p.name)
    for name in names.collect():
      print name
    {% endhighlight %}
    
    </div>
    
    </div>
    
    
    # Data Sources
    
    Spark SQL supports operating on a variety of data sources through the `DataFrame` interface.
    A DataFrame can be operated on as normal RDDs and can also be registered as a temporary table.
    Registering a DataFrame as a table allows you to run SQL queries over its data.  This section
    describes the various methods for loading data into a DataFrame.
    
    
    
    [Parquet](http://parquet.io) is a columnar format that is supported by many other data processing systems.
    Spark SQL provides support for both reading and writing Parquet files that automatically preserves the schema
    
    of the original data.
    
    
    ### Loading Data Programmatically
    
    Using the data from the above example:
    
    <div class="codetabs">
    
    <div data-lang="scala"  markdown="1">
    
    
    // sqlContext from the previous example is used in this example.
    
    // This is used to implicitly convert an RDD to a DataFrame.
    import sqlContext.implicits._
    
    val people: RDD[Person] = ... // An RDD of case class objects, from the previous example.
    
    // The RDD is implicitly converted to a DataFrame by implicits, allowing it to be stored using Parquet.
    
    people.saveAsParquetFile("people.parquet")
    
    // Read in the parquet file created above.  Parquet files are self-describing so the schema is preserved.
    
    // The result of loading a Parquet file is also a DataFrame.
    
    val parquetFile = sqlContext.parquetFile("people.parquet")
    
    //Parquet files can also be registered as tables and then used in SQL statements.
    
    parquetFile.registerTempTable("parquetFile")
    
    val teenagers = sqlContext.sql("SELECT name FROM parquetFile WHERE age >= 13 AND age <= 19")
    teenagers.map(t => "Name: " + t(0)).collect().foreach(println)
    
    </div>
    
    <div data-lang="java"  markdown="1">
    
    {% highlight java %}
    
    // sqlContext from the previous example is used in this example.
    
    DataFrame schemaPeople = ... // The DataFrame from the previous example.
    
    // DataFrames can be saved as Parquet files, maintaining the schema information.
    
    schemaPeople.saveAsParquetFile("people.parquet");
    
    
    // Read in the Parquet file created above.  Parquet files are self-describing so the schema is preserved.
    
    // The result of loading a parquet file is also a DataFrame.
    DataFrame parquetFile = sqlContext.parquetFile("people.parquet");
    
    
    //Parquet files can also be registered as tables and then used in SQL statements.
    
    parquetFile.registerTempTable("parquetFile");
    
    DataFrame teenagers = sqlContext.sql("SELECT name FROM parquetFile WHERE age >= 13 AND age <= 19");
    
    List<String> teenagerNames = teenagers.map(new Function<Row, String>() {
      public String call(Row row) {
        return "Name: " + row.getString(0);
      }
    }).collect();
    
    {% endhighlight %}
    
    </div>
    
    <div data-lang="python"  markdown="1">
    
    {% highlight python %}
    
    # sqlContext from the previous example is used in this example.
    
    schemaPeople # The DataFrame from the previous example.
    
    # DataFrames can be saved as Parquet files, maintaining the schema information.
    
    schemaPeople.saveAsParquetFile("people.parquet")
    
    # Read in the Parquet file created above.  Parquet files are self-describing so the schema is preserved.
    
    # The result of loading a parquet file is also a DataFrame.
    
    parquetFile = sqlContext.parquetFile("people.parquet")
    
    
    # Parquet files can also be registered as tables and then used in SQL statements.
    
    parquetFile.registerTempTable("parquetFile");
    
    teenagers = sqlContext.sql("SELECT name FROM parquetFile WHERE age >= 13 AND age <= 19")
    teenNames = teenagers.map(lambda p: "Name: " + p.name)
    for teenName in teenNames.collect():
      print teenName
    
    Configuration of Parquet can be done using the `setConf` method on SQLContext or by running
    
    `SET key=value` commands using SQL.
    
    <table class="table">
    <tr><th>Property Name</th><th>Default</th><th>Meaning</th></tr>
    <tr>
      <td><code>spark.sql.parquet.binaryAsString</code></td>
      <td>false</td>
      <td>
    
        Some other Parquet-producing systems, in particular Impala and older versions of Spark SQL, do
        not differentiate between binary data and strings when writing out the Parquet schema.  This
    
        flag tells Spark SQL to interpret binary data as a string to provide compatibility with these systems.
      </td>
    </tr>
    
    <tr>
      <td><code>spark.sql.parquet.int96AsTimestamp</code></td>
      <td>true</td>
      <td>
        Some Parquet-producing systems, in particular Impala, store Timestamp into INT96. Spark would also
        store Timestamp as INT96 because we need to avoid precision lost of the nanoseconds field. This
        flag tells Spark SQL to interpret INT96 data as a timestamp to provide compatibility with these systems.
      </td>
    </tr>
    
    <tr>
      <td><code>spark.sql.parquet.cacheMetadata</code></td>
    
      <td>true</td>
    
        Turns on caching of Parquet schema metadata.  Can speed up querying of static data.
    
      </td>
    </tr>
    <tr>
      <td><code>spark.sql.parquet.compression.codec</code></td>
    
      <td>gzip</td>
    
        Sets the compression codec use when writing Parquet files. Acceptable values include:
    
        uncompressed, snappy, gzip, lzo.
      </td>
    </tr>
    
    <tr>
      <td><code>spark.sql.parquet.filterPushdown</code></td>
      <td>false</td>
      <td>
        Turn on Parquet filter pushdown optimization. This feature is turned off by default because of a known
        bug in Paruet 1.6.0rc3 (<a href="https://issues.apache.org/jira/browse/PARQUET-136">PARQUET-136</a>).
        However, if your table doesn't contain any nullable string or binary columns, it's still safe to turn
        this feature on.
      </td>
    </tr>
    
    <tr>
      <td><code>spark.sql.hive.convertMetastoreParquet</code></td>
      <td>true</td>
      <td>
        When set to false, Spark SQL will use the Hive SerDe for parquet tables instead of the built in
        support.
      </td>
    </tr>
    
    ## JSON Datasets
    <div class="codetabs">
    
    <div data-lang="scala"  markdown="1">
    
    Spark SQL can automatically infer the schema of a JSON dataset and load it as a DataFrame.
    
    This conversion can be done using one of two methods in a SQLContext:
    
    * `jsonFile` - loads data from a directory of JSON files where each line of the files is a JSON object.
    
    * `jsonRDD` - loads data from an existing RDD where each element of the RDD is a string containing a JSON object.
    
    Note that the file that is offered as _jsonFile_ is not a typical JSON file. Each
    line must contain a separate, self-contained valid JSON object. As a consequence,
    a regular multi-line JSON file will most often fail.
    
    
    // sc is an existing SparkContext.
    
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    
    // A JSON dataset is pointed to by path.
    // The path can be either a single text file or a directory storing text files.
    val path = "examples/src/main/resources/people.json"
    
    // Create a DataFrame from the file(s) pointed to by path
    
    val people = sqlContext.jsonFile(path)
    
    // The inferred schema can be visualized using the printSchema() method.
    people.printSchema()
    // root
    
    //  |-- age: integer (nullable = true)
    //  |-- name: string (nullable = true)
    
    // Register this DataFrame as a table.
    
    people.registerTempTable("people")
    
    
    // SQL statements can be run by using the sql methods provided by sqlContext.
    val teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
    
    
    // Alternatively, a DataFrame can be created for a JSON dataset represented by
    
    // an RDD[String] storing one JSON object per string.
    val anotherPeopleRDD = sc.parallelize(
      """{"name":"Yin","address":{"city":"Columbus","state":"Ohio"}}""" :: Nil)
    val anotherPeople = sqlContext.jsonRDD(anotherPeopleRDD)
    
    <div data-lang="java"  markdown="1">
    
    Spark SQL can automatically infer the schema of a JSON dataset and load it as a DataFrame.
    This conversion can be done using one of two methods in a SQLContext :
    
    * `jsonFile` - loads data from a directory of JSON files where each line of the files is a JSON object.
    
    * `jsonRDD` - loads data from an existing RDD where each element of the RDD is a string containing a JSON object.
    
    Note that the file that is offered as _jsonFile_ is not a typical JSON file. Each
    line must contain a separate, self-contained valid JSON object. As a consequence,
    a regular multi-line JSON file will most often fail.
    
    
    {% highlight java %}
    // sc is an existing JavaSparkContext.
    
    SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
    
    
    // A JSON dataset is pointed to by path.
    // The path can be either a single text file or a directory storing text files.
    String path = "examples/src/main/resources/people.json";
    
    // Create a DataFrame from the file(s) pointed to by path
    DataFrame people = sqlContext.jsonFile(path);
    
    
    // The inferred schema can be visualized using the printSchema() method.
    people.printSchema();
    // root
    
    //  |-- age: integer (nullable = true)
    //  |-- name: string (nullable = true)
    
    // Register this DataFrame as a table.
    
    people.registerTempTable("people");
    
    
    // SQL statements can be run by using the sql methods provided by sqlContext.
    
    DataFrame teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19");
    
    // Alternatively, a DataFrame can be created for a JSON dataset represented by
    
    // an RDD[String] storing one JSON object per string.
    List<String> jsonData = Arrays.asList(
      "{\"name\":\"Yin\",\"address\":{\"city\":\"Columbus\",\"state\":\"Ohio\"}}");
    JavaRDD<String> anotherPeopleRDD = sc.parallelize(jsonData);
    
    DataFrame anotherPeople = sqlContext.jsonRDD(anotherPeopleRDD);
    
    {% endhighlight %}
    </div>
    
    <div data-lang="python"  markdown="1">
    
    Spark SQL can automatically infer the schema of a JSON dataset and load it as a DataFrame.
    
    This conversion can be done using one of two methods in a SQLContext:
    
    * `jsonFile` - loads data from a directory of JSON files where each line of the files is a JSON object.
    
    * `jsonRDD` - loads data from an existing RDD where each element of the RDD is a string containing a JSON object.
    
    Note that the file that is offered as _jsonFile_ is not a typical JSON file. Each
    line must contain a separate, self-contained valid JSON object. As a consequence,
    a regular multi-line JSON file will most often fail.
    
    
    {% highlight python %}
    # sc is an existing SparkContext.
    from pyspark.sql import SQLContext
    sqlContext = SQLContext(sc)
    
    # A JSON dataset is pointed to by path.
    # The path can be either a single text file or a directory storing text files.
    path = "examples/src/main/resources/people.json"
    
    # Create a DataFrame from the file(s) pointed to by path
    
    people = sqlContext.jsonFile(path)
    
    # The inferred schema can be visualized using the printSchema() method.
    people.printSchema()
    # root
    
    #  |-- age: integer (nullable = true)
    #  |-- name: string (nullable = true)
    
    # Register this DataFrame as a table.
    
    people.registerTempTable("people")
    
    
    # SQL statements can be run by using the sql methods provided by sqlContext.
    teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
    
    
    # Alternatively, a DataFrame can be created for a JSON dataset represented by
    
    # an RDD[String] storing one JSON object per string.
    anotherPeopleRDD = sc.parallelize([
      '{"name":"Yin","address":{"city":"Columbus","state":"Ohio"}}'])
    anotherPeople = sqlContext.jsonRDD(anotherPeopleRDD)
    {% endhighlight %}
    </div>
    
    </div>
    
    ## Hive Tables
    
    
    Spark SQL also supports reading and writing data stored in [Apache Hive](http://hive.apache.org/).
    However, since Hive has a large number of dependencies, it is not included in the default Spark assembly.
    
    Hive support is enabled by adding the `-Phive` and `-Phive-thriftserver` flags to Spark's build.
    
    This command builds a new assembly jar that includes Hive. Note that this Hive assembly jar must also be present
    
    on all of the worker nodes, as they will need access to the Hive serialization and deserialization libraries
    
    (SerDes) in order to access data stored in Hive.
    
    
    Configuration of Hive is done by placing your `hive-site.xml` file in `conf/`.
    
    
    <div class="codetabs">
    
    <div data-lang="scala"  markdown="1">
    
    
    When working with Hive one must construct a `HiveContext`, which inherits from `SQLContext`, and
    
    adds support for finding tables in the MetaStore and writing queries using HiveQL. Users who do
    
    not have an existing Hive deployment can still create a HiveContext.  When not configured by the
    hive-site.xml, the context automatically creates `metastore_db` and `warehouse` in the current
    directory.
    
    // sc is an existing SparkContext.
    
    val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)
    
    sqlContext.sql("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
    sqlContext.sql("LOAD DATA LOCAL INPATH 'examples/src/main/resources/kv1.txt' INTO TABLE src")
    
    sqlContext.sql("FROM src SELECT key, value").collect().foreach(println)
    
    {% endhighlight %}
    
    </div>
    
    <div data-lang="java"  markdown="1">
    
    
    When working with Hive one must construct a `HiveContext`, which inherits from `SQLContext`, and
    
    adds support for finding tables in the MetaStore and writing queries using HiveQL. In addition to
    
    the `sql` method a `HiveContext` also provides an `hql` methods, which allows queries to be
    
    expressed in HiveQL.
    
    {% highlight java %}
    
    // sc is an existing JavaSparkContext.
    
    HiveContext sqlContext = new org.apache.spark.sql.hive.HiveContext(sc);
    
    sqlContext.sql("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)");
    sqlContext.sql("LOAD DATA LOCAL INPATH 'examples/src/main/resources/kv1.txt' INTO TABLE src");
    
    
    // Queries are expressed in HiveQL.
    
    Row[] results = sqlContext.sql("FROM src SELECT key, value").collect();