write as工具 writeas作品集

什么是时序数据库

时序数据库,全称时间序列数据库(Time Series Database,TSDB),用于存储大量基于时间的数据,时序数据(Time Series Data)指的是一系列基于时间的数据,例如CPU利用率,北京的房价变化趋势,某一地区的温度变化等。

时序数据库支持时序数据的快速写入、持久化,多维度查询、聚合等操作,同时可以记录所有的历史数据,查询时将时间作为数据的过滤条件。

时序数据的使用场景广泛,包括DevOps监控,应用程序指标,IoT传感器数据,实时动态数据分析等场景。

1

初识InfluxDB

InfluxDB是时序数据库中应用比较广泛的一种,在DB-Engines TSDB rank中位居首位,可见InfluxDB在互联网的受欢迎程度是非常高的。下图是截止到2021年8月时序数据库的排名情况。

它是go语言开发的数据库,InfluxDB自发布至今,已经有两个版本,InfluxDB1.x系列提供一种类似SQL的查询语言InfluxQL,用于数据交互。2019年1月新推出的influxDB2.0 alpha版本,主推全新的查询语言Flux,支持TICK架构。在 2020 年底推出InfluxDB 2.0 正式版本,该版本又分为InfluxDB Cloud 和 InfluxDB OSS两个系列。

时序数据库与我们熟悉的关系型数据库有所不同,首先需要了解一下InfluxDB中字段的含义,如下图所示:

2

TICK架构分析与各组件功能介绍

TICK架构 是 InfluxData 平台的组件的集合首字母缩写,该集合包括Telegraf、InfluxDB、Chronograf和 Kapacitor。TICK架构以及各组件分工情况如图所示:

除了上图可视化管理工具Chronograf外,还有一种可视化工具Grafana,它也是用于大规模指标数据的可视化展示,提供包括折线图,饼图,仪表盘等多种监控数据可视化UI,若应用过程中考虑到扩展性问题,也会使用Grafana代替Chronograf。

3

InfluxDB的特点

● 数据写入:

①. 高并发高吞吐,可持续的数据写入。

②. 写多读少,时序数据95%以上都是写操作,例如在监控系统数据的时候,监控数据特别多,但是通常只会关注几个关键指标。

③. 数据实时写入,不支持数据更新,但是可以人为更新修改。

● 数据分析与查询:

①. 数据查询是按照时间段读取,例如1小时,1分钟,给出具体时间范围。

②. 最近的数据读取率高,越旧的数据读取率越低。

③. 多种精度查询和多种维度分析。

● 数据存储:

①. 存储数据规模大的数据,监控数据的数据大多数情况下都是TB或者PB级。

②. 数据存放具有时效性,InfluxDB提供了保存策略,可以认为是数据的保质期,超过周期范围,就可以认为数据失效,需要回收。节约存储成本,清理低价值的数据。

4

InfluxDB存储原理

InfluxDB的存储结构树是时间结构合并树(Time-Structured Merge Tree,TSM),它是由日志结构化合并树(Log-Structured Merge Tree,LSM),根据实际需求变化而来的。

**①. LSM树 **

LSM树包含叁部分:Memtable,Immutable和SSTable。MemTable是内存中的数据结构,用于保存最近产生的数据,并按照Key有序地组织数据。内存并不是可靠存储,若断电就会丢失数据,因此通常会使用预写式日志(Write-ahead logging,WAL)的方式来保证数据的可靠性。

②. TSM存储引擎

TSM存储引擎主要包括四部分:Cache,WAL,TSM File,Compactor。下图中shard与TSM引擎主要部分放在一起,但其实shard在是TSM存储引擎之上的一个概念。在 InfluxDB 中按照数据产生的时间范围,会创建不同的shard分组,每个 shard 都有本身的 cache、wal、tsm file 以及 compactor。

InfluxDB 2.0应用实践

准备工作

安装虚拟机,创建Ubuntu环境,安装Jmeter5.4版本。

1. 安装influxdb2.0

wget https://dl.influxdata.com/influxdb/releases/influxdb2-2.0.7-amd64.deb sudo dpkg -i influxdb2-2.0.7-amd64.deb

③. 启动influxdb服务

sudo service influxdb start

④. 关闭influxdb服务

sudo service influxdb stop sudo service influxdb status

⑥. 卸载influxdb

sudo dbkg –r influxdb2

2. 安装telegraf

wget https://dl.influxdata.com/telegraf/releases/telegraf_1.19.0~rc1-1_amd64.deb

②. 安装telegraf

sudo dpkg -i telegraf_1.19.0~rc1-1_amd64.deb

③. 启动telegraf

systemctl start telegraf systemctl status telegraf

应用实践

1. Telegraf+InfluxDB2.x系统可视化监控

Influxdb2.0 已经集成了图形界面,简单的可视化监控可以无需使用 Grafana,本文例子暂时没有使用Grafana。

①. 访问influxdb2

首先在终端键入启动influxdb服务的指令,然后在虚拟机的火狐浏览器地址栏输入 http://localhost:8086 1,点击回车键进入influxDB浏览器访问首页。首次登录会设置用户名,密码,点击sign in完成登录。

②. 创建bucket,存储数据

依次点击 Data → Bucket → Create Bucket,在弹框中输入bucket名字,然后点击创建,完成bucket的创建,此处创建了一个name为demo_bucket的 bucket。

③. 创建一个telegraf收集数据

依次点击Data → Telegraf → Create Configuration → continue。在Bucket选择刚才创建的 “demo_bucket“ ,监控目标选择系统System,然后点击continue。

点击 continue 之后,可以知道要系统数据包括哪些,Telegraf Configuration name需要填充,此处填写为system_data,然后点击create and verity,完成这一操作。

④. 使用Telegraf搜集数据

点击 create and verity 之后,并不能马上实现数据搜集,需要在当前页面获取API Token和Telegraf的启动指令,依次复制,并在终端执行指令。

⑤. 系统数据可视化呈现与保存

点击Export,在From这里选择刚才创建的bucket:demo_bucket,在第一个filter那里会在选择bucket之后,默认出现_measurement,选择cpu,相当于关系型数据库中的表,在第2个Filter处选择可视化的字段。例子选择的是usage_system(系统用量百分比),相关的Filter选定之后,点击submit,就会呈现出可视化的结果,这里默认的是Graph,出现如图所示的曲线图。在图中黄色线框部分可以选择动态页面刷新时间,在绿色线框部分可以选择数据开始的时间。

点击Save会保存刚才的图,在选择target dashboard,可以选择已经存在的system ,这里将可视化图命名为usage_system_graph。

2. Jmeter+InfluxDB2.0搭建性能监控环境

Jmeter相对于InfluxDB来说,属于外部系统,所以需要在InfluxDB中生成一个token用于外部时序数据写入数据库。

在InfluxDB中选择 Data → Tokens, 点击 Generate → Read/Write Token ,完成token创建。

通过双击jmeter.bat进入Jmeter工作界面,创建一个线程组(Thread Group),在线程组中点选Loop Count Infinite,然后依次添加Java请求(Java Request),查看结果树(View Results Tree),汇总报告(Summary Report),后端监听器(BackendListener)。主要是在后端监听器填写相关参数,influxdbUrl要修改host,填写创建的组织org和bucket,influxdbToken就是上面创建的Jmeter_Token复制进去。若可以在influxdb中看到Java请求的数据结果,说明Jmeter与InfluxDB连接成功。

3. Flux在python3中的查询应用

Flux查询语言是InfluxDB2.0主推的查询语言,提供FluxTable,CSV,DataFrame和Raw Data四种查询API,但是在时间和日期上,较InfluxQL相对局限,仅支持RFC3339格式,默认值是当天零点。

下图中红色框图则是曲线图的Flux查询语句,from表示数据源所在的bucket,|>表示管道连接符,range表示所查询数据所在的时间范围,其中 v.timeRangeStart 和 v.timeRangeStop 代表时间区间下拉框选中的时间段,filter是对range范围内的数据进行过滤,filter中的参数fn,是基于列和属性过滤数据逻辑的匿名函数,yield只在同一个Flux中出现多查询的时候才会出现,yield函数将过滤后的表作为Flux查询结果输出。

①. Flux语言实现系统数据的查询

首先通过InfluxDBClient连接InfluxDB数据库,InfluxDBClient中需要提供url,token的作用是保证外部系统可以访问数据,不同的bucket有不同的token,org是数据存储所在的组织,在首次登录的时候完成创建。

import pandas as pd from influxdb_client import InfluxDBClient # 设置 my_token = "PePwz1xFzM_edpm6NB0DyR2B04XWqDNQEFPmp9i8hxVW8DmlTTSzywrTyh_p5uv_k1h0Qdxy3U99J2S7TV9X7A==" client = InfluxDBClient(url='http://192.168.79.147:8086', token=my_token, org='org_demo') query_api = client.query_api() # Flux查询语句 mem_query = ''' from(bucket: "demo_bucket") |> range(start: -5w, stop: now()) |> filter(fn: (r) => r["_measurement"] == "mem") |> filter(fn: (r) => r["_field"] == "available") |> filter(fn: (r) => r["host"] == "ubuntu") |> yield(name: "mean") ''' table = query_api.query_data_frame(mem_query, "org_demo") # 提取查询结果的部分字段值 mem_example = pd.DataFrame(table, columns=['_start', '_value', '_field', 'host']) print(mem_example.head(5)) client.close()

②. Flux语言实现数据插入

from influxdb_client import InfluxDBClient, Point, Dialect from influxdb_client.client.write_api import SYNCHRONOUS my_token="ENL3dUfGzTBFGcHzJ8iCIfbKF0fF7C7-P5PDkGpDWLzvvHuP2v9tKVgeZAFqV3y8sLXJt8alK0e-jicHVDgOEg==" client = InfluxDBClient(url='http://192.168.79.147:8086', token=my_token, org='org_demo') write_api = client.write_api(write_options=SYNCHRONOUS) """ 数据准备 """ _point1 = Point("_measurement").tag("location", "Beijing").field("temperature", 36.0) _point2 = Point("_measurement").tag("location", "Shanghai").field("temperature", 32.0) write_api.write(bucket="python_bucket", record=[_point1, _point2])

结果:

总结

从DB-Engines数据库趋势排行榜中可以看出,时序型数据库是数据库市场中份额增长最快的部分,尤其是在大数据的背景下,随时都在产生海量的时序数据,现在更多的企业会通过时序数据存储和数据分析来获得预测能力和实时决策能力,为客户提供更好的使用体验。

更多学习资料戳下方!!!

正文翻译


Background_Space3668
Just to encourage anyone struggling with the first book maybe thinking "alright I guess this is cool but why is it hyped so much?". I get it. The first book is odd, but ultimately fine. The last 50 pages are super cool but there's a huge slog in the middle, at least for me.
The second and third however, are maybe the best sci-fi I've ever read. The ideas are incredible and so interesting. One of those books where after finishing I just sat in my chair for half an hour and continued to think about it, doing nothing.
Also each time they introduced an idea and I thought "hm ok but what about X?" thinking I'd found a plothole, usually a page or two later he would write "As for X,..." it's crazy good.
Also, not spoiling anything I think, but just to whet your appetite, the proposed solution to the Fermi paradox is....I mean. It makes sense and is laid out so simply. I'll leave it at that.
Bite the bullet, read the trilogy if you like sci-fi.

只是为了鼓励那些对第一部感到纠结的人,也许会想:“好吧,我觉得这很酷,但是为什么它被炒得这么热?”我可以理解。第一部是很奇怪,但结尾还是不错的。最后的50页非常酷,但是中间的部分挺费劲,至少对我来说是这样。
然而,第二部和第叁部可能是我读过的最好的科幻小说。这些点子让人难以置信,非常有趣。其中一本我读完之后,我只是坐在椅子上半个小时,继续思考,什么也不做。
此外,每次他们拿出一个点子时,我都会想:“呃,好吧,但是 X 呢?”我还以为我发现了一个情节漏洞,通常一两页之后,他就会写道:“至于 X,......”这简直太棒了。还有,我不是想剧透什么,只是为了吊起你的胃口,费米悖论的解决方桉是... 我认为这很有道理,而且简单明了。我就不多说了。如果你喜欢科幻小说,就咬咬牙读完叁部曲吧。

评论翻译

jotomatoes -> Background_Space3668
Ahh thank you, seems like for various reasons gettting through book one is a bit of a challange but it sounds like the pay off is worth the journey.
I personally love science fiction genere, especially ones that tackle big idea themes. And since several people have mentioned and recommened this series to me, I might just power through the first part and see how I'm feeling about The Dark Forest.

啊,谢谢你,看来由于种种原因,读完第一部是个不小的挑战,但听起来付出是值得的。
我个人非常喜欢科幻小说,尤其是那些探讨大主题的科幻小说。既然有几个人向我提到并推荐过这个系列,我可能会先看完第一部,然后再看看我对《黑暗森林》的感觉如何。

jaydfox -> Background_Space3668
3 body was cool but the 2nd book The Dark Forest is the best one.
I've read and re-read the fist book, and I've read and partially re-read the second book. Once I finish re-reading the second book, I plan to read the third (already bought a copy). I'm having a hard time picturing where the series goes from here, but that comment is quite the recommendation, lol!

叁体很酷,但第二部《黑暗森林》是最好的一部。
我已经读完并重读了第一部,第二部我也读完并部分重读了。重读完第二部,我打算读第叁部(已经买了一本)。 我很难想象这个系列以后会怎么发展,但这个评论很有说服力,哈哈!

thepotplant
It is the single most grindingly depressing and bleak book I have ever read.

这是我读过的最压抑、最凄凉的一本书。

davtruss -> thepotplant
During the pandemic, I read several dozen books of different series about the Roman Legions, and scouts, and reconnaissance, and such, and one thing that jumped out at me in connection with the 3 body trilogy is that you can't build a fire for warmth, or hot food, or light, if somebody who sees the fire is determined to do you harm.

在疫情期间,我读了几十本关于罗马军团、侦察军、侦察等等的不同系列的书,而突然浮现在我脑海与《叁体》叁部曲有关的一件事是,你不能生火取暖、吃熟食或者照明,如果有人看到火光就会伤害你。
原创翻译:龙腾网 https://www.ltaaa.cn 转载请注明出处

SimbaOnSteroids -> davtruss
The bleak part is that it makes a lot of sense for our universe to operate under the same principle, as it’s essentially just extending how terrestrial ecosystems work to the universe at large.

令人沮丧的是,我们的宇宙在同样的原则下运行是非常合理的,因为它本质上只是将陆地生态系统的运行方式延伸到了整个宇宙。

SimbaOnSteroids -> SimbaOnSteroids
My favorite solution to the Fermi Paradox is that there is no paradox.
Because otherwise Dark Forest or some other filter is ahead of us.

我最喜欢的费米悖论解决方桉是“根本不存在悖论”。
否则,"黑暗森林 "或其他过滤器就会出现在我们面前。

ptownBlazers -> thepotplant
So bleak. Love it but bleak

太凄凉了。喜欢,但很凄凉

Rrraou -> thepotplant
It is the single most grindingly depressing and bleak book I have ever read.
And I loved it.

这是我读过的最压抑、最凄凉的一本书。但我很喜欢。

Billofrights_boris
The Dark Forest is honestly THE best book I’ve ever read

老实说,《黑暗森林》是我读过的最好的书

ComfyPillowThief -> Billofrights_boris
I don't know if those books are "the best books" for me, I tend to agree with people saying those book are build around ideas and not around good storytelling. But those ideas...all those ideas are making the books instant science fiction classics for me. I can't imagine those books not becoming reference material for sci-fi literature and movies for many years to come. You could create many universes full of rich and interesting lore build around those ideas and concepts.

我不知道叁体对我来说是否是“最好的书”,我倾向于同意人们所说的叁体是围绕着点子建立的,而不是围绕着好的故事。但是这些点子... 所有这些点子让叁体立刻成为我的科幻经典。我可以想象叁体在未来的许多年里会成为科幻文学和电影的参考资料。你可以围绕这些点子和概念创造许多充满丰富而有趣的知识的宇宙。

QueensOfTheBronzeAge -> ComfyPillowThief
My main problem with The Three Body Problem is that the characters felt less like people and more like arbitrary tools to drive the plot forward.
A prime example is when the main character, Wang, is meeting with Ding Yi, the husband of a recently deceased character, Yang Dong. Ding asks if the Wang can just randomly visit his mother-in-law to check on her, because she is probably upset with the death of her daughter. Then the main character just does it, no questions asked, resulting in the plot moving forward.
Does the rest of the trilogy get better about this? Because stuff like that really took me out of it. The plot is so cool, but the characters just don’t feel like people.

叁体给我的主要问题是,角色感觉不像人,更像是推动情节发展的随意使用的工具。
一个典型的例子是,主人公汪淼在与刚去世的杨东的丈夫丁仪见面。丁仪问汪淼是否可以随便去看望一下他的岳母,因为她可能正在为女儿的死而难过。然后,男主角就这样做了,没有问任何问题,结果剧情就向前推进了。
叁部曲的其他部在这方面会有所改进吗?因为这样的情况真的让我不舒服。情节太酷了,但角色感觉不像人。

roof_pizza -> QueensOfTheBronzeAge
Yeah, this is quite the opposite of what it sounds like you enjoy. The scientific concepts are center stage, and the characters are just vehicles to connect the plot to the science. (They're GREAT books and I loved them despite this, but if you can't put aside the fact that the characters are literally paper cut-outs, you might not enjoy)

是的,这和你喜欢的听起来恰恰相反。科学概念是中心,人物只是连接情节和科学的载体。(尽管如此,这两本书还是很棒,我很喜欢,但如果你不能抛开书中的人物都是剪纸这一事实,你可能就不会喜欢)。
原创翻译:龙腾网 https://www.ltaaa.cn 转载请注明出处

elfin_makenzie
The strategy-philosophy of Dark Forest was so absolutely unique

黑暗森林的战略哲学绝对独一无二

grgext -> elfin_makenzie
Changed my perspective on the universe for sure, probably in my list for top 10 books of all time.
I try to get my friends to read it, but with the caveat they need to read the first book for it to make sense.

这本书无疑改变了我对宇宙的看法,可能是我心目中的十大好书之一。
我试着让我的朋友们读这本书,但前提是他们必须读过第一本书才能理解。

jaydfox -> elfin_makenzie
After about the first third of the book, the story got seriously depressing. Like, existential dread and betrayal depressing.
I absolutely loved the book.

大约读了前叁分之一,故事就变得非常压抑。 比如,生存的恐惧和背叛的压抑。
我非常喜欢这本书。

Drop_Release -> jaydfox
Jesus christ yes - was absolutely blown away by Three Body Problem, and then the rest of the trilogy was even wilder
I must say Dark Forrest and Three Body Problem really made me rethink how I view quite a few scifi tropes and the wishful work of the SETI program

完全如此,叁体让我彻底大开眼界了,叁部曲的其余部分就更狂野了。
我必须说,《黑暗森林》和《叁体》确实让我重新思考了我对许多科幻小说的看法,以及搜寻地外文明计划的一厢情愿。