Scala环境下载
https://www.scala-lang.org/download/all.html
Scala插件下载
https://plugins.jetbrains.com/plugin/1347-scala/versions
https://plugins.jetbrains.com/
使用Scala统计每日新增最多的城市。
object Ten1 {
def main(args: Array[String]): Unit = {
val source = scala.io.Source.fromFile("") getLines()
source.next()
val yq = source.toList.filter(_.length > 0).map(line => {
val fields = line.split(",")
(fields(0), (fields(2), fields(1)))
})
println(yq)
val re = yq.groupBy(ro => ro._1)
.map(kvs => (kvs._1, kvs._2.map(_._2).sortWith(_._1 > _._1).head)
)
println(re)
re.foreach(li => {
println("日期:" + li._1 + "\t" + "Top1:" + li._2._2 + "\t" + " 新增:" + li._2._1)
})
}
}
=========================================================================================================================
# 统计未支付的订单数量
# 每个订单的商品列表(包含哪些商品)
# 每个订单的总金额
# 找出消费最多的客户
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)
}
}
=========================================================================
词频统计
package com.fuxi.zji
object Ten4 {
def main(args: Array[String]): Unit = {
val words = """Scala combines object-oriented and functional programming in one concise, high-level language. Scala's static types help avoid bugs in complex applications, and its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries."""
val w = Array(words).toBuffer
val x = w.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size))
x.foreach(kv=>println(kv._1,kv._2))
println(x)
}
}
scala练习
最新推荐文章于 2024-08-30 23:27:36 发布