【问题标题】:Input type must be string type but got ArrayType(StringType,true) error in Spark using Scala输入类型必须是字符串类型,但使用 Scala 在 Spark 中出现 ArrayType(StringType,true) 错误
【发布时间】:2016-10-28 17:52:52
【问题描述】:

我是 Spark 的新手,正在使用 Scala 创建一个基本分类器。我正在从文本文件中读取数据集并将其拆分为训练和测试数据集。然后我试图标记训练数据,但它失败了

Caused by: java.lang.IllegalArgumentException: requirement failed: Input type must be string type but got ArrayType(StringType,true).
at scala.Predef$.require(Predef.scala:224)
at org.apache.spark.ml.feature.RegexTokenizer.validateInputType(Tokenizer.scala:149)
at org.apache.spark.ml.UnaryTransformer.transformSchema(Transformer.scala:110)
at org.apache.spark.ml.Pipeline$$anonfun$transformSchema$4.apply(Pipeline.scala:180)
at org.apache.spark.ml.Pipeline$$anonfun$transformSchema$4.apply(Pipeline.scala:180)
at scala.collection.IndexedSeqOptimized$class.foldl(IndexedSeqOptimized.scala:57)
at scala.collection.IndexedSeqOptimized$class.foldLeft(IndexedSeqOptimized.scala:66)
at scala.collection.mutable.ArrayOps$ofRef.foldLeft(ArrayOps.scala:186)
at org.apache.spark.ml.Pipeline.transformSchema(Pipeline.scala:180)
at org.apache.spark.ml.PipelineStage.transformSchema(Pipeline.scala:70)
at org.apache.spark.ml.Pipeline.fit(Pipeline.scala:132)
at com.classifier.classifier_app.App$.<init>(App.scala:91)
at com.classifier.classifier_app.App$.<clinit>(App.scala)
... 1 more

错误。

代码如下:

val input_path = "path//to//file.txt"

case class Sentence(value: String)
val sentencesDS = spark.read.textFile(input_path).as[Sentence]  

val Array(trainingData, testData) = sentencesDS.randomSplit(Array(0.7, 0.3))     

val tokenizer = new Tokenizer()
  .setInputCol("value")
  .setOutputCol("words")

val pipeline = new Pipeline().setStages(Array(tokenizer, regexTokenizer, remover, hashingTF, ovr))
val model = pipeline.fit(trainingData)

我该如何解决这个问题?任何帮助表示赞赏。

我已经定义了管道中的所有阶段,但没有将它们放在代码 sn-p 中。

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    当管道中的执行顺序发生变化时,错误已解决。

    val pipeline = new Pipeline().setStages(Array (indexer, regexTokenizer, remover, hashingTF))
    val model = pipeline.fit(trainingData) 
    

    tokenizer 已替换为 regexTokenizer。

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 2018-03-28
      • 2020-07-13
      • 2017-09-03
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 2018-04-24
      • 2016-11-13
      相关资源
      最近更新 更多