sparkSQL学习记录之二

在SPARKSQL中也可以运行SQL语句来生成DataFrame。

如val df=sqlContext.sql("SELECT * FROM table")

Spark SQL支持2种方式转换存在的RDDS为DataFrames,第一中方式是使用反射去推断包含特定类型的对象的RDD的表的模式。当你在写Spark应用程序时,已经知道表的模式这种基于反射的方法会使代码更简洁,使用起来方便。第二种方式是通过程序接口,允许你构造一个模式并且应用该模式到一个存在的RDD中。这种方式显得很冗余,但是它允许你在运行程序前,而不知道列名和列的类型时,去构造DataFrames。

下面先看使用反射来推断表的模式:

Scala的Spark SQL接口支持自动将包含用例类的RDD转换为DataFrame。用例类定义了表的模式。使用反射,用例类的参数的名字会被读取,变成列的名字。用例类也可以被嵌套,或者包含更复杂的类型,比如Sequence或者Arrays。RDD会被透明的转换成DataFrame,然后被注册成一个数据表。数据表可以使用SQL语句来进行操作。

示例代码:

//sc是一个存在的SparkContext

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

//这个可以透明的转换RDD为DataFrame
import sqlContext.implicits._

//定义的用例类
case class Person(name: String, age: Int)

//创建一个RDD并且注册为表
val people = sc.textFile("examples/src/main/resources/people.txt").map(_.split(",")).map(p => Person(p(0), p(1).trim.toInt)).toDF()
people.registerTempTable("people")

//使用SQL语句
val teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")

//打印成People的名字
teenagers.map(t => "Name: " + t(0)).collect().foreach(println)


使用特定的模式:

当用例类不可以提前被定义时,我们可以使用下面的三个步骤来创建一个DataFrame

1.使用原始的RDD来创建一个基于行的RDD

2.使用StructType来创建表的模式,并其匹配步骤1创建的RDD的行的结构.

3.通过使用SQLContext提供的createDataFrame方法,应用该模式到基于行的RDD。

示例代码如下:

// sc is an existing SparkContext.
val sqlContext = new org.apache.spark.sql.SQLContext(sc)


// Create an RDD
val people = sc.textFile("examples/src/main/resources/people.txt")


// The schema is encoded in a string
val schemaString = "name age"


// Import Spark SQL data types and Row.
import org.apache.spark.sql._


// Generate the schema based on the string of schema
val schema =
  StructType(
    schemaString.split(" ").map(fieldName => StructField(fieldName, StringType, true)))


// Convert records of the RDD (people) to Rows.
val rowRDD = people.map(_.split(",")).map(p => Row(p(0), p(1).trim))


// Apply the schema to the RDD.
val peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema)


// Register the DataFrames as a table.
peopleDataFrame.registerTempTable("people")


// SQL statements can be run by using the sql methods provided by sqlContext.
val results = sqlContext.sql("SELECT name FROM people")


// The results of SQL queries are DataFrames and support all the normal RDD operations.
// The columns of a row in the result can be accessed by ordinal.
results.map(t => "Name: " + t(0)).collect().foreach(println)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值