scala - 如何在scala中反序列化protobuf
问题描述
我在 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
解决方案
您需要能够访问描述在 python 进程中序列化的对象的消息模式的 proto 文件。文件必须在项目之间共享,这是定义合同的强制性信息。
然后,您可以使用ScalaPB生成相应的案例类。这些类由 sbt 插件自动生成,它们提供可用于反序列化的辅助方法。检查parseFrom
生成Message
类的伴随对象中提供的方法
推荐阅读
- azure-logic-apps - Azure Logic App:转发时如何将嵌入图像保留在电子邮件正文中?
- python - RecursionError 限制了我在大数组中的排序功能
- javascript - mapbox:在低缩放比例下避免重复图层
- python - 如何解决 pytest 中的弃用警告
- javascript - 我不明白程序的工作原理
- java - 有没有办法使用 elasticsearch java api 加载包含索引映射的 json 文件?
- kotlin - 为什么我不能在 kotlin 中合并两个列表
- authentication - 无法获取实时环境的 PayPal 访问令牌
- javascript - 我在从 ajax 代码中获取“第 4 行 C:\xampp\htdocs\ERP\std.php 中的类”中的日期时遇到问题
- python-3.x - 如何在python中将int数组转换为字符串