spark2.0之后的Dataset官网信息翻译。。。

本文介绍如何在Spark中创建DataSet并进行基本操作,包括使用case class定义编码器、自动提供的常见数据类型编码器及DataFrame与DataSet间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一个DataSet(数据集)

但是,数据集类似于RDDs,它们不使用Java序列化或Kryo(官方文档的地址https://github.com/EsotericSoftware/kryo/blob/master/README.md),而是使用专门的编码器对对象进行序列化,以便通过网络进行处理或传输。尽管编码器和标准序列化都负责将对象转换成字节,编码器是动态生成的代码,并使用允许Spark执行许多操作(如过滤、排序和散列)的格式,但是不会将字节反序列化为对象。

// Note: Case classes in Scala 2.10 can support only up to 22 fields. To work around this limit,
// you can use custom classes that implement the Product interface
case class Person(name: String, age: Long)

// 利用case class类来创建encoder编码器
val caseClassDS = Seq(Person("Andy", 32)).toDS()
caseClassDS.show()
// +----+---+
// |name|age|
// +----+---+
// |Andy| 32|
// +----+---+

// Encoders for most common types are automatically provided by importing spark.implicits._
//对于常见数据类型通过导入sparkSession的隐士转化来吧数据进行转换
val primitiveDS = Seq(1, 2, 3).toDS()
primitiveDS.map(_ + 1).collect() // Returns: Array(2, 3, 4)

// DataFrames can be converted to a Dataset by providing a class. Mapping will be done by name
people.json :内容如下 
 {"name":"tom", "age":12}
 {"name":"jerry", "age":23}
 {"name":"roky", "age":45}
val path = "examples/src/main/resources/people.json"
val peopleDS = spark.read.json(path).as[Person]
peopleDS.show()
// +----+-------+
// | age|   name|
// +----+-------+
// |null|Michael|
// |  30|   Andy|
// |  19| Justin|
// +----+-------+

另外关于Dataframe和Dataset之间的相互转换可以通过sparkSession的隐士转换。当然也是要借助case class,相当于为表提供了schema.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值