博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spark rdd 和 DF 转换
阅读量:4259 次
发布时间:2019-05-26

本文共 1104 字,大约阅读时间需要 3 分钟。

RDD   -》 DF

 

有两种方式

一、

 

一、Inferring the Schema Using Reflection

 

将 RDD[t]   转为一个 object ,然后 to df

 

val peopleDF = spark.sparkContext  .textFile("examples/src/main/resources/people.txt")  .map(_.split(","))  .map(attributes => Person(attributes(0), attributes(1).trim.toInt))  .toDF()

 

 

rdd 也能直接装 DATASet  要  import 隐式装换 类 import spark.implicits._

 如果  转换的对象为  tuple .   转换后  下标为 _1  _2   .....

 

 

 

二、Programmatically Specifying the Schema

 

把 columnt meta  和  rdd   createDataFrame 在一起

 

val peopleRDD = spark.sparkContext.textFile("examples/src/main/resources/people.txt")// The schema is encoded in a stringval schemaString = "name age"// Generate the schema based on the string of schemaval fields = schemaString.split(" ")  .map(fieldName => StructField(fieldName, StringType, nullable = true))val schema = StructType(fields)

 

val rowRDD = peopleRDD  .map(_.split(","))  .map(attributes => Row(attributes(0), attributes(1).trim))// Apply the schema to the RDDval peopleDF = spark.createDataFrame(rowRDD, schema)// Creates a temporary view using the DataFramepeopleDF.createOrReplaceTempView("people")

 

 

 

 

 

 

DF  to  RDd

 

val tt = teenagersDF.rdd

转载地址:http://fylei.baihongyu.com/

你可能感兴趣的文章
二叉树按层遍历并按层打印和蛇形打印
查看>>
二叉树打印节点和为某值的全部路径
查看>>
打印普通二叉树最大搜索子树
查看>>
bitmap用途
查看>>
LRUCache
查看>>
布隆过滤器
查看>>
Hash总览
查看>>
关于redis
查看>>
排序总览
查看>>
关于c++的class(继承、重载、隐藏)
查看>>
关于c++的class(偏c++11以前,构造、静态成员、const、初始化列表、友元、内联、template)
查看>>
关于网络(同步、异步、阻塞、非阻塞,select/poll/epoll,rpc/msgqueue,tcpip常见面试题)
查看>>
一道特殊的排序面试题(交换思想活学活用)
查看>>
c++11新特性总结
查看>>
设计模式之:单例模式
查看>>
leetcode之数值计算类-----9. Palindrome Number(判断一个数是否为回文数)
查看>>
leetcode之链表类之链表排序-----147/148. 链表快速排序 链表插入排序
查看>>
leetcode之链表类之链表归并类-----OJ 2/21/23/445 链表相加求和 链表归并
查看>>
leetcode之链表逆序翻转类-----92/206 逆序 24/25/61/143 按规则翻转 86/234 双指针分治 19/82/83/203 按规则删除
查看>>
leetcode之深搜递归回溯类-----1/167/653. two sum(记忆化搜索寻找和为给定值的两个数)
查看>>