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来进行下面的例子的运行。
所有函数的入口点便是
使用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()
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()
相关文章推荐
- CoreData数据库迁移的操作
- oracle数据库密码过期怎么办
- MySQL InnoDB 悲观锁机制
- mysql 修改主键自增值(起始值)
- 使用sqoop 1.4.4从mysql向hdfs导入表出现链接数据库失败错误的解决办法
- oracle统计数据库所有表的数据记录数SQL
- mysql:键缓存
- sql2008数据备份和还原,数据表导出导入
- IT忍者神龟之基于CAS实现单点登录(SSO)之配置CAS服务端的数据库查询认证机制(一)
- 使用SAS/ACCESS For Oracle时字段长度映射翻倍问题
- Mysql分段统计简单数据信息例如年龄,省份,性别等等
- Mysql5.7忘记root密码及mysql5.7修改root密码的方法
- ubuntu 安装mariadb galera版本
- 在Windows环境下MongoDB搭建和简单操作
- mysql乐观锁总结和实践
- (转)MYSQL获取自增主键【4种方法】
- Oracle 11g 安装
- mysql记录sql执行时间
- Environment variable ORACLE_UNQNAME not defined.Please set ORACLE_UNQNAME to database unique name.
- 搭建sparksql的hive测试环境