【问题标题】:Read certain folders based on a variable根据变量读取某些文件夹
【发布时间】:2021-03-23 18:55:25
【问题描述】:

我有以下文件夹结构:

/mnt/tmp/fldr1
/mnt/tmp/fldr2
/mnt/tmp/fldr3
/mnt/tmp/fldr4
/mnt/tmp/fldr5
/mnt/tmp/fldr6

基于一个变量,我将从某些文件夹中读取镶木地板文件。

eg. 
if trail = 2 I would read only fldr1 and fldr2
if trail = 4 I would read only fldr1, fldr2, fldr3 and fldr4
if trail = 6 I would read all the folders

我有以下代码。

scala> val cnt = 2
val cnt: Int = 2

scala> val xs = List(1, cnt)
val xs: List[Int] = List(1, 2)

scala> val ys = List("/mnt/tmp1", "/mnt/tmp2")
val ys: List[String] = List(/mnt/tmp1, /mnt/tmp2)

scala> val cross = xs.flatMap(x => ys.map(y => (y + "/trial=" + x.toString)))
val cross: List[String] = List(/mnt/tmp1/trial=1, /mnt/tmp2/trial=1, /mnt/tmp1/trial=2, /mnt/tmp2/trial=2)

然后我会通读路径并获取镶木地板文件。

还有其他更简单的方法吗?

【问题讨论】:

  • 你只需要一个骨架——如何根据变量从目录中读取,还是你想要一个完整的spark程序?
  • 我已经用我刚刚编写的代码编辑了这个问题......不确定是否有更好的方法来做到这一点!

标签: scala apache-spark databricks


【解决方案1】:

基本骨架可以是:

val dirsstr="/mnt/tmp/fldr1,/mnt/tmp/fldr2,/mnt/tmp/fldr3,/mnt/tmp/fldr4,/mnt/tmp/fldr5,/mnt/tmp/fldr6"
val dirs = dirsstr.split(",")

//get an random generator - you will have your own input
val trail = scala.util.Random

trail.nextInt(6) match{
  case 2 => {
    readFiles(dirs(0));
    readFiles(dirs(1));
  }
  case 4 => for(i <- (0 to 3)) readFiles(dirs(i));
  case 6 => dirs.foreach(dir => readFiles(dir))
  case _ => println("Out of scope of your question :)")
}

readFiles 取决于您的火花、镶木地板的上下文。当然,还有很多其他(也许更好)的方法可以做到这一点,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-08
    • 2021-03-27
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2021-03-18
    相关资源
    最近更新 更多