将批量数据加载到Elasticsearch时出错

时间:2016-07-04 09:13:50

标签: python json pandas elasticsearch pyelasticsearch

我在python中使用Elasticsearch。我在pandas框架中有数据(3列),然后我添加了两列_index和_type,并使用pandas内置方法将数据转换为每个记录的json。

data =  data.to_json(orient='records') 

这是我的数据,

[{"op_key":99140046678,"employee_key":991400459,"Revenue Results":6625.76480192,"_index":"revenueindex","_type":"revenuetype"},     
 {"op_key":99140045489,"employee_key":9914004258,"Revenue Results":6691.05435536,"_index":"revenueindex","_type":"revenuetype"},
......
}]

我的映射是:

user_mapping =  {
        "settings" : {
            "number_of_shards": 3,
            "number_of_replicas": 2
        },

        'mappings': {
            'revenuetype': {
                'properties': {
                    'op_key':{'type':'string'},
                    'employee_key':{'type':'string'},
                    'Revenue Results':{'type':'float','index':'not_analyzed'},
                }
            }
        }
    }

然后在使用helpers.bulk(es,data)时遇到此错误:

    Traceback (most recent call last):
      File "/Users/adaggula/Documents/workspace/ElSearchPython/sample.py", line 59, in <module>
        res = helpers.bulk(client,data)
      File "/Users/adaggula/workspace/python/pve/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 188, in bulk
        for ok, item in streaming_bulk(client, actions, **kwargs):
      File "/Users/adaggula/workspace/python/pve/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 160, in streaming_bulk
        for result in _process_bulk_chunk(client, bulk_actions, raise_on_exception, raise_on_error, **kwargs):
      File "/Users/adaggula/workspace/python/pve/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 89, in _process_bulk_chunk
        raise e
    elasticsearch.exceptions.RequestError: TransportError(400, u'action_request_validation_exception', u'Validation Failed: 1: index is
 missing;2: type is missing;3: index is missing;4: type is missing;5: index is 
missing;6: ....... type is missing;999: index is missing;1000: type is missing;')

看起来每个json对象都缺少索引和类型。怎么克服这个?

1 个答案:

答案 0 :(得分:0)

Pandas数据框到json转换是解决问题的技巧。

data =  data.to_json(orient='records')
data= json.loads(data)