Spark SQL (一)開始入門(僅示範JAVA)

1. 起始點:SparkSession

Spark SQL中所有功能的入口點是Sparksession類。要創建一個Sparksession,僅使用 SparkSession.builder()就可以了:
Find full example code at “examples/src/main/java/org/apache/spark/examples/sql/JavaSparkSQLExample.java” in the Spark repo.

import org.apache.spark.sql.SparkSession;
SparkSession spark = SparkSession
  .builder()
  .appName("Java Spark SQL basic example")
  .master("local[*]")
  .config("spark.some.config.option", "some-value")
  .getOrCreate();

Spark2.0中的Sparksession爲Hive特性提供了內嵌的支持,包括使用HiveQL編寫查詢的能力,訪問Hive UDF,以及從Hive表中讀取數據的能力。爲了使用這些特性,你不需要去有一個已存在的Hive設置。

2. 創建DataFrames

在一個Sparksession中,應用程序可以從一個已經存在的RDD ,從hive表,或者從Spark數據源中創建一個DataFrames。
舉個例子,下面就是基於一個JSON文件創建一個DataFrame
Find full example code at “examples/src/main/java/org/apache/spark/examples/sql/JavaSparkSQLExample.java” in the Spark repo.

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

Dataset<Row> df = spark.read().json("examples/src/main/resources/people.json");

// DatasetFrame 標準輸出顯示的內容 *Displays the content of the DataFrame to stdout*
df.show();
// +----+-------+
// | age|   name|
// +----+-------+
// |null|Michael|
// |  30|   Andy|
// |  19| Justin|
// +----+-------+

3. 無類型的Dataset操作(aka DataFrame 操作)

DataFrames 提供了一個特定的語法用在 ScalaJavaPython and R中機構化數據的操作。
正如上面提到的一樣,Spark 2.0 中,DataFrames 在 Scala 和 Java API 中,僅僅是多個 Row 的 Dataset。這些操作也參考了與強類型的 Scala/Java Datasets 中的 “類型轉換” 對應的 “無類型轉換”
這裏包括一些使用 Dataset 進行結構化數據處理的示例 :
Find full example code at “examples/src/main/java/org/apache/spark/examples/sql/JavaSparkSQLExample.java” in the Spark repo.

// col("...") is preferable to df.col("...")
import static org.apache.spark.sql.functions.col;

// 以樹格式打印架構 *Print the schema in a tree format* 
df.printSchema();
// root
// |-- age: long (nullable = true)
// |-- name: string (nullable = true)

// 僅選擇“name”列  *Select only the "name" column*
df.select("name").show();
// +-------+
// |   name|
// +-------+
// |Michael|
// |   Andy|
// | Justin|
// +-------+

// 選擇所有人,但年齡增加1 *Select everybody, but increment the age by 1*
df.select(col("name"), col("age").plus(1)).show();
// +-------+---------+
// |   name|(age + 1)|
// +-------+---------+
// |Michael|     null|
// |   Andy|       31|
// | Justin|       20|
// +-------+---------+

// 選擇21歲以上的人 *Select people older than 21*
df.filter(col("age").gt(21)).show();
// +---+----+
// |age|name|
// +---+----+
// | 30|Andy|
// +---+----+

// 按年齡計算人數 *Count people by age*
df.groupBy("age").count().show();
// +----+-----+
// | age|count|
// +----+-----+
// |  19|    1|
// |null|    1|
// |  30|    1|
// +----+-----+

爲了能夠在 DataFrame 上被執行的操作類型的完整列表請參考 API 文檔

除了簡單的列引用和表達式之外,DataFrame 也有豐富的函數庫,包括 string 操作,date 算術,常見的 math 操作以及更多。可用的完整列表請參考 DataFrame 函數指南

4. 應用程序以編程的方式運行 SQL 查詢(Running SQL Queries Programmatically)

SparkSessionsql 函數可以讓應用程序以編程的方式運行 SQL 查詢,並將結果作爲一個 Dataset<Row> 返回。
Find full example code at “examples/src/main/java/org/apache/spark/examples/sql/JavaSparkSQLExample.java” in the Spark repo.

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

//將DataFrame 註冊爲SQL臨時視圖 *Register the DataFrame as a SQL temporary view*
df.createOrReplaceTempView("people");

Dataset<Row> sqlDF = spark.sql("SELECT * FROM people");
sqlDF.show();
// +----+-------+
// | age|   name|
// +----+-------+
// |null|Michael|
// |  30|   Andy|
// |  19| Justin|
// +----+-------+

5. 全局臨時視圖

6. 創建Datasets

7. RDD的互操作性

7.1 使用反射推斷Schema

7.2 以編程的方式指定Schema

8. 聚合(Aggregations)

8.1 非類型化用戶定義聚合函數(Untyped User-Defined Aggregate Functions)

8.2 類型安全的用戶定義聚合函數(Type-Safe User-Defined Aggregate Functions)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章