[英]Scala/RDD : How to compare a value of tuple with a list of values in the same tuple
我有如下數據
val t=((1,List(1,2,3,4)),(2,List(1,2,3,4)),(3,List(1,2,3,4)),(4,List(1,2,3,4)))
我想要像這樣的輸出:
1--1
2--2
3--3
4--4
可以通過使用scala或spark core來幫助身體嗎?
嘗試這個:
val spark = SparkSession.builder()
.appName("app_name")
.master("local[2]")
.getOrCreate()
import spark.implicits._
val df = spark.sparkContext.parallelize(Seq((1,List(1,2,3,4)),(2,List(1,2,3,4)),(3,List(1,2,3,4)),(4,List(1,2,3,4))))
df.map{
case (id, list) => s"${id}--${list(id - 1)}"
}.toDF().show()
使用RDD的另一種方法:
輸入:
(1,List(1, 2, 3, 4))
(2,List(1, 2, 3, 4))
(3,List(1, 2, 3, 4))
(4,List(1, 2, 3, 4))
t.map(x => s"""${x._1} -- ${if(x._2.contains(x._1)) x._1}""").collect.foreach(println)
輸出:
1 -- 1
2 -- 2
3 -- 3
4 -- 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.