简体   繁体   English

序列化和数据库存储之间的区别

[英]difference between serialization and database storage

What is difference between serialization and database storage In java? 序列化和数据库存储有什么区别在java? Doesnt serialization actually mean storing data in a database on server? 序列化是否实际意味着将数据存储在服务器上的数据库中?

Let's think of the database like a bowl. 让我们把数据库想象成一个碗。

在此输入图像描述

If you want to keep stuff from going everywhere, you put it in the bowl. 如果你想让东西无处可去,你就把它放在碗里。

Your stuff is the data you want to store. 你的东西是你想要存储的数据。 Right now it's out there, on the table, in a box. 现在它在桌子上,在一个盒子里。 在此输入图像描述

So we're going to take the stuff out of that box. 所以我们要从那个盒子里取出东西。 The problem is, the stuff in our box probably won't fit into the bowl. 问题是,我们盒子里的东西可能不适合放入碗中。 How do we fix that? 我们如何解决这个问题?

We need to change it into the type of object that will fit into our bowl. 我们需要将它改变成适合我们碗的物体类型。 We need to serialize it. 我们需要序列化它。

在此输入图像描述

Our serialized data will fit in the bowl now. 我们的序列化数据现在可以放入碗中。 So we take our serialized data and we pour it into the bowl, and we have the most important meal of the day. 所以我们把我们的序列化数据拿到碗里,我们吃了当天最重要的一餐。

在此输入图像描述

In case this was all really complicated. 如果这一切都非常复杂。 Simplified: to serialize is to change , and a database is a place to store stuff. 简化: 序列化改变数据库存储东西的地方。 Often, you change stuff before you store it. 通常,在存储之前更改内容。

Serialization can be used to prepare an object for database storage - it is a the process of converting an object into a storable or transmittable format, such as a string or a stream of bytes. 序列化可用于为数据库存储准备对象 - 它是将对象转换为可存储或可传输格式的过程,例如字符串或字节流。

We can't store a java object into most normal storage types as-is - but if we for instance serialize it into JSON we can store it. 我们不能将java对象按原样存储到大多数普通存储类型中 - 但是如果我们将其序列化为JSON,我们可以存储它。 We can then retrieve the JSON at a later point from the storage and deserialize it to get back an object the same as our original object, given that the serialization and deserialization is properly implemented. 然后,我们可以在稍后从存储中检索JSON并对其进行反序列化以获取与原始对象相同的对象,前提是已正确实现序列化和反序列化。

Of course, this doesn't have to entail database storage - having the object serialized into a JSON stream for instance also allows us to transmit it over the internet to be deserialized on another computer. 当然,这不一定需要数据库存储 - 例如将对象序列化为JSON流也允许我们通过互联网传输它以在另一台计算机上反序列化。

No. Not at all. 一点都不。 Serialization in Java is an API which generates a storeable version of an object that you can later load back from disk (or wherever you store it) and make it back into an object with ( hopefully! ) the same state as it once had. Java中的序列化是一种API,它可以生成一个对象的可存储版本,以后可以从磁盘(或存储它的任何位置)加载回来,然后将其重新放回到具有( 希望! )状态的对象中。 There are alternatives to it such as Google Protobufs which are better for networked applications, but it is good enough for most simple uses. 有一些替代品,例如Google Protobufs ,它们更适合网络应用程序,但它对于大多数简单用途来说已经足够了。

Serialization is the process of converting a data structure into a form that can be persisted (saved on a hard drive) in any way. 序列化是将数据结构转换为可以以任何方式保存(保存在硬盘驱动器上)的形式的过程。 It can be binary, xml, plain text, html, ... usually the goal is to be able to deserialize, that is restore back the state of your data structure at the time it was persisted. 它可以是二进制,xml,纯文本,html,...通常目标是能够反序列化,即在数据结构持久化时恢复数据结构的状态。

A database is just the place (and not the way ) where you store your data. 数据库只是存储数据的地方 (而不是方式 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM