DataFrames与RDDs的相互转换
import org.apache.spark.sql.{DataFrameReader, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
object InferringSchema {
def main(args: Array[String]) {
//创建SparkConf()并设置App名称
val conf = new SparkConf().setAppName("SQL-intsmaze")
//SQLContext要依赖SparkContext
val sc = new SparkContext(conf)
//创建SQLContext
val sqlContext = new SQLContext(sc)
//从指定的地址创建RDD
val lineRDD = sc.textFile("hdfs://192.168.19.131:9000/person.tzt").map(_.split(","))
//创建case class
//将RDD和case class关联
val personRDD = lineRDD.map(x => Person(x().toInt, x(), x().toInt))
//导入隐式转换,如果不导入无法将RDD转换成DataFrame
//将RDD转换成DataFrame
import sqlContext.implicits._
val personDF = personRDD.toDF
//注册表
personDF.registerTempTable("intsmaze")
//传入SQL
val df = sqlContext.sql("select * from intsmaze order by age desc limit 2")
//将结果以JSON的方式存储到指定位置
df.write.json("hdfs://192.168.19.131:9000/personresult")
//停止Spark Context
sc.stop()
}
}
//case class一定要放到外面
case class Person(id: Int, name: String, age: Int)
spark shell中不需要导入sqlContext.implicits._是因为spark shell默认已经自动导入了。

打包提交到yarn集群:
/home/hadoop/app/spark/bin/spark-submit --class InferringSchema \
--master yarn \
--deploy-mode cluster \
--driver-memory 512m \
--executor-memory 512m \
--executor-cores \
--queue default \
/home/hadoop/sparksql-1.0-SNAPSHOT.jar
通过编程接口指定Schema(Programmatically Specifying the Schema)
当JavaBean不能被预先定义的时候,编程创建DataFrame分为三步:
从原来的RDD创建一个Row格式的RDD.
创建与RDD中Rows结构匹配的StructType,通过该StructType创建表示RDD的Schema.
通过SQLContext提供的createDataFrame方法创建DataFrame,方法参数为RDD的Schema.
import org.apache.spark.sql.{Row, SQLContext}
import org.apache.spark.sql.types._
import org.apache.spark.{SparkContext, SparkConf}
object SpecifyingSchema {
def main(args: Array[String]) {
//创建SparkConf()并设置App名称
val conf = new SparkConf().setAppName("SQL-intsmaze")
//SQLContext要依赖SparkContext
val sc = new SparkContext(conf)
//创建SQLContext
val sqlContext = new SQLContext(sc)
//从指定的地址创建RDD
val personRDD = sc.textFile(args()).map(_.split(","))
//通过StructType直接指定每个字段的schema
val schema = StructType(
List(
StructField("id", IntegerType, true),
StructField("name", StringType, true),
StructField("age", IntegerType, true)
)
)
//将RDD映射到rowRDD
val rowRDD = personRDD.map(p => Row(p().toInt, p().trim, p().toInt))
//将schema信息应用到rowRDD上
val personDataFrame = sqlContext.createDataFrame(rowRDD, schema)
//注册表
personDataFrame.registerTempTable("intsmaze")
//执行SQL
val df = sqlContext.sql("select * from intsmaze order by age desc ")
//将结果以JSON的方式存储到指定位置
df.write.json(args())
//停止Spark Context
sc.stop()
}
}
将程序打成jar包,上传到spark集群,提交Spark任务
/home/hadoop/app/spark/bin/spark-submit --class SpecifyingSchema \
--master yarn \
--deploy-mode cluster \
--driver-memory 512m \
--executor-memory 512m \
--executor-cores \
--queue default \
/home/hadoop/sparksql-1.0-SNAPSHOT.jar \
hdfs://192.168.19.131:9000/person.txt hdfs://192.168.19.131:9000/intsmazeresult
/home/hadoop/app/spark/bin/spark-submit --class SpecifyingSchema \
--master yarn \
--deploy-mode client \
--driver-memory 512m \
--executor-memory 512m \
--executor-cores \
--queue default \
/home/hadoop/sparksql-1.0-SNAPSHOT.jar \
hdfs://192.168.19.131:9000/person.txt hdfs://192.168.19.131:9000/intsmazeresult
在maven项目的pom.xml中添加Spark SQL的依赖
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.</artifactId>
<version>1.6.</version>
</dependency>
DataFrames与RDDs的相互转换的更多相关文章
- 2.sparkSQL--DataFrames与RDDs的相互转换
Spark SQL支持两种RDDs转换为DataFrames的方式 使用反射获取RDD内的Schema 当已知类的Schema的时候,使用这种基于反射的方法会让代码更加简洁而且效果也很好. 通 ...
- Spark SQL 之 DataFrame
Spark SQL 之 DataFrame 转载请注明出处:http://www.cnblogs.com/BYRans/ 概述(Overview) Spark SQL是Spark的一个组件,用于结构化 ...
- Spark SQL 官方文档-中文翻译
Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...
- SparkSql官方文档中文翻译(java版本)
1 概述(Overview) 2 DataFrames 2.1 入口:SQLContext(Starting Point: SQLContext) 2.2 创建DataFrames(Creating ...
- Spark记录-SparkSql官方文档中文翻译(部分转载)
1 概述(Overview) Spark SQL是Spark的一个组件,用于结构化数据的计算.Spark SQL提供了一个称为DataFrames的编程抽象,DataFrames可以充当分布式SQL查 ...
- 转】Spark SQL 之 DataFrame
原博文出自于: http://www.cnblogs.com/BYRans/p/5003029.html 感谢! Spark SQL 之 DataFrame 转载请注明出处:http://www.cn ...
- DataFrames,Datasets,与 SparkSQL
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets(中英双语)
文章标题 A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets 且谈Apache Spark的API三剑客:RDD.Dat ...
- Spark RDDs vs DataFrames vs SparkSQL
简介 Spark的 RDD.DataFrame 和 SparkSQL的性能比较. 2方面的比较 单条记录的随机查找 aggregation聚合并且sorting后输出 使用以下Spark的三种方式来解 ...
随机推荐
- 如何Request客户端的传值的Data
我们在做B/S的项目,客户端向服务端传值的时候,一般都是request接受. Request常用三个接受方式为:Request.QueryString,Request.Form,Request.Par ...
- Linux下安装配置SVN
1.检查系统上是否安装了SVN rpm -qa subversion 没有安装,则使用以下命令安装 yum -y install subversion 2.配置svn并启动svn服务 (1) 指定s ...
- SpringMvc 400 Bad Request解决方法
今天做项目的时候突然报出400 Bad Request错误,后台没有出现任何问题. 首先我看了看log日志中没有接受到任何参数,可以确定这个请求并没有发送出去,所以应该是前台数据提交的问题. 然后我看 ...
- BNU4208:Bubble sort
冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面.即首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数放前,大数放后,如 ...
- Go基础---->go的基础学习(二)
这里记录的是go中函数的一些基础知识.道听途说终是浅,身临其境方知深. go的基础知识 一.go中函数的基础使用 package main import ( "fmt" " ...
- Git介绍和基本原理
官方文档:http://git-scm.com/doc 1.1 起步 - 关于版本控制 本章关于开始学习 Git. 我们从介绍有关版本控制工具的一些背景知识开始,然后讲解如何在你的系统运行 Git,最 ...
- 以用户名注册来分析三种Action获取数据的方式
1.注入属性 直接注入属性: public String userName; public String getUserName() { return userName; } public void ...
- ios 监听设备旋转方向
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { if(fromI ...
- Win10安装软件时出现2502、2503错误代码的问题
主要是权限不够,C:\Windows\temp先访问权限 找到该目录,选择temp文件夹,右键弹出快捷菜单,选择“管理员取得所有权”.确定,OK. 再安装软件OK.
- 委托(Func与Action)
1.平时我们如果要用到委托一般都是先声明一个委托类型,比如: private delegate string Say(); string说明适用于这个委托的方法的返回类型是string类型,委托名Sa ...