如何在数组中执行快速查找[org.apache.spark.sql.Row]

时间:2018-06-14 14:22:13

标签: scala

如何从Array[org.apache.spark.sql.Row]执行查找。

我有一个如下所示的数组:

Array[org.apache.spark.sql.Row] = Array([Aber,Abercrombie], [AbercrombieKids,Abercrombie Kids], [AceHardware,Ace Hardware], [Acme Market,Acme Fresh Market])

我的输入将是一个字符串值,例如" AbercrombieKids",如果字符串与数组的第一个索引值完全匹配,那么我应该得到第二个索引值,即{{1}作为输出。

1 个答案:

答案 0 :(得分:0)

我试图处理你的所有场景,请通过解释找到以下答案

输入所有场景

scala> val rowArr=Array(Row("Aber","Abercrombie"), Row("AbercrombieKids","",null,"Abercrombie Kids"), Row("AceHardware","Ace Hardware"),Row(null,"Acme Market","Acme Fresh Market",null),Row(null,null))
rowArr: Array[org.apache.spark.sql.Row] = Array([Aber,Abercrombie], [AbercrombieKids,,null,Abercrombie Kids], [AceHardware,Ace Hardware], [null,Acme Market,Acme Fresh Market,null], [null,null])

scala> val matchStr="AbercrombieKids"
matchStr: String = AbercrombieKids

Array[org.apache.spark.sql.Row]转换为Array[Array[String]]

scala> val arrOfArrStr=rowArr.map(Row=>Row.mkString(",").split(","))
arrOfArrStr: Array[Array[String]] = Array(Array(Aber, Abercrombie), Array(AbercrombieKids, "", null, Abercrombie Kids), Array(AceHardware, Ace Hardware), Array(null, Acme Market, Acme Fresh Market, null), Array(null, null))

移除emptynull Strings,然后选择Arrays大于1的length

scala> val arrWithoutNull=arrOfArrStr.map(arr=>arr.filter(str=>str!="null" && str.size>0)).filter(_.length>1)
arrWithoutNull: Array[Array[String]] = Array(Array(Aber, Abercrombie), Array(AbercrombieKids, Abercrombie Kids), Array(AceHardware, Ace Hardware), Array(Acme Market, Acme Fresh Market))

现在,在String的{​​{1}}中匹配您的arrWithoutNull,并仅Array[Array[String]]右侧匹配Arrays

String

如果scala> val matchedArrStr=arrWithoutNull.filter(_.contains(matchStr)).flatMap(arr=>arr.drop(arr.indexOf(matchStr)+1)) matchedArrStr: Array[String] = Array(Abercrombie Kids) 不是head

,请立即matchedArrStr matchedArrStr
empty

我希望这会对你有所帮助!