SparkSQL-03

内容出处:http://spark.apache.org/docs/latest/sql-programming-guide.html

SparkSQL的三个愿景:

                                   1.Less Code

                                       a)可以自己推导schema(比如:直接读取json、Parquet,结构在数据文件中有)

                                       Partition Discovery可以自己推导schema

                                       

                                       Schema Merging(类似 ProtoBuffer Avro Thrift,灵活的添加删除列)

// This is used to implicitly convert an RDD to a DataFrame.
import spark.implicits._

// Create a simple DataFrame, store into a partition directory
val squaresDF = spark.sparkContext.makeRDD(1 to 5).map(i => (i, i * i)).toDF("value", "square")
squaresDF.write.parquet("data/test_table/key=1")

// Create another DataFrame in a new partition directory,
// adding a new column and dropping an existing column
val cubesDF = spark.sparkContext.makeRDD(6 to 10).map(i => (i, i * i * i)).toDF("value", "cube")
cubesDF.write.parquet("data/test_table/key=2")

// Read the partitioned table
val mergedDF = spark.read.option("mergeSchema", "true").parquet("data/test_table")
mergedDF.printSchema()

// The final schema consists of all 3 columns in the Parquet files together
// with the partitioning column appeared in the partition directory paths
// root
//  |-- value: int (nullable = true)
//  |-- square: int (nullable = true)
//  |-- cube: int (nullable = true)
//  |-- key: int (nullable = true)

   加      等于   

DF.write.parquet("path")这种写法是一个简写,通常开发的时候我们会:

DF.write.format("parquet").mode("overwrite").save("path")

或者

Import org.apache.spark.sql.SaveMode

DF.write.format("parquet").mode(SaveMode.Overwrite).save("path")

关于save mode的一些介绍


                                       b)Catalyst 自动优化

                                   2.Less Data

                                       分析大数据最快的方法是什么?

                                       答:只拿我们需要的,忽略过滤无关数据,用到的技术比如按时间分区,文件存储格式(ORC Parqurt)压缩,列式存储,过滤(where join的on),还有仅Parqurt格式支持的为谓词下推和映射

                                   3.Let the optimizer do the hard work

                                       举个例子:求工资大于30000,只需要name,不需要age和salary

                                         case class Person(name:String, age:Int, salary:Double)
                                       sc.textFile("")
                                       .map(x=>split("\t"))
                                       .map(x => Person(......))
                                       .map(x=> (name, salary))
                                       .filter(_._2 > 30000)
                                       .map(_._1)

                                       .collect

如何去优化?


第一步:将filter操作提前,

第二步:列裁剪 分区裁剪,在读取列存储的数据的时候,通过PPD谓词下压在Nosql数据库中的index中直接读满足条件的数据,不满足条件的数据直接跳过不读。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值