IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 从Elasticsearch索引库中读取数据的操作 -> 正文阅读

[大数据]从Elasticsearch索引库中读取数据的操作

作者:>

从ES索引库中读取数据的代码逻辑如下:

#!/usr/bing/env python
# -*-coding:utf-8-*-
# author:xx
# datetime:21-x-x 下午xx:xx
# software:PyCharm

import os
import sys
import json
import inspect

filename = inspect.getframeinfo(inspect.currentframe()).filename
matrix_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(filename))))
sys.path.insert(0, matrix_dir)

from datetime import datetime
from elasticsearch import Elasticsearch


class ReadData(object):

    def __init__(self):
        # connect es
        self.es_client = Elasticsearch(hosts="localhost:xxxx/")
        # index name (medical)
        self.index_name = "xx-xx-index"
        # index name (public chat)
        self.index_name = "ssk-public-index-1"
        # data path
        self.data_path = os.path.join(matrix_dir, "xxx/xxx/")
        # control
        self.debug = True

    # save data into file
    def save_data(self, data_list):
        # open file
        with open(os.path.join(self.data_path, "xxx_xxx.data"), 'w', encoding="utf-8") as file:
            # traverse the data
            for temp_dict in data_list:
                # transfer into json format
                temp_dict = json.dumps(temp_dict, ensure_ascii=False)
                file.write(temp_dict)
                file.write("\n")

        # prompt
        print("*******save finish*****")

    # read data from es
    def read_es(self):
        # set the container
        result_list = list()
        # 检查index是否存在
        if self.es_client.indices.exists(index=self.index_name):
            page = self.es_client.search(index=self.index_name, scroll="20m", size=200)
            # 游标用于输出es查询出的scroll id
            scroll_id = page['_scroll_id']
            if self.debug == True:
                print("the scroll_id is : %s" %(scroll_id))
                print("=*="*10)

            # es查询出的结果总量
            scroll_size = page['hits']['total']
            if self.debug == True:
                print("the total number is : %d" %(scroll_size))
                print("=*=" * 10)

            # traverse the data
            for source_dict in page['hits']['hits']:
                # get the data info
                temp_dict = source_dict["_source"]
                # add the data into list
                result_list.append(temp_dict)

            while scroll_size > 0:
                page = self.es_client.scroll(scroll_id=scroll_id, scroll="20m")
                scroll_id = page['_scroll_id']
                # get the page size
                scroll_size = len(page['hits']['hits'])
                if self.debug == True:
                    print("the scroll number is : %d" % (scroll_size))
                    print("=*=" * 10)

                # traverse the data
                for source_dict in page['hits']['hits']:
                    # get the data info
                    temp_dict = source_dict["_source"]
                    # add the data into list
                    result_list.append(temp_dict)

            if self.debug == True:
                print("*******read finish start save*******")
                print("=*=" * 10)
            # save data into file
            self.save_data(result_list)

        else:
            print('{} not find in es'.format(self.index_name))
            return None

if __name__ == "__main__":
    # the start_time
    start_time = datetime.now()
    # define the testObject
    dataObject = ReadData()
    # call the function
    dataObject.read_es()
    # the end_time
    end_time = datetime.now()
    use_time = end_time - start_time
    print("run time is:%ss||time:%s" % (use_time.seconds, use_time))

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-08-10 13:29:13  更:2021-08-10 13:30:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 20:15:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码