0

我完全是 scala 和 java 的新手,并试图运行示例生产者代码。它所做的只是获取一些存储在 csv 文件中的原始产品和引荐来源网址,并使用 rnd 生成一些随机日志。以下是我的代码:

object LogProducer extends App {
//WebLog config
val wlc = Settings.WebLogGen

val Products = scala.io.Source.fromInputStream(getClass.getResourceAsStream("/products.csv")).getLines().toArray
val Referrers = scala.io.Source.fromInputStream(getClass.getResourceAsStream("/referrers.csv")).getLines().toArray
val Visitors = (0 to wlc.visitors).map("Visitors-" + _)
val Pages = (0 to wlc.pages).map("Pages-" + _)

val rnd = new Random()
val filePath = wlc.filePath

val fw = new FileWriter(filePath, true)

//adding randomness to time increments for demo
val incrementTimeEvery = rnd.nextInt(wlc.records - 1) + 1

var timestamp = System.currentTimeMillis()
var adjustedTimestamp = timestamp

for (iteration <- 1 to wlc.records) {
  adjustedTimestamp = adjustedTimestamp + ((System.currentTimeMillis() - timestamp) * wlc.timeMultiplier)
timestamp = System.currentTimeMillis()
  val action = iteration % (rnd.nextInt(200) + 1) match {
    case 0 => "purchase"
    case 1 => "add_to_cart"
    case _ => "page_view"
  }

  val referrer = Referrers(rnd.nextInt(Referrers.length - 1))
  val prevPage = referrer match {
    case "Internal" => Pages(rnd.nextInt(Pages.length - 1))
    case _ => ""
  }

val visitor = Visitors(rnd.nextInt(Visitors.length - 1))
val page = Pages(rnd.nextInt(Pages.length - 1))
val product = Products(rnd.nextInt(Products.length - 1))

val line = s"$adjustedTimestamp\t$referrer\t$action\t$prevPage\t$visitor\t$page\t$product\n"
fw.write(line)

if (iteration % incrementTimeEvery == 0) {
  //os.flush()
  println(s"Sent $iteration messages!")
  val sleeping = rnd.nextInt(incrementTimeEvery * 60)
  println(s"Sleeping for $sleeping ms")
  }
 }
}

它非常简单,它基本上是生成一些变量并将其添加到行中。

但是,我得到了一个我无法理解的大异常错误堆栈:

"C:\Program Files\Java\jdk1.8.0_92\bin\java...

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:70)
at scala.collection.Iterator.foreach(Iterator.scala:929)
at scala.collection.Iterator.foreach$(Iterator.scala:929)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
at scala.collection.generic.Growable.$plus$plus$eq(Growable.scala:59)
at scala.collection.generic.Growable.$plus$plus$eq$(Growable.scala:50)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
at scala.collection.TraversableOnce.to(TraversableOnce.scala:310)
at scala.collection.TraversableOnce.to$(TraversableOnce.scala:308)
at scala.collection.AbstractIterator.to(Iterator.scala:1417)
at scala.collection.TraversableOnce.toBuffer(TraversableOnce.scala:302)
at scala.collection.TraversableOnce.toBuffer$(TraversableOnce.scala:302)
at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1417)
at scala.collection.TraversableOnce.toArray(TraversableOnce.scala:289)
at scala.collection.TraversableOnce.toArray$(TraversableOnce.scala:283)
at scala.collection.AbstractIterator.toArray(Iterator.scala:1417)
at clickstream.LogProducer$.delayedEndpoint$clickstream$LogProducer$1(logProducer.scala:16)
at clickstream.LogProducer$delayedInit$body.apply(logProducer.scala:12)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at clickstream.LogProducer$.main(logProducer.scala:12)
at clickstream.LogProducer.main(logProducer.scala)

Process finished with exit code 1

有人可以帮我确定异常的含义吗?谢谢大家

4

2 回答 2

0

这是一个编码问题,对于 Scala 来说,一个快速的解决方法是:

代替:

val Products=scala.io.Source.fromInputStream(getClass.getResourceAsStream("/products.csv")).getLines().toArray
val Referrers = scala.io.Source.fromInputStream(getClass.getResourceAsStream("/referrers.csv")).getLines().toArray

使用这个:

val Products=scala.io.Source.fromInputStream(getClass.getResourceAsStream("/products.csv"))("UTF-8").getLines().toArray
val Referrers = scala.io.Source.fromInputStream(getClass.getResourceAsStream("/referrers.csv"))("UTF-8").getLines().toArray

有关 java 和更多详细信息,请查看此链接:http ://biercoff.com/malformedinputexception-input-length-1-exception-solution-for-scala-and-java/

于 2019-03-12T03:29:53.853 回答
0

所以这并不难..这是我的业余知识。这是一个简单的 IO 异常,Intellij 无法从我的 csv 文件中获取值。当我将它导入资源根目录时,它给了我一个错误编码的警告消息。

错误出现在这一点上:

val Products = scala.io.Source.fromInputStream(getClass.getResourceAsStream("/products.csv")).getLines().toArray

感谢您的努力

于 2017-06-05T15:19:00.650 回答