首页 > 解决方案 > Apache Spark - 使用和不使用案例类的性能

问题描述

我有 2 个数据集,客户和订单

我想同时加入客户密钥。

我尝试了两种方法,一种使用案例类,一种不使用。

使用案例类: -> 只需要永远完成 - 几乎 11 分钟

case class Customer(custKey: Int, name: String, address: String, phone: String, acctBal: String, mktSegment: String, comment: String) extends Serializable

case class Order(orderKey: Int, custKey: Int, orderStatus: String, totalPrice: Double, orderDate: String, orderQty: String, clerk: String, shipPriority: String, comment: String) extends Serializable

val customers = sc.textFile("customersFile").map(row => row.split('|')).map(cust => (cust(0).toInt, Customer(cust(0).toInt, cust(1), cust(2), cust(3), cust(4), cust(5), cust(6))))
val orders = sc.textFile("ordersFile").map(row => row.split('|')).map(order => (order(1).toInt, Order(order(0).toInt, order(1).toInt, order(2), order(3).toDouble, order(4), order(5), order(6), order(7), order(8))))

orders.join(customers).take(1)

没有案例类——几秒钟内完成

val customers = sc.textFile("customersFile").map(row => row.split('|'))
val orders = sc.textFile("ordersFile").map(row => row.split('|'))
val customersByCustKey = customers.map(row => (row(0), row)) // customer key is the first column in customers rdd, hence row(0) 
val ordersByCustKey = orders.map(row => (row(1), row)) //  customer key is the second column in orders rdd, hence row(1) 

ordersByCustKey.join(customersByCustKey).take(1)

想知道这是否是由于使用案例类时序列化/反序列化所花费的时间?

如果是,建议在哪些情况下使用案例类?

使用案例类的作业详细信息: 工作详情 - 案例类

没有案例类的工作细节: 没有案例类的工作细节

标签: apache-sparkrddcase-class

解决方案


推荐阅读