【问题标题】:How to deserialize a protobuf in scala如何在scala中反序列化protobuf
【发布时间】:2020-07-23 21:12:57
【问题描述】:

我在 Python 进程生成的文件中有一个 protobuf 序列化对象。我现在需要在 Scala 中反序列化它。 我在官方 google protobuf 文档中没有看到 scala 文档。有参考吗?我该怎么办呢。

我开始为此研究 ScalaPB,但遇到了错误。任何建议 -

plugins.sbt 文件:`addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.34")

libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.7"`

错误 - [error] (update) sbt.librarymanagement.ResolveException: Error downloading com.thesamet:sbt-protoc;sbtVersion=1.0;scalaVersion=2.12:0.99.34 [error] Not found [error] Not found [error] not found: C:\Users\USER\.ivy2\local\com.thesamet\sbt-protoc\scala_2.12\sbt_1.0\0.99.34\ivys\ivy.xml [error] not found: https://repo1.maven.org/maven2/com/thesamet/sbt-protoc_2.12_1.0/0.99.34/sbt-protoc-0.99.34.pom [error] (ssExtractDependencies) sbt.librarymanagement.ResolveException: Error downloading com.thesamet:sbt-protoc;sbtVersion=1.0;scalaVersion=2.12:0.99.34 [error] Not found [error] Not found [error] not found: C:\Users\USER\.ivy2\local\com.thesamet\sbt-protoc\scala_2.12\sbt_1.0\0.99.34\ivys\ivy.xml [error] not found: https://repo1.maven.org/maven2/com/thesamet/sbt-protoc_2.12_1.0/0.99.34/sbt-protoc-0.99.34.pom [error] Total time: 8 s, completed Jul 23, 2020, 6:41:59 PM [info] shutting down sbt server

【问题讨论】:

    标签: scala protocol-buffers


    【解决方案1】:

    您需要能够访问描述在 python 进程中序列化的对象的消息模式的 proto 文件。文件必须在项目之间共享,这是定义合同的强制性信息。

    然后您可以使用ScalaPB 生成相应的案例类。这些类由 sbt 插件自动生成,它们提供可用于反序列化的辅助方法。检查生成的Message类的伴随对象中提供的方法parseFrom

    【讨论】:

    • 谢谢伊万!我开始使用 ScalaPB,但在插件更新期间出现错误。任何建议。
    • @shukatghouse 我无法重现您的问题。使用最新的 sbt 并按照 scalapb.github.io/sbt-settings.html 上的说明进行操作
    猜你喜欢
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    相关资源
    最近更新 更多