代码之家  ›  专栏  ›  技术社区  ›  Ellen Spertus

如何使用类型安全API模拟烫伤的文本行?

  •  1
  • Ellen Spertus  · 技术社区  · 7 年前

    我在嘲笑一个 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 , the 调试 输出为:

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

    如果我从 Data3 , the 调试 输出为:

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

    所有运行都会使测试失败,并显示相同的错误消息:

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

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ellen Spertus    7 年前

    在这种特殊情况下,问题在于 Data1 . 如果你写的是:

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

    您应该得到预期的输出:

    ['0', 'line0']
    ['line0']
    ['1', 'line1']
    ['line1']
    ['2', 'line2']
    ['line2']
    
    推荐文章