首页 > 其他分享 >DataFrame与rdd之间的转换(val rdd1 = dataFrame.rdd)

DataFrame与rdd之间的转换(val rdd1 = dataFrame.rdd)

时间:2022-08-30 08:36:19浏览次数:47  
标签:rdd1 String val dataFrame rdd DataFrame Student

核心语句val rdd1 = dataFrame.rdd

package SparkSQL.DataFreamCreate.dataframetordd

import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{DataTypes, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}
import scala.beans.BeanProperty

object dftordd1 {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("dataFrameCreate").setMaster("local[*]")
    val sparkSession = SparkSession.builder().config(conf).getOrCreate()

    val seq:Seq[Student] = Array(Student("zs",20,"男"),Student("ls",21,"女"),Student("ww",22,"男"))
    val rdd:RDD[Student] = sparkSession.sparkContext.makeRDD(seq)
    val dataFrame:DataFrame = sparkSession.createDataFrame(rdd,classOf[Student])
    dataFrame.show()

    val rdd1 = dataFrame.rdd
    val rdd2: RDD[Row] = rdd1.map(row => {
      Row(row.getAs[String]("name"), row.getAs[Int]("age") + 5, row.getAs[String]("sex"))
    })
    val structType = StructType(Array(
      StructField("name", DataTypes.StringType),
      StructField("age", DataTypes.IntegerType),
      StructField("sex", DataTypes.StringType)
    ))
    val frame: DataFrame = sparkSession.createDataFrame(rdd2, structType)
    frame.show()
  }
}
case class Student(@BeanProperty var name:String,@BeanProperty var age:Int,@BeanProperty var sex:String)

标签:rdd1,String,val,dataFrame,rdd,DataFrame,Student
From: https://www.cnblogs.com/jsqup/p/16638035.html

相关文章

  • React报错之Property 'value' does not exist on type EventTarget
    正文从这开始~总览当event参数的类型不正确时,会产生"Property'value'doesnotexistontypeEventTarget"错误。为了解决该错误,将event的类型声明为React.ChangeEvent......
  • CF1455G Forbidden Value 题解
    CF1455GForbiddenValue已知初始值\(x=0\),给定下面2种命令:set\(y\)\(v\),令\(x=y\),或花费\(v\)元钱删除该命令;if\(y\)...end,如果\(x==y\),执行if...end中的命令,否......
  • assert failed: tcpip_send_msg_wait_sem IDF/components/lwip/lwip/src/api/tcpip.c:
    assertfailed:tcpip_send_msg_wait_semIDF/components/lwip/lwip/src/api/tcpip.c:455(Invalidmbox)assertfailed:tcpip_send_msg_wait_semIDF/components/lwip/l......
  • Public Key Retrieval is not allowed
    运行jar程序报错PublicKeyRetrievalisnotallowed 1.修改程序配置文件中的连接数据库的url,加上allowPublicKeyRetrieval=true参数,失败2.修改default_authenticati......
  • Map遍历 key-value 的4种方法
    四种方法先用keySet()取出所有key值,再取出对应value——增强for循环遍历先用keySet()取出所有key值,再取出对应value——使用迭代器遍历通过entrySet来获取key-value—......
  • EvaluationSystem:后端业务接口(js同步操作数据库)
    1、用户业务接口(services/user.js)用户相关业务:注册账号登录账号查看用户信息修改个人资料2、数据业务接口(services/data.js)添加一条数据查询一条数据所有数据......
  • EvaluationSystem:数据库模型建立
    1、用户table(./models/user.js)用户字段:useraccount:账号(主键)nickname:昵称password:密码evalnum:已参与测评数量2、数据table(./models/data.js)数据字段:name:数据名字......
  • EvaluationSystem:中间件和共享模块
    1、共享模块(shared)【第一】数据库连接(shared/sequelize.js)//数据库const{Sequelize}=require('sequelize');module.exports=newSequelize({dialect:'mys......
  • setTimeout、setInterval 和 requestAnimationFrame
    与setTimeout和setInterval不同,requestAnimationFrame不需要设置时间间隔,大多数电脑显示器的刷新频率是60Hz,大概相当于每秒钟重绘60次。大多数浏览器都会对重绘......
  • EvaluationSystem:路由设置
    1、首页路由(routes/home.js)2、用户路由(routes/user.js)3、数据路由(routes/data.js)4、测评路由(routes/ceping.js)5、管理员路由(routes/)(//::todo)......