Demonstrates reading of a JSON file using Spark SQL. - Java Big Data

Java examples for Big Data:apache spark

Description

Demonstrates reading of a JSON file using Spark SQL.

Demo Code



import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;


public class SparkSQLDemo {


    public static void main(String[] args) {
        final SparkSession sparkSession = SparkSession.builder()
                .appName("Spark SQL Demo").master("local[5]").getOrCreate();

        final Dataset<Row> jsonDataFrame = sparkSession.read().json(
                "src/main/resources/data.json");
        jsonDataFrame.printSchema();//from w ww.  j  a  va2  s . c  om

        System.out.println("DSL API with Condition Expression:");
        jsonDataFrame.select("name").where("age = 30").show();
        System.out.println("Pure DSL API:");
        jsonDataFrame.select("name")
                .where(jsonDataFrame.col("age").equalTo(30)).show();

        System.out.println("SQL Query:");
        jsonDataFrame.createOrReplaceTempView("people");
        sparkSession.sql("SELECT name FROM people WHERE age = 30").show();
    }
}

Related Tutorials