最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python操作ES的方式及與Mysql數(shù)據(jù)同步過(guò)程示例

 更新時(shí)間:2022年04月19日 18:01:30   作者:Jeff的技術(shù)棧  
這篇文章主要為大家介紹了?Python操作Elasticsearch的兩種方式及與Mysql數(shù)據(jù)同步過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

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:用來(lái)干什么?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是短語(yǔ)查詢,不會(huì)對(duì)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)測(cè)系統(tǒng):(爬蟲)
# 只監(jiān)控微博---》宜家:微博,百度貼吧,上市公司
# 公安:負(fù)面的,---》追蹤到哪個(gè)用戶發(fā)的---》找上門了
# qq群,微信群----》輿情監(jiān)控(第三方做不了,騰訊出的輿情監(jiān)控,第三方機(jī)構(gòu)跟騰訊合作,騰訊提供接口,第三方公司做)
# 平臺(tái)開發(fā)出來(lái),別人買服務(wù)---》買一年的微博關(guān)鍵字監(jiān)控

ERP:公司財(cái)務(wù),供應(yīng)鏈

某個(gè)大公司,金蝶,用友,開發(fā)了軟件----》你們公司自己買服務(wù)器---》軟件跑在你服務(wù)器上
saas模式:公司買服務(wù),10年服務(wù)----》賬號(hào)密碼---》登進(jìn)去就能操作---》出了問(wèn)題找用友---》服務(wù)器在別人那---》政務(wù)云,各種云---所有東西上云

---政府花錢買的東西---》用友敢泄露嗎?
---未來(lái)的云計(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 = "測(cè)試數(shù)據(jù)"
    # article.author = "egon"
    # article.save()  # 數(shù)據(jù)就保存了
    #查詢數(shù)據(jù)
    # s=Article.search()
    # s = s.filter('match', title="測(cè)試")
    # results = s.execute()
    # # 類比queryset對(duì)象,列表中一個(gè)個(gè)對(duì)象
    # # es中叫Response,當(dāng)成一個(gè)列表,列表中放一個(gè)個(gè)對(duì)象
    # print(results)
    #刪除數(shù)據(jù)
    # s = Article.search()
    # s = s.filter('match', title="測(cè)試").delete()
    #修改數(shù)據(jù)
    s = Article().search()
    s = s.filter('match', title="測(cè)試")
    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方法,用信號(hào)存入,異步操作
  -缺陷:有代碼侵入
# 第五種方案:(項(xiàng)目不寫代碼,自動(dòng)同步),第三方開源的插件
	-https://github.com/siddontang/go-mysql-elasticsearch----go寫
  -你可以用python重寫一個(gè),放到git上給別人用(讀了mysql的日志)
  -跟平臺(tái)無(wú)關(guān),跟語(yǔ)言無(wú)關(guān)
  -如何使用:
  	-源碼下載---》交叉編譯---》可執(zhí)行文件--》運(yùn)行起來(lái)--》配置文件配好,就完事了
    # 配置文件
    [[source]]
    schema = "數(shù)據(jù)庫(kù)名"
    tables = ["article"]
    [[rule]]
    schema = "數(shù)據(jù)庫(kù)名"
    table = "表明"
    index = "索引名"
    type = "類型名"
  # 缺陷:
  	-es跟mysql同步時(shí),不希望把表所有字段都同步,mysql的多個(gè)表對(duì)著es的一個(gè)類型
  # 話術(shù)升級(jí):
  	-一開始同步
    -用了開源插件(讀取mysql日志,連接上es,進(jìn)行同步)
    -用信號(hào)自己寫的
    -再高端:仿著他的邏輯,用python自己寫的,----》(把這個(gè)東西開源出來(lái))

haystack的使用

  • django上的一個(gè)第三方模塊 ---》你使用過(guò)的django第三方模塊有哪些?
  • 可以在django上實(shí)現(xiàn)全文檢索
  • 相當(dāng)于orm--》對(duì)接es,solr,whoosh
  • http://m.fzitv.net/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ù)庫(kù)
  -epoll模型(io多路復(fù)用)
  -單線程,沒(méi)有線程,進(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的三種方式
  	-方式一:(一般不用,沒(méi)有配置文件)
    	-redis-server
    -方式二:(用的也很少)
    	redis-serve --port 6380
    -方式三:(都用這種,配置文件)
    	daemonize yes #是否以守護(hù)進(jìn)程啟動(dòng)
      pidfile /var/run/redis.pid   #進(jìn)程號(hào)的位置,刪除
      port 6379    #端口號(hào)
      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 使用場(chǎng)景
  -看md文檔

以上就是Python操作ES的方式及Mysql數(shù)據(jù)同步過(guò)程示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Python操作ES方式Mysql數(shù)據(jù)同步的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

得荣县| 常熟市| 始兴县| 定西市| 新疆| 兴安盟| 屏南县| 仪征市| 娄底市| 金寨县| 长白| 夏河县| 左贡县| 阿克苏市| 墨竹工卡县| 班戈县| 惠安县| 弥勒县| 滕州市| 封开县| 黄陵县| 丰城市| 紫阳县| 盱眙县| 仁寿县| 甘洛县| 大港区| 沙田区| 类乌齐县| 南陵县| 芦山县| 蒲江县| 蓬溪县| 宁阳县| 固镇县| 天全县| 潍坊市| 延长县| 灵丘县| 邢台县| 苍溪县|