您的位置:首页 > 数据库

sparkSQL学习记录之一

2016-03-31 16:33 363 查看
Spark SQL是一个为了结构化数据处理的模块,并且提供了一个叫做DataFrames的编程抽象,它也能够作为分布式SQL查询引擎。

DataFrames是一个分布式的由指定的列组成的数据集合,它相当于关系数据库中的表,或者R/Python中的数据框架,但是能够进行更多的优化。Data能够被各种wide array源所构造,如,结构化的数据文件,hive中表,外部数据库,或者存在的RDDs。

DataFrame API可以支持JAVA,SCALA,Pthon,下面的程序都是以scala来编写。

我们可以通过spark-shell来进行下面的例子的运行。

所有函数的入口点便是
SQLContext
 个类,或者它的某个子类。但是要创建一个基本的SQLContext,我们需要一个SparkContext。

使用shell,sc会已经被创建,此时我们需要使用该语句来创建一个sqlContext。

valsqlContext=neworg.apache.spark.sql.SQLContext(sc)

下面我们根据官方的例子来演示下DataFrame的操作。

val sc: SparkContext // An existing SparkContext.

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

// Create the DataFrame

val df = sqlContext.jsonFile("examples/src/main/resources/people.json")

// Show the content of the DataFrame

df.show()

// Print the schema in a tree format

df.printSchema()

// Select only the "name" column

df.select("name").show()

// Select everybody, but increment the age by 1

df.select("name", df("age") + 1).show()

// Select people older than 21

df.filter(df("name") > 21).show()

// Count people by age

df.groupBy("age").count().show()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: