如何从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}作为输出。
答案 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))
移除empty
和null
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
我希望这会对你有所帮助!