【问题标题】:How to mock a TextLine for Scalding using the type safe API?如何使用类型安全 API 模拟 TextLine 以进行 Scalding?
【发布时间】:2018-07-26 11:20:28
【问题描述】:

我正在尝试为 Scalding 作业模拟 TextLine,但无论我是显式还是隐式表达偏移量,偏移量似乎都混入了行中。

这是我的工作:

package changed

import com.twitter.scalding._
import com.twitter.scalding.typed.TDsl._

class MyJob(args: Args) extends Job(args) { 
  val mySource = TextLine(args("input"))
  val myPipe : TypedPipe[String] = mySource
    .read
    .debug
    .toTypedPipe[String]('line)
    .debug
    .write(TypedTsv[String](args("output")))
}

请注意,我在转换为类型安全 API 之前和之后记录了元组。

这是我的测试:

package changed

import com.twitter.scalding.{JobTest, TextLine, TypedTsv, TupleConversions}
import org.scalatest.FunSpec

class MyTest extends FunSpec with TupleConversions {
  val Input = "/tmp/testInput"
  val Output = "/tmp/testOutput"
  val Data1 = List((0 -> "line0", 1 -> "line1", 2 -> "line2"))
  val Data2 = List((0, "line0", 1, "line1", 2, "line2"))
  val Data3 = List(("line0", "line1", "line2"))

  JobTest("sandcrawler.MyJob")
    .arg("test", "")
    .arg("app.conf.path", "app.conf")
    .arg("output", Output)
    .arg("input", Input)
    .arg("debug", "true")
    .source(TextLine(Input), Data1)
    .sink[String](TypedTsv[String](Output)) {
      outputBuffer =>
      it("should return a 3-element list.") {
        assert(outputBuffer.size === 3)
      }
    }
    .run
    .finish
}

如果我从常量列表Data1 中得到输入,如上所示,两次调用debug 输出的元组(分别)是:

['(0,line0)', '(1,line1)']
['(1,line1)']

如果我从Data2 获得输入,debug 的输出是:

['0', 'line0']
['line0']

如果我从Data3 获得输入,debug 的输出是:

['line0', 'line1']
['line1']

所有运行都以相同的错误消息使测试失败:

[info] MyTest:
[info] - should return a 3-element list. *** FAILED ***
[info]   1 did not equal 3 (MyTest.scala:23)

换句话说,只写入一个元组。

我应该如何表示/访问我的模拟数据?

【问题讨论】:

    标签: mocking cascading scalding


    【解决方案1】:

    在这种特定情况下,问题在于Data1 周围的额外括号集。如果你改为写:

    val Data1 = List(0 -> "line0", 1 -> "line1", 2 -> "line2")
    

    你应该得到预期的输出:

    ['0', 'line0']
    ['line0']
    ['1', 'line1']
    ['line1']
    ['2', 'line2']
    ['line2']
    

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 2013-08-18
      • 2019-05-21
      • 1970-01-01
      • 2021-07-11
      相关资源
      最近更新 更多