【问题标题】:How to insert an already created json-format string to Elasticsearch Bulk如何将已创建的 json 格式字符串插入 Elasticsearch Bulk
【发布时间】:2018-01-26 14:31:31
【问题描述】:

在 python 脚本中,

我正在尝试 elasticsearch.helpers.bulk 存储多条记录。

我会从另一个软件得到一个json格式的字符串,我想把它附加在源代码部分

我通过 answer 获得了 helpers.bulk 格式

我的部分代码:

def saveES(output,name):
    es = Elasticsearch([{'host':'localhost','port':9200}]) 
    output = output.split('\n')
    i=0
    datas=[]
    while i<len(output):
            data = {
                    "_index":"name",
                    "_type":"typed",
                    "_id":saveES.counter,
                    "_source":[[PROBLEM]]
            }
            i+=1
            saveES.counter+=1
            datas.append(data)

    helpers.bulk(es, datas)

我想在 [[PROBLEM]] 中附加一个 json 格式的字符串

我怎样才能把它贴进去?我已经很努力了,但是输出不正确..

如果我使用:

"_source":{
"image_name":'"'+name+'",'+output[i]
}

打印数据结果为:

{'_type': 'typed', '_id': 0, '_source': {'image_name': '"nginx","features": "os,disk,package", "emit_shortname": "f0b03efe94ec", "timestamp": "2017-08-18T17:25:46+0900", "docker_image_tag": "latest"'}, '_index': 'name'}

这个结果表明组合成一个字符串。

但我希望:

{'_type': 'typed', '_id': 0, '_source': {'image_name': 'nginx','features': 'os,disk,package', 'emit_shortname': 'f0b03efe94ec', 'timestamp': '2017-08-18T17:25:46+0900', 'docker_image_tag': 'latest'}, '_index': 'name'}

【问题讨论】:

    标签: python elasticsearch elasticsearch-bulk-api elasticsearch-bulk


    【解决方案1】:

    你的代码有很多问题。

    1. 您在循环中覆盖 data 的值
    2. 你不尊重任何规范(Pesp8 之类的)
    3. 你是 while 而不是一个理解列表
    4. 你创建了 2 个无用的变量
    5. 你在你的函数中实例化你的 es

    这是改进后的代码

    es = Elasticsearch([{'host':'localhost','port':9200}]) # You don't have to initialise this variable every time you are calling the function but only once.
    
    
    def save_es(output,es):  # Peps8 convention
        output = output.split('\n') # you don't need a while loop. A comprehension loop will avoid a lot of trouble
        data = [    # Please without s in data
           {
              "_index": "name",
              "_type": "typed",
              "_id": index,
              "_source": {
                  "image_name":"name" + name}
            }
            for index, name in enumerate(output)
        ]    
        helpers.bulk(es, data)
    
    save_es(output, es)
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢,但我已经尝试过了。如果我使用 "image_name":"name"+output[i] 代码,那么数据是 {'_type': 'typed', '_id': 3, '_source': { 'image_name': '"nginx","feat": "foo", "os": 'foo''}, '_index': 'name'}。但是,正确的数据是 {'_type': 'typed', '_id': 3, '_source': {'image_name': 'nginx', 'feat': 'foo', 'os': 'foo '}, '_index': '名称'}
    • 好吧,您的“正确”结果甚至不是 JSON 文档,存在昏迷问题。此外,在您的代码和我的代码之后,我应该在您的 JSON 中看到类似 'image_name':'name' 的内容,但没有一个。
    • 对不起。实际结果很长,所以我做了一个简单的更正,似乎出现了一个错字。简单来说, "image_name": "name1" + name2 在这种情况下,name1 和 name2 合并为一个字符串。您的代码中的“名称”值是字符串吗?
    • namevalue 是您的输出中存在的内容,所以我猜是一个字符串
    • 是的,name 是字符串.. 哦.. 我觉得我的英语水平不够,所以沟通似乎很困难... 两个字符串合并为一个字符串,即所有数据都以字符串形式存储在image_name中。我不知道我是否正确交付了......你使用TeamViewer吗?我想以演示模式显示执行结果屏幕。
    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 2020-02-08
    • 2021-11-20
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多