Scala环境下载
https://www.scala-lang.org/download/all.html
Scala插件下载
https://plugins.jetbrains.com/plugin/1347-scala/versions
https://plugins.jetbrains.com/
表1(订单id、订单日期、顾客id、订单状态)
order_id order_date order_customer_id order_status
1 2013-07-25 00:00:00 11599 CLOSED
2 2013-07-25 00:00:00 256 PENDING_PAYMENT
4 2013-07-25 00:00:00 8827 CLOSED
表2(订单明细id、订单id、商品id、商品数量、总价、单价)
order_item_id order_item_order_id order_item_product_id order_item_quantity order_item_subtotal order_item_product_price
1 1 957 1 299.98 299.98
2 2 1073 1 199.99 199.99
3 2 502 5 250 50
4 2 403 1 129.99 129.99
5 4 897 2 49.98 24.99
6 4 365 5 299.95 59.99
7 4 502 3 150 50
8 4 1014 4 199.92 49.98
题目
# 统计未支付的订单数量
# 每个订单的商品列表(包含哪些商品)
# 每个订单的总金额
# 找出消费最多的客户
代码
package com.fuxi.zji
object sss {
def main(args: Array[String]): Unit = {
val data1 = scala.io.Source.fromFile("./src/data1/").getLines()
val data2 = scala.io.Source.fromFile("./src/data2").getLines()
data1.next()
data2.next()
val d1 = data1.map(lien => {
val fields = lien.split("\t")
(fields(0).toInt, fields(1), fields(2), fields(3))
}).toList
val d2 = data2.map(line => {
val fields = line.split("\t")
(fields(0).toInt, fields(1).toInt, fields(2).toInt, fields(3).toInt,
fields(4).toDouble, fields(5).toDouble)
}).toList
//未支付的订单数量
val re1 = d1.filter(_._4 == "PENDING_PAYMENT").size
println(re1 + "个未支付")
//计算每个订单对应的商品id
val re = d1.groupBy(ro => ro._1).map(lien => (lien._1, d2.filter(lien._1 == _._2).map(_._3)))
re.foreach(li => {
println("订单号:" + li._1 + "\t" + "商品的明细:" + li._2.mkString(","))
})
//计算每个订单的总金额
val rq = d1.map(lien => (lien._1, d2.filter(lien._1 == _._2).map(_._5)))
rq.foreach(li => {
println("订单:" + li._1 + "\t" + "总金额:" + li._2.sum)
})
//找出消费最多的客户
val rw = d1.map(lien => (lien._3, d2.filter(lien._1 == _._2).map(_._5).sum)).sortWith(_._2 > _._2).head
println("消费最多的客户:" + rw._1 + "\t" + "总金额:" + rw._2)
}
}