Python操作ES的封装

30 篇文章 12 订阅 ¥49.90 ¥99.00
120 篇文章 26 订阅
4 篇文章 3 订阅
import json
import requests
import time


class RuidGet(object):
    '''
    配置一个唯一的ruid
    props: None
    return:
    '''
    @classmethod
    def get_str_ruid(cls):
        '''
        获取16进制字符串唯一id
        :return:
    
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
利用Python操作Elasticsearch可以使用elasticsearchelasticsearch-dsl这两个库,也可以使用更为简单的es-pandas库。其中,elasticsearch库提供了与Elasticsearch交互的低级接口,而elasticsearch-dsl库则提供了更高级别的查询构建器和对象映射器。es-pandas库则提供了一种将Elasticsearch数据转换为pandas DataFrame的简单方法。 以下是一个使用elasticsearch库进行查询的例子: ```python from elasticsearch import Elasticsearch # 连接到Elasticsearch es = Elasticsearch() # 查询所有文档 res = es.search(index="my_index", body={"query": {"match_all": {}}}) # 输出结果 for hit in res['hits']['hits']: print(hit['_source']) ``` 以下是一个使用elasticsearch-dsl库进行查询的例子: ```python from elasticsearch import Elasticsearch from elasticsearch_dsl import Search # 连接到Elasticsearch es = Elasticsearch() # 创建查询对象 s = Search(using=es, index="my_index").query("match", title="python") # 执行查询并输出结果 response = s.execute() for hit in response: print(hit.title) ``` 以下是一个使用es-pandas库将Elasticsearch数据转换为pandas DataFrame的例子: ```python from es_pandas import es_pandas # 将Elasticsearch数据转换为pandas DataFrame df = es_pandas.DataFrame({ "host": {"field": "host.keyword"}, "response": {"field": "response_time_ms"}, "timestamp": {"field": "@timestamp", "dtype": "datetime64[ns]"} }, es_url="http://localhost:9200", es_index_pattern="my_index-*") # 输出DataFrame print(df.head()) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

什么都干的派森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值