Python操作ES的方式及與Mysql數(shù)據(jù)同步過程示例
Python操作Elasticsearch的兩種方式
# 官方提供的:Elasticsearch
# pip install elasticsearch
# GUI:pyhon能做圖形化界面編程嗎?
-Tkinter
-pyqt
# 使用(查詢是重點(diǎn))
# pip3 install elasticsearch
https://github.com/elastic/elasticsearch-py
from elasticsearch import Elasticsearch
obj = Elasticsearch(['127.0.0.1:9200','192.168.1.1:9200','192.168.1.2:9200'],)
# 創(chuàng)建索引(Index)
# body:用來干什么?mapping:{},setting:{}
# result = obj.indices.create(index='user',ignore=400)
# print(result)
# 刪除索引
# result = obj.indices.delete(index='user', ignore=[400, 404])
# 插入和查詢數(shù)據(jù)(文檔的增刪查改),是最重要
# 插入數(shù)據(jù)
# POST news/politics/1
# {'userid': '1', 'username': 'lqz','password':'123'}
# data = {'userid': '1', 'username': 'lqz','password':'123'}
# result = obj.create(index='news', doc_type='politics', id=1, body=data)
# print(result)
# 更新數(shù)據(jù)
'''
不用doc包裹會(huì)報(bào)錯(cuò)
ActionRequestValidationException[Validation Failed: 1: script or doc is missing
'''
# data ={'doc':{'userid': '1', 'username': 'lqz','password':'123ee','test':'test'}}
# result = obj.update(index='news', doc_type='politics', body=data, id=1)
# print(result)
# 刪除數(shù)據(jù)
# result = obj.delete(index='news', doc_type='politics', id=1)
# 查詢
# 查找所有文檔
# query = {'query': {'match_all': {}}}
# 查找名字叫做jack的所有文檔
# query = {'query': {'match': {'desc': '嬌憨可愛'}}}
# query = {'query': {'term': {'from': 'sheng'}}}
query = {'query': {'term': {'name': '娘子'}}}
# term和match的區(qū)別
# term是短語查詢,不會(huì)對term的東西進(jìn)行分詞
# match 會(huì)多match的東西進(jìn)行分詞,再去查詢
# 查找年齡大于11的所有文檔
# allDoc = obj.search(index='lqz', doc_type='doc', body=query)
allDoc = obj.search(index='lqz', doc_type='doc', body=query)
print(allDoc)
import json
print(json.dumps(allDoc))
# print(allDoc['hits']['hits'][0]['_source'])
# 如何集成到django項(xiàng)目中:創(chuàng)建索引,提前創(chuàng)建好就行了
# 插入數(shù)據(jù),查詢數(shù)據(jù),修改數(shù)據(jù)
# query = {'query': {'term': {'name': '娘子'}}}
# allDoc = obj.search(index='lqz', doc_type='doc', body=query)
# json格式直接返回
# saas :軟件即服務(wù),不是用人家服務(wù),而是寫服務(wù)給別人用----》正常的開發(fā)
# 輿情監(jiān)測系統(tǒng):(爬蟲)
# 只監(jiān)控微博---》宜家:微博,百度貼吧,上市公司
# 公安:負(fù)面的,---》追蹤到哪個(gè)用戶發(fā)的---》找上門了
# qq群,微信群----》輿情監(jiān)控(第三方做不了,騰訊出的輿情監(jiān)控,第三方機(jī)構(gòu)跟騰訊合作,騰訊提供接口,第三方公司做)
# 平臺(tái)開發(fā)出來,別人買服務(wù)---》買一年的微博關(guān)鍵字監(jiān)控ERP:公司財(cái)務(wù),供應(yīng)鏈
某個(gè)大公司,金蝶,用友,開發(fā)了軟件----》你們公司自己買服務(wù)器---》軟件跑在你服務(wù)器上
saas模式:公司買服務(wù),10年服務(wù)----》賬號密碼---》登進(jìn)去就能操作---》出了問題找用友---》服務(wù)器在別人那---》政務(wù)云,各種云---所有東西上云
---政府花錢買的東西---》用友敢泄露嗎?
---未來的云計(jì)算---》只能能上網(wǎng)---》計(jì)算機(jī)運(yùn)算能力有限---》上云買服務(wù)---》計(jì)算1+。。。+100 ---》買了計(jì)算服務(wù),直接拿到結(jié)果
# 第二種使用方式
# https://github.com/elastic/elasticsearch-dsl-py
# pip3 install elasticsearch-dsl
from datetime import datetime
from elasticsearch_dsl import Document, Date, Nested, Boolean,analyzer, InnerDoc, Completion, Keyword, Text,Integer
from elasticsearch_dsl.connections import connections
connections.create_connection(hosts=["localhost"])
class Article(Document):
title = Text(analyzer='ik_max_word', search_analyzer="ik_max_word", fields={'title': Keyword()})
author = Text()
class Index:
name = 'myindex' # 索引名
def save(self, ** kwargs):
return super(Article, self).save(** kwargs)
if __name__ == '__main__':
# Article.init() # 創(chuàng)建映射
# 保存數(shù)據(jù)
# article = Article()
# article.title = "測試數(shù)據(jù)"
# article.author = "egon"
# article.save() # 數(shù)據(jù)就保存了
#查詢數(shù)據(jù)
# s=Article.search()
# s = s.filter('match', title="測試")
# results = s.execute()
# # 類比queryset對象,列表中一個(gè)個(gè)對象
# # es中叫Response,當(dāng)成一個(gè)列表,列表中放一個(gè)個(gè)對象
# print(results)
#刪除數(shù)據(jù)
# s = Article.search()
# s = s.filter('match', title="測試").delete()
#修改數(shù)據(jù)
s = Article().search()
s = s.filter('match', title="測試")
results = s.execute()
print(results[0])
results[0].title="xxx"
results[0].save()
# 其他操作,參見文檔
mysql和Elasticsearch同步數(shù)據(jù)
# 只要article表插入一條數(shù)據(jù),就自動(dòng)同步到es中
# 第一種方案:
-每當(dāng)aritcle表插入一條數(shù)據(jù)(視圖類中,Article.objects.create(),update)
-往es中插入一條
-缺陷:代碼耦合度高,改好多地方
# 第二種方案:
-重寫create方法,重寫update方法
-缺陷:同步操作---》es中插入必須返回結(jié)果才能繼續(xù)往下走
# 第三種方案:
-用celery,做異步
-缺陷:引入celery,還得有消息隊(duì)列。。。
# 第四種方案:(用的最多)
-重寫create方法,重寫update方法,用信號存入,異步操作
-缺陷:有代碼侵入
# 第五種方案:(項(xiàng)目不寫代碼,自動(dòng)同步),第三方開源的插件
-https://github.com/siddontang/go-mysql-elasticsearch----go寫
-你可以用python重寫一個(gè),放到git上給別人用(讀了mysql的日志)
-跟平臺(tái)無關(guān),跟語言無關(guān)
-如何使用:
-源碼下載---》交叉編譯---》可執(zhí)行文件--》運(yùn)行起來--》配置文件配好,就完事了
# 配置文件
[[source]]
schema = "數(shù)據(jù)庫名"
tables = ["article"]
[[rule]]
schema = "數(shù)據(jù)庫名"
table = "表明"
index = "索引名"
type = "類型名"
# 缺陷:
-es跟mysql同步時(shí),不希望把表所有字段都同步,mysql的多個(gè)表對著es的一個(gè)類型
# 話術(shù)升級:
-一開始同步
-用了開源插件(讀取mysql日志,連接上es,進(jìn)行同步)
-用信號自己寫的
-再高端:仿著他的邏輯,用python自己寫的,----》(把這個(gè)東西開源出來)
haystack的使用
- django上的一個(gè)第三方模塊 ---》你使用過的django第三方模塊有哪些?
- 可以在django上實(shí)現(xiàn)全文檢索
- 相當(dāng)于orm--》對接es,solr,whoosh
- http://www.dbjr.com.cn/article/218631.htm
- 不支持es,6以上版本
- haystack+Elasticsearch實(shí)現(xiàn)全文檢索
- es的原生操作:ELasticsearch Elasticsearch-dsl
Redis補(bǔ)充
#1 只有5種數(shù)據(jù)結(jié)構(gòu):
-多種數(shù)據(jù)結(jié)構(gòu):字符串,hash,列表,集合,有序集合
#2 單線程,速度為什么這么快?
-本質(zhì)還是因?yàn)槭莾?nèi)存數(shù)據(jù)庫
-epoll模型(io多路復(fù)用)
-單線程,沒有線程,進(jìn)程間的通信
#3 linux上 安裝redis#下載
https://redis.io/download/
#解壓
tar -xzf redis-5.0.7.tar.gz
#建立軟連接
ln -s redis-5.0.7 redis
cd redis
make&&make install
# bin路徑下幾個(gè)命令:redis-cli,redis-server,redis-sentinel
# 在任意位置能夠執(zhí)行redis-server 如何做?配置環(huán)境變量
#4 啟動(dòng)redis的三種方式
-方式一:(一般不用,沒有配置文件)
-redis-server
-方式二:(用的也很少)
redis-serve --port 6380
-方式三:(都用這種,配置文件)
daemonize yes #是否以守護(hù)進(jìn)程啟動(dòng)
pidfile /var/run/redis.pid #進(jìn)程號的位置,刪除
port 6379 #端口號
dir "/opt/soft/redis/data" #工作目錄
logfile 6379.log #日志位置
# 啟動(dòng):redis-server redis.conf1
#5 客戶端連接
redis-cli -h 127.0.0.1 -p 6379
#6 使用場景
-看md文檔以上就是Python操作ES的方式及Mysql數(shù)據(jù)同步過程示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Python操作ES方式Mysql數(shù)據(jù)同步的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
asp.net php asp jsp 301重定向的代碼(集合)
介紹一下針對各類程序系統(tǒng)實(shí)施301重定向的代碼,需要的朋友可以參考下。2010-11-11
VsCode運(yùn)行html界面的實(shí)戰(zhàn)步驟
在VSCode中默認(rèn)編寫的HTML頁面是不能運(yùn)行的,下面這篇文章主要給大家介紹了關(guān)于VsCode運(yùn)行html界面的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
Github創(chuàng)建個(gè)人訪問Tokens令牌
這篇文章介紹了Github創(chuàng)建個(gè)人訪問Tokens令牌的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

