Elasticsearch as Database

1 需求

服务使用容器化部署,需要增加虚机管理视图,统计资源分配、库存等数据

2 实践

2.1 表结构定义

Elasticsearch 的 mapping(相当于关系型数据库的表结构定义)

index 相当于表('index name must be lowercase')

由于ElasticSearch使用索引名称作为磁盘上的目录名称,这些名称必须符合不同操作系统的约定。
同时需遵守下列规则:

索引名必须为小写字母
不能包括 \, /, *, ?, “, <, >, |, 空格, 逗号, #
7.0版本之后不能使用冒号:
不能以这些字符 -, _, + 开头
不能包括 . 或 …
长度不能超过 255 个字符
host_doc = {}
...
host_doc["free_cpu_in_core"] = host_data["freeCpuInCore"]
host_doc["free_mem_size_in_mb"] = host_data["freeMemSizeInMB"]
host_doc["timestamp"] = datetime.now()
 
index = "xxxx"
es_id = "xxxx"
es_res = es.index(index=index.lower(), id=es_id, document=host_doc)

2.2 过滤和排序

传送门

Last updated