简体   繁体   English

如何在Scala Spark中反向映射和遍历键和值

[英]How to reverse map and loop through keys and values in Scala Spark

I am writing a Scala Spark script where I am storing (Hive partitions) HDFS paths as keys and their underlying file count as values. 我正在编写一个Scala Spark脚本,其中将(Hive分区)HDFS路径存储为键,并将其基础文件计数为值。 I want to write a for loop to check if the filepath exists (using key) and count is greater than 0. I have to reverse the whole map because I want to start checking for latest partition. 我想编写一个for循环来检查文件路径是否存在(使用键)并且count大于0。我必须反转整个映射,因为我想开始检查最新的分区。

I couldn't find any method to reverse the map and loop through it. 我找不到任何方法来反转地图并循环通过它。 Here's my code: 这是我的代码:

val paths = collection.mutable.Map[String, Float]()
status.foreach( x => paths += (x.getPath.toString -> fs.getContentSummary(new Path(x.getPath.toString).getFileCount() )

This is what I am planning to do : 这是我打算做的事情:

//Reverse the map to make last element as first element
var result = ""
for ((k,v) <- paths) {
if(!fs.exists(new Path(k)) && v < 1)
  continue
else
  result = k
  break
}

"Up Vote" if answer works for you. 如果答案适合您,请“投票”。

You need to use ListMap and then you sort map based on key ( basically you can reverse the map and perform further operations ). 您需要使用ListMap,然后根据键对地图进行排序(基本上,您可以反转地图并执行进一步的操作)。

import scala.collection.immutable.ListMap 导入scala.collection.immutable.ListMap

val paths_sortBy_key_asc = ListMap(paths.toSeq.sortBy(_._1) < _. 1: *) val path_sortBy_key_asc = ListMap(paths.toSeq.sortBy(_._ 1)<_。 1: *)

or 

val paths_sortBy_key_dsc = ListMap(paths.toSeq.sortBy(_._1) > _. 1: *) val path_sortBy_key_dsc = ListMap(paths.toSeq.sortBy(_._ 1)> _。1 *)

Hope this helps !! 希望这可以帮助 !!

There are multiple options. 有多种选择。

  1. Use TreeMap to construct data structure. 使用TreeMap构造数据结构。 Its is sorted by keys. 它是按键排序的。
  2. Convert it to a seq or list and sort it. 将其转换为seq或列表并对其进行排序。

All these options are outlined here with an example. 所有这些选项均在此处通过示例进行了概述。

refer to sort map link 参考排序图链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM