#!/usr/bin/env python
@desc :
coding = "utf-8"
author = "bytedance"
python
class EsMeta:
def __init__(self, inType, esNodes, esIndex, esType, selectFields):
self.inType = inType
self.esNodes = esNodes
self.esIndex = esIndex
self.esType = esType
self.selectFields = selectFields
# 返回值类型必须是string
def __str__(self):
return f"inType={self.inType},esNodes={self.esNodes},esIndex={self.esIndex}," \
f"esType={self.esType},selectFields={self.selectFields}"
@classmethod
def parse_rule(cls, rule: str):
keyvalue_list = rule.split("##")
return_dict = {}
for keyvalue in keyvalue_list:
info_list = keyvalue.split("=")
return_dict[info_list[0]] = info_list[1]
return EsMeta(**return_dict)
测试代码
使用上面定义的规则,就能在下面一大串有规则的表格数据中提取出读取es上数据所需要的内容
if name == 'main':
rule = "inType=Elasticsearch##esNodes=192.168.88.166:9200##esIndex=policy_client##esType=_doc##selectFields=user_id,sex"
obj = EsMeta.parse_rule(rule)
print(obj)