ubl007
作者ubl007·2023-12-04 15:14
私有云高级交付(DTA方向)·百度智能云

sponge --- linux下保持输入输出一致性的数据流传导工具

字数 1399阅读 413评论 0赞 0

sponge 是什么?
根据 man page 上的描述可知,sponge 是一个最晚诞生于2006年的的数据流传导工具,它不同于shell中的重定向和pipe管道,最大的特性在于保持了输入输出内容原子一致性 。

sponge(1) sponge(1)
NAME
sponge - soak up standard input and write to a file
SYNOPSIS
sed '...' file | grep '...' | sponge file
DESCRIPTION
sponge reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before opening the output file. This allows con‐
structing pipelines that read from and write to the same file.
It also creates the output file atomically by renaming a temp file into place, and preserves the permissions of the output file if it already exists. If the output file is
a special file or symlink, the data will be written to it.
If no output file is specified, sponge outputs to stdout.
AUTHOR
Colin Watson and Tollef Fog Heen

sponge能用来干什么?
sponge可以用于抽取数据,典型的应用是Prometheus中传导自定义的指标数据到node_exporter的textfile模块数据中。
还可以在离线数据仓库的构建过程中承担ETL流程中的数据抽取任务,用此种方法可以保证DWD层数据与原始数据 严格一致。(注意:用sponge 抽取非关系型数据库原始数据到DWD层构建数据仓库这个方法本人未进行过TB级别的数据量测试,阿里系和华为系的狂人可能会不认可——甚至怀疑你进行专业知识造假)

如何获取sponge ?
sponge 工具套件在moreutils 套件中,RHEL系列的发行版可以通过添加 EPEL 源的方式进行moreutils 套件安装,参考命令如下:

yum -y install epel-release && yum -y install moreutils

用例参考:

1)在Prometheus 中自定义node_exporter 中执行crontab排程时的应用

          • /root/prom/directory-size.sh /root/prom | sponge /root/prom/textfile/directory_size.prom

2)完整追加新内容

cat 新内容 | sponge -a 目标文件

3)删除以 # 开头的行

grep -v '^#' 被操作文件 | sponge 目标存储文件

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

X社区推广