Scala - DataFrame

基本概念

What's DataFrame

A DataFrame is equivalent to a relational table in Spark SQL [1]。

DataFrame的前身是SchemaRDD,從Spark 1.3.0開始SchemaRDD更名為DataFrame [2]。其實(shí)從使用上來看,跟RDD的區(qū)別主要是有了Schema,這樣就能根據(jù)不同行和列得到對(duì)應(yīng)的值。

Why DataFrame, Motivition

比RDD有更多的操作,而且執(zhí)行計(jì)劃上也比RDD有更多的優(yōu)化。能夠方便處理大規(guī)模結(jié)構(gòu)化數(shù)據(jù)。

How to use DataFrame

創(chuàng)建DataFrame

  • 創(chuàng)建一個(gè)空的DataFrame
    這里schema是一個(gè)StructType類型的
sqlContext.createDataFrame(sc.emptyRDD[Row], schema)
  • 從一個(gè)List創(chuàng)建
def listToDataFrame(list: ListBuffer[List[Any]], schema:StructType): DataFrame = {
    val rows = list.map{x => Row(x:_*)}
    val rdd = sqlContext.sparkContext.parallelize(rows)
    
    sqlContext.createDataFrame(rdd, schema)
}
  • 直接通過RDD生成
val departments = sc.parallelize(Array(
  (31, "Sales"), 
  (33, "Engineering"), 
  (34, "Clerical"),
  (35, "Marketing")
)).toDF("DepartmentID", "DepartmentName")

val employees = sc.parallelize(Array[(String, Option[Int])](
  ("Rafferty", Some(31)), ("Jones", Some(33)), ("Heisenberg", Some(33)), ("Robinson", Some(34)), ("Smith", Some(34)), 
  ("Williams", null)
)).toDF("LastName", "DepartmentID")
  • 讀取json文件創(chuàng)建[5]

json文件

{"name":"Michael"}
{"name":"Andy", "age":30}
{"name":"Justin", "age":19}

創(chuàng)建DataFrame

val df = sqlContext.jsonFile("/path/to/your/jsonfile")
df: org.apache.spark.sql.DataFrame = [age: bigint, name: string]
  • 從parquet文件讀出創(chuàng)建
val df:DataFrame = sqlContext.read.parquet("/Users/robin/workspace/cooked_data/bt")
  • 從MySQL讀取表chuang創(chuàng)建[5]
val jdbcDF = sqlContext.load("jdbc", Map("url" -> "jdbc:mysql://localhost:3306/db?user=aaa&password=111", "dbtable" -> "your_table"))
  • 從Hive創(chuàng)建[5]

Spark提供了一個(gè)HiveContext的上下文,其實(shí)是SQLContext的一個(gè)子類,但從作用上來說,sqlContext也支持Hive數(shù)據(jù)源。只要在部署Spark的時(shí)候加入Hive選項(xiàng),并把已有的hive-site.xml文件挪到$SPARK_HOME/conf路徑下,就可以直接用Spark查詢包含已有元數(shù)據(jù)的Hive表了

sqlContext.sql("select count(*) from hive_people")
  • 從CSV文件創(chuàng)建

有個(gè)spark-csv的library

可以從maven引入,也可以k從spark-shell $SPARK_HOME/bin/spark-shell --packages com.databricks:spark-csv_2.11:1.5.0

val df = sqlContext.read.format("com.databricks.spark.csv").
     option("header", "true").
     option("inferSchema","true").
     load("/Users/username/tmp/person.csv")

DataFrame基本操作

官方例子

// To create DataFrame using SQLContext
val people = sqlContext.read.parquet("...")
val department = sqlContext.read.parquet("...")

people.filter("age > 30")
 .join(department, people("deptId") === department("id"))
 .groupBy(department("name"), "gender")
 .agg(avg(people("salary")), max(people("age")))

Filter

  • 把id為null的行都filter掉
df.withColumn("id", when(expr("id is null"), 0).otherwise(1)).show

Join連接

  • inner join [4]
val employees = sc.parallelize(Array[(String, Option[Int])](
  ("Rafferty", Some(31)), ("Jones", Some(33)), ("Heisenberg", Some(33)), ("Robinson", Some(34)), ("Smith", Some(34)), ("Williams", null)
)).toDF("LastName", "DepartmentID")



val departments = sc.parallelize(Array(
  (31, "Sales"), (33, "Engineering"), (34, "Clerical"),
  (35, "Marketing")
)).toDF("DepartmentID", "DepartmentName")

departments.show()

+------------+--------------+
|DepartmentID|DepartmentName|
+------------+--------------+
|          31|         Sales|
|          33|   Engineering|
|          34|      Clerical|
|          35|     Marketing|
+------------+--------------+

employees.join(departments, "DepartmentID").show()
+------------+----------+--------------+
|DepartmentID|  LastName|DepartmentName|
+------------+----------+--------------+
|          31|  Rafferty|         Sales|
|          33|     Jones|   Engineering|
|          33|Heisenberg|   Engineering|
|          34|  Robinson|      Clerical|
|          34|     Smith|      Clerical|
|        null|  Williams|          null|
+------------+----------+--------------+
  • left outer join [4]
employees.join(departments, Seq("DepartmentID"), "left_outer").show()
+------------+----------+--------------+
|DepartmentID|  LastName|DepartmentName|
+------------+----------+--------------+
|          31|  Rafferty|         Sales|
|          33|     Jones|   Engineering|
|          33|Heisenberg|   Engineering|
|          34|  Robinson|      Clerical|
|          34|     Smith|      Clerical|
|        null|  Williams|          null|
+------------+----------+--------------+
val d1 = df.groupBy("startDate","endDate").agg(max("price") as "price").show
  • Join expression 用表達(dá)式連接 [3]
val products = sc.parallelize(Array(
  ("steak", "1990-01-01", "2000-01-01", 150),
  ("steak", "2000-01-02", "2020-01-01", 180),
  ("fish", "1990-01-01", "2020-01-01", 100)
)).toDF("name", "startDate", "endDate", "price")

products.show()

+-----+----------+----------+-----+
| name| startDate|   endDate|price|
+-----+----------+----------+-----+
|steak|1990-01-01|2000-01-01|  150|
|steak|2000-01-02|2020-01-01|  180|
| fish|1990-01-01|2020-01-01|  100|
+-----+----------+----------+-----+

val orders = sc.parallelize(Array(
  ("1995-01-01", "steak"),
  ("2000-01-01", "fish"),
  ("2005-01-01", "steak")
)).toDF("date", "product")

orders.show()

+----------+-------+
|      date|product|
+----------+-------+
|1995-01-01|  steak|
|2000-01-01|   fish|
|2005-01-01|  steak|
+----------+-------+

orders.join(products, $"product" === $"name" && $"date" >= $"startDate" && $"date" <= $"endDate") .show()
+----------+-------+-----+----------+----------+-----+
|      date|product| name| startDate|   endDate|price|
+----------+-------+-----+----------+----------+-----+
|2000-01-01|   fish| fish|1990-01-01|2020-01-01|  100|
|1995-01-01|  steak|steak|1990-01-01|2000-01-01|  150|
|2005-01-01|  steak|steak|2000-01-02|2020-01-01|  180|
+----------+-------+-----+----------+----------+-----+
  • Join types: inner, outer, left_outer, right_outer, leftsemi
  • Join with dataframe alias
val joinedDF = testDF.as('a).join(genmodDF.as('b), $"a.PassengerId" === $"b.PassengerId")

joinedDF.select($"a.PassengerId", $"b.PassengerId").take(10)

val joinedDF = testDF.join(genmodDF, testDF("PassengerId") === genmodDF("PassengerId"), "inner")

Reference

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容