加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala与Java Streaming:Scala不打印,Java可以工作

发布时间:2020-12-16 08:45:00 所属栏目:安全 来源:网络整理
导读:我正在分别使用 akka-stream和 RxJava对Scala与Java Reactive Spec实现进行比较.我的用例是一个简单的grep:给定一个目录,一个文件过滤器和一个搜索文本,我在该目录中查找具有该文本的所有匹配文件.然后我流式传输(filename – 匹配行)对. 这适用于Java,但对
我正在分别使用 akka-stream和 RxJava对Scala与Java Reactive Spec实现进行比较.我的用例是一个简单的grep:给定一个目录,一个文件过滤器和一个搜索文本,我在该目录中查找具有该文本的所有匹配文件.然后我流式传输(filename – >匹配行)对.
这适用于Java,但对于Scala,没有打印任何内容.没有例外但也没有输出.
测试数据是从互联网上下载的,但正如您所看到的,代码也可以轻松地使用任何本地目录进行测试.

斯卡拉:

object Transformer {
  implicit val system = ActorSystem("transformer")
  implicit val materializer = ActorMaterializer()
  implicit val executionContext: ExecutionContext = {
    implicitly
  }

  import collection.JavaConverters._

  def run(path: String,text: String,fileFilter: String) = {
    Source.fromIterator { () =>
      Files.newDirectoryStream(Paths.get(path),fileFilter).iterator().asScala
    }.map(p => {
      val lines = io.Source.fromFile(p.toFile).getLines().filter(_.contains(text)).map(_.trim).to[ImmutableList]
      (p,lines)
    })
      .runWith(Sink.foreach(e => println(s"${e._1} -> ${e._2}")))
  }
}

Java的:

public class Transformer {
    public static void run(String path,String text,String fileFilter) {
        Observable.from(files(path,fileFilter)).flatMap(p -> {
            try {
                return Observable.from((Iterable<Map.Entry<String,List<String>>>) Files.lines(p)
                        .filter(line -> line.contains(text))
                        .map(String::trim)
                        .collect(collectingAndThen(groupingBy(pp -> p.toAbsolutePath().toString()),Map::entrySet)));
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }).toBlocking().forEach(e -> System.out.printf("%s -> %s.%n",e.getKey(),e.getValue()));
    }

    private static Iterable<Path> files(String path,String fileFilter) {
        try {
            return Files.newDirectoryStream(Paths.get(path),fileFilter);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}

使用Scala测试进行单元测试:

class TransformerSpec extends FlatSpec with Matchers {
  "Transformer" should "extract temperature" in {
    Transformer.run(NoaaClient.currentConditionsPath(),"temp_f","*.xml")
  }

  "Java Transformer" should "extract temperature" in {
    JavaTransformer.run(JavaNoaaClient.currentConditionsPath(false),"*.xml")
  }
}

解决方法

Dang,我忘了Source返回Future,这意味着流程从未运行过. @MrWiggles的评论给了我一个提示.以下Scala代码生成与Java版本相同的结果.

注意:我的问题中的代码没有关闭DirectoryStream,对于包含大量文件的目录,导致java.io.IOException:系统中打开的文件过多.下面的代码正确关闭了资源.

def run(path: String,fileFilter: String) = {
  val files = Files.newDirectoryStream(Paths.get(path),fileFilter)

  val future = Source(files.asScala.toList).map(p => {
    val lines = io.Source.fromFile(p.toFile).getLines().filter(_.contains(text)).map(_.trim).to[ImmutableList]
    (p,lines)
    })
    .filter(!_._2.isEmpty)
    .runWith(Sink.foreach(e => println(s"${e._1} -> ${e._2}")))

  Await.result(future,10.seconds)

  files.close

  true // for testing
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读