将 Protobuf 消息持久化到数据库中

huangapple go评论104阅读模式
英文:

Persisting Protobuf messages to Database

问题

使用protobuf3定义的数据的正确持久化方式是什么?我正在使用golang和Java,这两个地方都支持ORM。在Java中使用Hibernate,在golang中使用gorm。在这两个地方,我都需要将生成的代码转换为相应的实体模型。我觉得为了让ORM能够理解,维护相同的对象结构非常痛苦。是否有任何数据库可以直接使用protobuf对象,或者我可以在protobuf本身中定义对象之间的关系?

非常感谢任何帮助。

英文:

What is the right way to persist data defined using protobuf3. I am using golang and Java, both place with support of ORMs. In java with Hibernate and golang with gorm. Both place i need to convert the Generated code to corresponding Entity model. I feel that is more pain full to maintain same object structure in order to be understandable by ORM. Is there any Database which i can use along with protobuf objects as is. Or i can define the relations between objects in the protobuf itself.

Any helps really appreciated.

答案1

得分: 3

这个问题有一个不太直接的解决方案。

Protobuf 3为消息标准化了JSON映射。一旦你将消息序列化为JSON,你就有多种选项可以将其存储在数据库中。

以下是一些可以存储JSON数据的数据库(还有很多其他的):

  • MariaDB

  • PostgreSQL

  • MongoDB

英文:

There is a not-straightforward solution to this problem.

Protobuf 3 standardises JSON mapping for the messages. Once you serialise your message to JSON, you have multiple options for storing it in a database.

The following (and many more) databases can store JSON data:

  • MariaDB

  • PostgreSQL

  • MongoDB

答案2

得分: 2

你的ORM处理的是对象,根据定义。它不应该知道或关心网络上的序列化。我建议将protobuf消息反序列化为ORM所使用的对象,并让ORM将它们持久化。将持久化层与网络协议耦合没有什么好处。

如果你放弃JPA并采用基于文档的解决方案,直接存储protobuf序列化可能是有意义的。

你必须决定JPA为你提供了多少价值。

英文:

Your ORM is dealing with objects, by definition. It should not know or care about serialization on the network. I'd suggest deserializing the protobuf message into objects that your ORM is used to and letting it persist them. There's no good reason to couple your persistence tier to the network protocol.

It might make sense to store the protobuf serialization directly if you get rid of JPA and go with a document based solution.

You have to decide how much value JPA is providing for you.

答案3

得分: 0

尽管这个问题很旧,但是自那时以来发生了一些事情,2018年苹果发布的FoundationDB Record Layer可以原生地存储Protocol Buffer。

英文:

Although this question is quite old, things have happened since then and the FoundationDB Record Layer, released by Apple in 2018, stores Protocol Buffer natively.

答案4

得分: 0

在Go语言中,我不了解gorm,但是似乎使用Ent(一种竞争的ORM)可以将Protobuf反序列化为与用于数据库表/关系的相同对象。Ent的官方教程中有相关内容。

需要注意的是,你需要使用Ent的Golang结构来指定你的Protobuf,而不是使用标准的proto3语言。

英文:

In Go, I don't know about gorm, but it seems that with Ent (a competing ORM) Protobufs can be deserialized into exactly the same objects which are used for DB tables/relations. Ent's official tutorial for that.

The caveat is that you specify your Protobuf with Ent's Golang structures, not via the standard proto3 language.

huangapple
  • 本文由 发表于 2017年8月13日 17:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/45659153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定