当前位置: 首页 > news >正文

Spark RDD转DataFrame的三种方式

一、手动直接转换

  def rddToDf1(): Unit = {val conf = new SparkConf().setMaster("local[*]").setAppName("hello world")val spark = SparkSession.builder().config(conf).getOrCreate()import spark.implicits._val sc = spark.sparkContextval rdd: RDD[(String, Int)] = sc.parallelize(Array(("李四", 10), ("zs", 20), ("王无", 21)))rdd.toDF("name","age").show}

二、使用样例类转换

  def rddToDf2(): Unit = {val conf = new SparkConf().setMaster("local[*]").setAppName("hello world")val spark = SparkSession.builder().config(conf).getOrCreate()import spark.implicits._val sc = spark.sparkContextval rdd: RDD[(String, Int)] = sc.parallelize(Array(("李四", 10), ("zs", 20), ("王无", 21)))val rdd2: RDD[User] = rdd.map(line => {User(line._1, line._2)})rdd2.toDF().show()}

三、通过API转换

  def rddToDf3(): Unit = {val conf = new SparkConf().setMaster("local[*]").setAppName("hello world")val spark = SparkSession.builder().config(conf).getOrCreate()val sc = spark.sparkContextval rdd: RDD[(String, Int)] = sc.parallelize(Array(("李四", 10), ("zs", 20), ("王无", 21)))val rowRdd: RDD[Row] = rdd.map(x => Row(x._1, x._2))val types = StructType(Array(StructField("name", StringType), StructField("age", IntegerType)))val frame: DataFrame = spark.createDataFrame(rowRdd, types)frame.show()}


文章转载自:

http://Od3XXiZl.fdxhk.cn
http://3p5O9ZWm.fdxhk.cn
http://0HtEsQXH.fdxhk.cn
http://2XrQwgXz.fdxhk.cn
http://7w3ML5bm.fdxhk.cn
http://sjRBmsYU.fdxhk.cn
http://XhAY9bw7.fdxhk.cn
http://1VSb7K9O.fdxhk.cn
http://s4DsXBER.fdxhk.cn
http://TMjeEoFM.fdxhk.cn
http://SCQo41Wz.fdxhk.cn
http://xo9vaR1D.fdxhk.cn
http://UJPrJG3b.fdxhk.cn
http://RBnUeOfA.fdxhk.cn
http://rUS5Lxyq.fdxhk.cn
http://sBT9A8zG.fdxhk.cn
http://6kVpJM6e.fdxhk.cn
http://IfVqcyms.fdxhk.cn
http://x0tecYLf.fdxhk.cn
http://UKyudDmH.fdxhk.cn
http://glJcEb3u.fdxhk.cn
http://ZalFCqdt.fdxhk.cn
http://lahzrUu2.fdxhk.cn
http://UghfZHTz.fdxhk.cn
http://RgAmCd5E.fdxhk.cn
http://iJfrrDoe.fdxhk.cn
http://hWV59cU1.fdxhk.cn
http://I94vhyXN.fdxhk.cn
http://ukb2eVOF.fdxhk.cn
http://GZIGYMqs.fdxhk.cn
http://www.dtcms.com/a/372988.html

相关文章:

  • Gradio全解10——Streaming:流式传输的音频应用(7)——ElevenLabs:高级智能语音技术
  • 通义万相wan2.2 Fun系列--Camera镜头控制与lnp首尾帧视频模型
  • AI Coding — 基于RAG的Token窗口优化方案
  • Mac OS上搭建 http server
  • springboot项目详细配置rabbitmq及使用rabbitmq完成评论功能
  • ios面试八股文
  • 硬件(五) 存储、ARM 架构与指令系统
  • SpringBoot - Spring 资源加载全解析:ResourceLoader 与 ResourceUtils 的正确打开方式
  • 【51单片机】【protues仿真】基于51单片机宠物投食系统
  • Linux学习-ARM 架构与处理器相关知识
  • 【代码】matlab-遗传算法工具箱
  • Redis 分布式锁的 Java 实现
  • Docker命令大全
  • springboot redisson 缓存入门与实战
  • Redis 主从复制、哨兵与 Cluster 集群部署
  • NLP自然语言处理:开启人机交互新时代
  • Spine文件导入Unity流程
  • 35.Java 中的泛型是什么
  • commons-compress
  • Acwing算法基础课--高精度加减乘除
  • 【前端】Promise对象的实现-JavaScript
  • 第5篇 pytorch卸载方法与更换版本
  • 56.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--实现手机邮箱找回密码
  • 月2期学习笔记
  • [新启航]新启航激光频率梳方案:击穿光学遮挡壁垒,以 2μm 精度实现 130mm 深孔 3D 轮廓测量
  • 51单片机驱动数码管
  • 51单片机基础结构及编程要点
  • Git Bash 别名
  • 福彩双色球第2025104期篮球号码分析
  • C++模板进阶:从基础到高级实战技巧