国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Elasticsearch】使用Python完成對(duì)ES的插入操作

這篇具有很好參考價(jià)值的文章主要介紹了【Elasticsearch】使用Python完成對(duì)ES的插入操作。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

實(shí)現(xiàn)思路

1.Python搭建Flask服務(wù),編寫ES腳本。
2.通過Java調(diào)用Python接口,完成對(duì)ES的插入操作。

環(huán)境配置

Elasticsearch 7.16.0

具體代碼實(shí)現(xiàn)

ESObject模板

import json
from flask import Flask, request, jsonify, Response
import jieba
import time
import hashlib
import random
import string
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk

server = Flask(__name__)
server.config["JSON_AS_ASCII"] = False


class ESObject:
    def __init__(self, index_name, index_type, host='127.0.0.1', port=9300):
        self.host = host
        self.port = port
        self.index_name = index_name
        self.index_type = index_type

        self.es_obj = self.connect_elasticsearch()

    def set_index_name(self, index_name):
        self.index_name = index_name

    def set_index_type(self, index_type):
        self.index_type = index_type

    def connect_elasticsearch(self):
        """
        創(chuàng)建連接
        :return:
        """
        _es = None
        _es = Elasticsearch([{'host': self.host, 'port': self.port}], request_timeout=60, max_retries=3,
                            retry_on_timeout=True)
        if _es.ping():
            print('The connection is successful!')
        else:
            print("Error: ES could not connect!")
        return _es

    def create_index(self, settings):
        """
        創(chuàng)建索引(數(shù)據(jù)庫(kù))
        訪問“http://localhost:9200/entities/_mappings”驗(yàn)證創(chuàng)建是否成功
        :return:
        """
        created = False
        try:
            if not self.es_obj.indices.exists(self.index_name):
                # 參數(shù)ignore = 400在檢查后不再需要,因?yàn)檫@可以防止錯(cuò)誤地覆蓋現(xiàn)有索引
                self.es_obj.indices.create(index=self.index_name, ignore=400, body=settings)
                print("Created Index")
            created = True
        except Exception as ex:
            print(str(ex))
        finally:
            return created

    def delete_index(self):
        try:
            if self.es_obj.indices.exists(self.index_name):
                # 參數(shù)ignore 用來忽略 Index 不存在而刪除失敗導(dǎo)致程序中斷的問題
                self.es_obj.indices.delete(index=self.index_name, ignore=[400, 404])
                print("Deleted Index")
        except Exception as ex:
            print(str(ex))

    def store_record(self, record):
        try:
            outcome = self.es_obj.index(index=self.index_name, doc_type=self.index_type, body=record)
            print(outcome['result'])
            return outcome
        except Exception as ex:
            print("Error in indexing data")
            print(str(ex))

    def store_record_list(self, record_list):
        for record in record_list:
            self.store_record(record)

    def bulk_index_data(self, record_list):
        """
        批量插入
        :param record_list:
        :return:
        """
        ACTIONS = []
        i = 1
        for record in record_list:
            action = {
                "_index": self.index_name,
                "_type": self.index_type,
                # "_id": i,  # _id 可以默認(rèn)生成,不賦值
                "_source": record
            }
            i += 1
            ACTIONS.append(action)
        success, _ = bulk(self.es_obj, ACTIONS, index=self.index_name, raise_on_error=True)
        print('Performed %d actions' % success)

    def get_data_by_id(self, id):
        res = self.es_obj.get(index=self.index_name, doc_type=self.index_type, id=id)
        return res['hits']

    def get_data_by_body(self, search):
        # res = self.es_obj.search(index=self.index_name, doc_type=self.index_type, body=search)
        res = self.es_obj.search(index=self.index_name, body=search)
        return res['hits']

    def update_data(self, id, body):
        res = self.es_obj.update(index=self.index_name, doc_type=self.index_type, id=id, body=body)
        return res

    def delete_type_data(self):
        query_object = {'query': {'match_all': {}}}
        res = self.es_obj.delete_by_query(index_name=self.index_name, doc_type=self.index_type, body=query_object)
        return res

    def delect_index_data(self, id):
        res = self.es_obj.delete(index=self.index_name, doc_type=self.index_type, id=id)
        return res

    def delete_by_query(self, query):
        res = self.es_obj.delete_by_query(index=self.index_name, doc_type=self.index_type, body=query)
        return res


def secret_key(length=30):
    """
    Generate secret key from alpha and digit.
    :param length: length of secret key.
    :return: [length] long secret key.
    """
    key = ''
    while length:
        key += random.choice(string.ascii_letters + string.digits)
        length -= 1
    return key


def hash_code(*args, **kwargs):
    """
    Generate 64-strings(in hashlib.sha256()) hash code.
    :param args: for any other position args packing.
    :param kwargs: for any other key-word args packing.
    :return: 64-strings long hash code.
    """
    text = ''
    if not args and not kwargs:
        text += time.strftime("%Y%m%d%H%M%s")
    if args:
        for arg in args:
            text += str(arg)
    if kwargs:
        for kwarg in kwargs:
            text += str(kwargs[kwarg])
    return hashlib.sha256(text.encode("utf-8")).hexdigest()


if __name__ == '__main__':
    server.run(host='0.0.0.0', port=8660, debug=False)

插入函數(shù)及接口

def es_insert(datas):
    try:
        # 可以等同于 DataBase
        index_name = "index_name"
        # 可以等同于 Table
        index_type = "_doc"
		# ES對(duì)象
        es = ESObject(index_name, index_type, 'localhost', 9200)
		
        # # 刪除索引
        # es.delete_index()
        #
        # # 建立索引
        # settings = {
        #     "settings": {
        #         "number_of_shards": 5,
        #         "number_of_replicas": 0
        #     },
        #     "index": {
        #         "refresh_interval": "20s"
        #     },
        #     "mappings": {
        #         index_type: {
        #             "dynamic": "strict",
        #             "properties": {
        #                 "es_id": {
        #                     "type": "text"
        #                 }, "content": {
        #                     "type": "text"
        #                 }, "file_name": {
        #                     "type": "text"
        #                 }, "jieba_content": {
        #                     "type": "text"
        #                 }
        #             }
        #         }
        #     }
        # }
        # es.create_index(settings)
		#插入操作
        record_list = []
        file_name = datas["filename"]
        for i, data in enumerate(datas["contentList"]):
            jieba_con_list = jieba.lcut(data)
            jieba_con_str = str.join(" ", jieba_con_list)
            es_id = secret_key()
            # print(es_id)
            # print(secret_key())
            record = {"es_id": es_id, "content": data, "file_name": file_name, "jieba_content": jieba_con_str}
            record_list.append(record)
            if len(record_list) >= 10000:
                start = time.time()
                es.bulk_index_data(record_list)
                print("Finished!")
                end = time.time()
                print(str(end - start))
                record_list = []
        print("record_list finished")
        start = time.time()
        es.bulk_index_data(record_list)
        print("success finished!")
        end = time.time()
        print(str(end - start))
        return "1"
    except Exception as e:
        print(e)
        return "0"


@server.route("/es_insert", methods=['get', 'post'])
def question_regex():
    if not request.data:
        return "fail"
     #獲取接口調(diào)用傳入的參數(shù)
    data = json.loads(request.data.decode("utf-8"))
    # print(data)
    res_code = es_insert(data)
    print(res_code)
    return Response(str(res_code))

拓展思路

ESObject是一個(gè)模板,其中有很多其他的函數(shù)。通過Java調(diào)用,還可以實(shí)現(xiàn)很多操作,如刪除、查詢等。文章來源地址http://www.zghlxwxcb.cn/news/detail-557899.html

拓展刪除操作示例

def es_delete_by_id(p_file_name):
    try:
        # 等同于 DataBase
        index_name = "index_name"
        # 等同于 Table
        index_type = "_doc"
        es = ESObject(index_name, index_type, 'localhost', 9200)
        ld_datas = es.get_data_by_body(10000)

        ll_hits = ld_datas['hits']
        ll_delete_list = []
        for i, d in enumerate(ll_hits):
            l_id = d['_id']
            l_file_name = d['_source']['file_name']
            if p_file_name == l_file_name:
                es.delete_index_data(l_id)
                ll_delete_list.append(l_file_name)
        print(list(set(ll_delete_list)))
        return "1"
    except Exception as e:
        print(e)
        return "0"


@server.route("/es_delete", methods=['get', 'post'])
def question_regex():
    if not request.data:
        return "fail"
    data = json.loads(request.data.decode("utf-8"))
    print(data)

    # filename = ''
    # l_res_code = es_delete_by_id(filename)
    l_delete_file_name = data["filename"]
    l_res_code = es_delete_by_id(l_delete_file_name)
    return Response(str(l_res_code))

到了這里,關(guān)于【Elasticsearch】使用Python完成對(duì)ES的插入操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 項(xiàng)目中使用es(一):使用springboot操作elasticsearch

    項(xiàng)目中使用es(一):使用springboot操作elasticsearch

    寫在前面 對(duì)于elasticsearch的搭建,前面寫了一篇文章有簡(jiǎn)單描述如何搭建es,本次主要介紹如何在項(xiàng)目里使用,主要使用ElasticsearchRepository和ElasticsearchRestTemplate操作es。 搭建項(xiàng)目環(huán)境和選擇合適版本 首先選擇合適的項(xiàng)目組件版本,因?yàn)閑s版本和springboot版本有對(duì)應(yīng),如果不合適會(huì)

    2024年02月08日
    瀏覽(26)
  • Java使用Maven工程操作OpenGL ES繪制三角形和圓形;繪制完成后操作鍵盤控制然圖形移動(dòng)

    Java使用Maven工程操作OpenGL ES繪制三角形和圓形;繪制完成后操作鍵盤控制然圖形移動(dòng)

    PS:想快速看到效果的小伙伴,可以在引入依賴后,先跳到完整代碼部分 第一步:依賴引入 第二步:創(chuàng)建類,引入需要的包,設(shè)置全局參數(shù) 1.創(chuàng)建類 2. 包引入 3. 全局參數(shù) 第三步:定義一個(gè)初始化方法 init() 1. GLFW 錯(cuò)誤信息的回調(diào)函數(shù) 這樣做,在發(fā)生 GLFW 錯(cuò)誤時(shí),錯(cuò)誤信息將

    2024年02月08日
    瀏覽(23)
  • 使用postman和es插件操作elasticsearch API

    使用postman和es插件操作elasticsearch API

    本文介紹了使用postman和es瀏覽器插件操作elasticsearch API的常用方法 本文使用的es瀏覽器插件時(shí)edge下的elasticvue,可以在edge的應(yīng)用商店直接搜索安裝,相較于es-head,這個(gè)插件一直在維護(hù)更新,使用還是很方便的 ? ? 查看索引 查看索引主要使用get方法,可以查看單個(gè)or多個(gè)索引,

    2024年02月07日
    瀏覽(22)
  • Python語言,ES(Elasticsearch)基礎(chǔ)查詢

    https://blog.csdn.net/y472360651/article/details/76652021 https://www.cnblogs.com/bainianminguo/articles/12763099.html

    2024年02月11日
    瀏覽(19)
  • ES批量上傳數(shù)據(jù) - Python操作ES
  • Elasticsearch、Kibana以及Java操作ES 的快速使用

    Elasticsearch、Kibana以及Java操作ES 的快速使用

    ? 創(chuàng)建docker自定義網(wǎng)絡(luò) docker自定義網(wǎng)絡(luò)可以使得 容器之間使用容器名網(wǎng)絡(luò)互連 ,默認(rèn)的網(wǎng)絡(luò)不會(huì)有這功能。 一定要配置自定義網(wǎng)絡(luò),并將兩個(gè)容器同時(shí)加到網(wǎng)絡(luò)中,否則下面的 http://es:9200 會(huì)無法訪問到es ? 啟動(dòng)elastic search、kibana容器 啟動(dòng) elastic search容器 訪問 http://192.168

    2024年02月09日
    瀏覽(26)
  • Springboot 整合 Elasticsearch(五):使用RestHighLevelClient操作ES ②

    Springboot 整合 Elasticsearch(五):使用RestHighLevelClient操作ES ②

    ?? 前情提要: Springboot 整合 Elasticsearch(三):使用RestHighLevelClient操作ES ① 目錄 ?一、Springboot 整合 Elasticsearch 1、RestHighLevelClient API介紹 1.1、全查詢 分頁 排序 1.2、單條件查詢 1.2.1、termQuery 1.2.2、matchQuery 1.2.3、短語檢索 1.3、組合查詢 1.4、范圍查詢 1.5、模糊查詢 1.6、分組

    2024年04月11日
    瀏覽(28)
  • Elasticsearch 7.X SpringBoot 使用 ElasticsearchRestTemplate 操作 ES

    Elasticsearch 7.X SpringBoot 使用 ElasticsearchRestTemplate 操作 ES

    前面學(xué)習(xí)了es rest接口對(duì)es進(jìn)行操作的方式,并且還學(xué)習(xí)了es的分片及擴(kuò)容,有講解了幾種常見的分詞器,喜歡的小伙伴可以看下本專欄的其他文章,本篇主要將 在 SpringBoot 中使用 ElasticsearchRestTemplate 對(duì)ES進(jìn)行操作。 對(duì)于SpringBoot對(duì)ES的操作早在以前我就寫過一篇文章,但那時(shí)基

    2023年04月09日
    瀏覽(20)
  • Elasticsearch 基礎(chǔ)操作與 ES-head 插件的使用

    Elasticsearch 基礎(chǔ)操作與 ES-head 插件的使用

    Elasticsearch 安裝請(qǐng)參考 搭建ELK日志管理平臺(tái) - - 2 ElasticSearch部署 主節(jié)點(diǎn) :默認(rèn)配置是1個(gè)分片1個(gè)副本 主數(shù)據(jù)分片 : 數(shù)據(jù)在分片中被分組存儲(chǔ),例如如果分片是3個(gè)的話,存入數(shù)據(jù) hello 可能被拆分存儲(chǔ)在這幾個(gè)分片之中 副本數(shù)據(jù)分片 : 對(duì)數(shù)據(jù)分片的拷貝 集群健康值 : 黃色表示

    2023年04月16日
    瀏覽(26)
  • Python連接es筆記三之es更新操作

    Python連接es筆記三之es更新操作

    本文首發(fā)于公眾號(hào):Hunter后端 原文鏈接:Python連接es筆記三之es更新操作 這一篇筆記介紹如何使用 Python 對(duì)數(shù)據(jù)進(jìn)行更新操作。 對(duì)于 es 的更新的操作,不用到 Search() 方法,而是直接使用 es 的連接加上相應(yīng)的函數(shù)來操作,本篇筆記目錄如下: 獲取連接 update() update_by_query() 批

    2024年02月07日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包