惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

故事的程序猿

先有潭柘寺,后有北京城~ 26年5月总结-咖啡拉花 装修灯具要注意⚠️ 26年4月总结-提前的假期 SpringBoot多PgSQL数据源的一种方式 带着女儿去广州长隆🚕✈️🦒🐼🐒🐨 长春小游 新功能添加:首页友链、画廊...【建站笔记】 26年3月总结-感冒了哦🤧 新手咖啡入坑指南!你真的需要一台咖啡机吗? 周日再来首钢园
使用Protobuf备份数据
猿1993👨🏻‍💻 · 2026-03-10 · via 故事的程序猿

· 阅读需 2 分钟

一个高效的序列化工具来备份数据,Protobuf 是一个不错的选择!

开源地址:https://github.com/protocolbuffers/protobuf

文 档:https://protobuf.dev/

安装protoc工具

首先安装Protobuf的工具,我使用的macbook,可以使用Homebrew安装。

安装完毕后验证:

protoc --version
libprotoc 3.21.5

定义描述文件

我使用的java,springboot工程。首先引入依赖

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>

定义描述文件,例如 Asset.proto

syntax = "proto3";
option java_package = "com.seatone.parse.proto";
option java_outer_classname = "AssetProto";
message Asset {
string id = 1;
string name = 2;
string standardTypeCode = 3;
}

然后使用工具生成对应的实体。

第一个参数为输出目录,第二个参数为描述文件。

protoc --java_out=./src/main/java/ ./src/main/resources/proto/Asset.proto

这样我们就可以利用这个对象来进行序列化和反序列化。

序列化和反序列化

@Test
public void protoToByteArray() throws InvalidProtocolBufferException {
byte[] byteArray = AssetProto.Asset.newBuilder()
.setId("l9L4T1Hps7tlses66AU")
.setName("abc")
.setStandardTypeCode("50101")
.build()
.toByteArray();
System.out.println(Arrays.toString(byteArray));

AssetProto.Asset asset = AssetProto.Asset.parseFrom(byteArray);
System.out.println(asset);
}

写入和读取文件

定义写入文件方法

/**
* 备份方法,流式写入 + GZIP
*/
public static void backupStream(List<?> data, String filePath) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
try (GZIPOutputStream gzip =
new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(Paths.get(filePath))))) {
mapper.writeValue(gzip, data);
}
}

定义还原文件方法

/**
* 还原方法
*/
public static <T> T restore(String filePath, Class<T> type) throws IOException {
ObjectMapper mapper = new ObjectMapper();
try (GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(Paths.get(filePath)))) {
if (type.equals(List.class)) {
JavaType javaType = mapper.getTypeFactory().constructCollectionType(List.class, byte[].class);
return mapper.readValue(gzip, javaType);
} else {
return mapper.readValue(gzip, type);
}
}
}

最终:备份和还原数据

/**
* 备份数据到文件
*/
@Test
public void backupAsset() throws IOException {
List<byte[]> byteList = Lists.newArrayList(AssetProto.Asset.newBuilder()
.setId("l9L4T1Hps7tlses66AU")
.setName("abc")
.setStandardTypeCode("50101")
.build()
.toByteArray()
);
backupStream(byteList, backup_path);
}

/**
* 从文件还原数据
*/
@Test
public void restoreAsset() throws IOException {
List<byte[]> restore = restore(backup_path, List.class);
for (byte[] bytes : restore) {
AssetProto.Asset asset = AssetProto.Asset.parseFrom(bytes);
System.out.println(asset);
}
}

谷歌出品,稳定和效率都值得肯定!

文章标题:使用Protobuf备份数据

版权声明:内容遵守许可协议。转载请注明出处!

侵权提示:部分信息可能来源于网络。如发现有侵权,请随时联系删除!

感谢阅读,如果内容对你有用,请作者喝杯咖啡: