Spark SQL
Spark SQLはSparkのモジュールであり、構造化データを処理しますが、非構造化データは処理できません。
特徴
- 統合が簡単(個別にインストールする必要はありません)
- 統一されたデータアクセス方式(構造化データタイプ:JDBC、Json、Hive、ParquerファイルはすべてSpark SQLデータソースとして使用できます)
- Hiveと完全に互換性があります(Hiveでデータを読み取り、Spark SQLで実行します)
- 標準データ接続をサポート
DataFrameを作成する
1.ケースクラスで作成
grade.txtファイル
06140411 Mr.Wu 102 110 106 318
06140407 Mr.Zhi 60 98 80 238
06140404 Mr.Zhang 98 31 63 192
06140403 Mr.Zhang 105 109 107 321
06140406 Mr.Xie 57 87 92 236
06140408 Mr.Guo 102 102 50 254
06140402 Mr.Li 54 61 64 179
06140401 Mr.Deng 83 76 111 270
06140409 Mr.Zhang 70 56 91 217
06140412 Mr.Yao 22 119 112 253
06140410 Mr.Su 45 65 80 190
06140405 Mr.Zheng 79 20 26 125
Scalaコード
# 定义schema
case class info(studentID: String,studentName: String,chinese: Int,math: Int,english: Int,totalGrade: Int)
val rdd = sc.textFile("/root/grade.txt").map(_.split("\t"))
val rdd1 = rdd.map(x => info(x(0),x(1),x(2).toInt,x(3).toInt,x(4).toInt,x(5).toInt))
val df = rdd1.toDF
df.show
その結果
+---------+-----------+-------+----+-------+----------+
|studentID|studentName|chinese|math|english|totalGrade|
+---------+-----------+-------+----+-------+----------+
| 06140411| Mr.Wu| 102| 110| 106| 318|
| 06140407| Mr.Zhi| 60| 98| 80| 238|
| 06140404| Mr.Zhang| 98| 31| 63| 192|
| 06140403| Mr.Zhang| 105| 109| 107| 321|
| 06140406| Mr.Xie| 57| 87| 92| 236|
| 06140408| Mr.Guo| 102| 102| 50| 254|
| 06140402| Mr.Li| 54| 61| 64| 179|
| 06140401| Mr.Deng| 83| 76| 111| 270|
| 06140409| Mr.Zhang| 70| 56| 91| 217|
| 06140412| Mr.Yao| 22| 119| 112| 253|
| 06140410| Mr.Su| 45| 65| 80| 190|
| 06140405| Mr.Zheng| 79| 20| 26| 125|
+---------+-----------+-------+----+-------+----------+
第二に、スパークセッションを介して作成します
grade.txtファイル
06140411 Mr.Wu 102 110 106 318
06140407 Mr.Zhi 60 98 80 238
06140404 Mr.Zhang 98 31 63 192
06140403 Mr.Zhang 105 109 107 321
06140406 Mr.Xie 57 87 92 236
06140408 Mr.Guo 102 102 50 254
06140402 Mr.Li 54 61 64 179
06140401 Mr.Deng 83 76 111 270
06140409 Mr.Zhang 70 56 91 217
06140412 Mr.Yao 22 119 112 253
06140410 Mr.Su 45 65 80 190
06140405 Mr.Zheng 79 20 26 125
Scalaコード
import org.apache.spark.sql.types._
import org.apache.spark.sql.Row
# 定义schema
val mySchema = StructType(List(StructField("studentID",DataTypes.StringType),StructField("studentName",DataTypes.StringType),StructField("chinese",DataTypes.IntegerType),StructField("math",DataTypes.IntegerType),StructField("english",DataTypes.IntegerType),StructField("totalGrade",DataTypes.IntegerType)))
val rdd = sc.textFile("/root/grade.txt").map(_.split("\t"))
val rdd1 = rdd.map(x => Row(x(0),x(1),x(2).toInt,x(3).toInt,x(4).toInt,x(5).toInt))
val df = spark.createDataFrame(rdd1,mySchema)
df.show
その結果
3.フォーマットされたファイルを読む
student.jsonファイル
{"studentID":"06140401", "studentName":"Mr.Deng"}
{"studentID":"06140402", "studentName":"Mr.Li"}
{"studentID":"06140403", "studentName":"Mr.Zhang"}
{"studentID":"06140404", "studentName":"Mr.Zhang"}
{"studentID":"06140405", "studentName":"Mr.Zheng"}
{"studentID":"06140406", "studentName":"Mr.Xie"}
{"studentID":"06140407", "studentName":"Mr.Zhi"}
{"studentID":"06140408", "studentName":"Mr.Guo"}
{"studentID":"06140409", "studentName":"Mr.Zhang"}
{"studentID":"06140410", "studentName":"Mr.Su"}
{"studentID":"06140411", "studentName":"Mr.Wu"}
{"studentID":"06140412", "studentName":"Mr.Yao"}
Scalaコード
val df = spark.read.json("/root/temp/student.json")
val df = spark.read.format("json").load("/root/temp/student.json")
df.show
その結果
DataFrameを操作する
DSLステートメント
df1.select($"studentName",$"chinese",$"math",$"english",$"totalGrade").show
df1.filter($"totalGrade">300).show
df1.groupBy($"roomID").count.show
SQLステートメント
グレード表
学生テーブル
ビューを作成
df1.createOrReplaceTempView("grade")
df2.createOrReplaceTempView("student")
spark.sql("select studentID,totalGrade from grade").show
spark.sql("select count(*) as studentNum from student").show
マルチテーブルクエリ(内部接続)
spark.sql("select studentName,totalGrade from grade,student where grade.studentID = student.studentID order by grade.studentID").show
Spark SQLビュー
createGlobalTempView、createOrReplaceGlobalTempView、createOrReplaceTempView、createTempView
(1)通常のビュー(ローカルビュー):現在のセッションでのみ有効(createOrReplaceTempView、createTempView)
(2)グローバルビュー:さまざまなセッションで役立ちます。原則:名前空間にグローバルビューを作成します:global_temp(ライブラリと同様)(createOrReplaceTempView、createTempView)
データセットを作成
まず、シーケンスを使用します
Scalaコードcase class Person(name: String,age: Int)
val rdd = Seq(Person("destiny",18),Person("freedom",20)).toDF
rdd.show
その結果
次に、JSONデータを使用します
case class Student(studentID: String,studentName: String)
val df = spark.read.format("json").load("/root/temp/student.json")
df.as[Student].show
その結果
3.他のフォーマットデータを使用する
val ds = spark.read.text("/root/temp/spark_workCount.txt").as[String]
val word = ds.flatMap(_.split(" ")).filter(_.length > 3)
word.show
その結果
val word = ds.flatMap(_.split(" ")).map((_,1)).groupByKey(_._1).count
その結果
オペレーションデータセット
ds.where($"totalGrade" >= 250).show
マルチテーブルクエリ
case class Grade(studentID: String,chinese: Int,math: Int,english: Int,totalGrade: Int)
case class Student(studentID: String,studentName: String)
val rdd = sc.textFile("/root/temp/gradeSheet.txt").map(_.split("\t"))
val ds1 = rdd.map(x => Grade(x(0),x(1).toInt,x(2).toInt,x(3).toInt,x(4).toInt)).toDS
val rdd = sc.textFile("/root/temp/studentSheet.txt").map(_.split("\t"))
val ds2 = rdd.map(x => Student(x(0),x(1))).toDS
ds1.join(ds2,"studentID").show
ds1.join(ds2,"studentID").where("totalGrade >= 250").show
データソースを使用
関数の読み込みと保存
Scalaコードval ds = spark.read.load("/root/temp/users.parquet")
# save结果的文件为parquet类型
ds.select($"name",$"favorite_color").write.save("/root/temp/parquet")
val ds1 = spark.read.load("/root/temp/parquet")
ds1.show
その結果
モード機能
df.write.mode("overwrite").save("/root/temp/parquet")
saveAsTable関数
df.select($"name").write.saveAsTable("table1")
spark.sql("select * from table1").show
オプション機能
スキーマのマージをサポート
val df = sc.makeRDD(1 to 6).map(x => (x,x*2)).toDF("singel","double")
df.write.mode("overwrite").save("/root/temp/table/key=1")
val df1 = sc.makeRDD(7 to 10).map(x => (x,x*3)).toDF("single","triple")
df1.write.mode("overwrite").save("/root/temp/table/key=2")
val df2 = spark.read.option("mergeSchema",true).parquet("/root/temp/table")
その結果