欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 動態(tài)遷移solr數(shù)據(jù)過程解析

 更新時間:2019年09月04日 09:49:33   作者:1066897515  
這篇文章主要介紹了python 動態(tài)遷移solr數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

前言

上項目的時候,遇見一次需求,需要把在線的 其中一個 collection 里面的數(shù)據(jù)遷移到另外一個collection下,于是就百度了看到好多文章,其中大部分都是使用導(dǎo)入的方法,沒有找到在線數(shù)據(jù)的遷移方法。于是寫了python腳本,分享出來。

思路: collection數(shù)據(jù)量比較大,所以一次性操作所有數(shù)據(jù)太大,于是分段執(zhí)行操作。

先分段 按1000條數(shù)據(jù)量進行查詢,處理成json數(shù)據(jù)

把處理后的json數(shù)據(jù) 發(fā)送到目的collection上即可

實現(xiàn):

一、使用http的接口先進行查詢

使用如下格式查詢:

其中:collection_name 是你查詢的collection的名稱

rows 是需要查詢多少行,這里設(shè)置為1000

start 從多少行開始進行查詢,待會兒腳本里面就是控制這個參數(shù)進行循環(huán)查詢

http://host:port/solr/collection_name/select?q=*:*&rows=1000&start=0

查詢處理后會得到如下圖片里面的數(shù)據(jù)格式,其中

在response里面,有兩個鍵值數(shù)據(jù)是我們需要的,一個是numFound(總的數(shù)據(jù)條數(shù)),docs(所有json數(shù)據(jù)都在這里面)

在docs里面,每條數(shù)據(jù)都帶有version 鍵值,這個需要給去掉


二、使用http的接口提交數(shù)據(jù)

wt:使用json格式提交

http://host:port/solr/collection_name/update?wt=json

header 需設(shè)置為 {"Content-Type": "application/json"}

提交參數(shù):solr在做索引的時候,如果文檔已經(jīng)存在,就替換。(這里的參數(shù)也可以直接加到url里面)

{"overwrite":"true","commit":"true"}

data_dict 就是我們處理后的 docs數(shù)據(jù)

提交數(shù)據(jù):data={"add":{ "doc":data_dict}}

三、實現(xiàn)的腳本如下:

#coding=utf-8
import requests as r
import json
import threading
import time
#發(fā)送數(shù)據(jù)到目的url des_url,data_dict 參數(shù)為去掉version鍵值后的一條字典數(shù)據(jù)
def send_data(des_url,data_dict):
 data={"add":{ "doc":data_dict}}
 headers = {"Content-Type": "application/json"}
 params = {"boost":1.0,"overwrite":"true","&commitWithin":1000,"commit":"true"}
 url = "%s/update?wt=json"%(des_url)
 re = r.post(url,json = data,params=params,headers=headers)
 if re.status_code != 200:
  print("導(dǎo)入出錯",data)

#獲取數(shù)據(jù),調(diào)用send_data 發(fā)送數(shù)據(jù)到目的url
def get_data(des_url,src_url):
  #定義起始行
 start = 0
 #先獲取到總的數(shù)據(jù)條數(shù)
 se_data=r.get("%s/select?q=*:*&rows=0&start=%s"%(src_url,start)).text
 se_dict = json.loads(se_data)
 numFound = int(se_dict["response"]["numFound"])
 #while循環(huán),1000條數(shù)據(jù)為一個循環(huán)
 while start < numFound:
  #定義存放多線程的列表
  th_li = []
    #獲取1000條數(shù)據(jù)
  se_data=r.get("%s/select?q=*:*&rows=1000&start=%s"%(src_url,start)).text
    #把獲取的數(shù)據(jù)轉(zhuǎn)換成字典
  se_dict = json.loads(se_data)
    #獲取數(shù)據(jù)里的docs數(shù)據(jù)
  s_data = (se_dict["response"]["docs"])

  #循環(huán)得到的數(shù)據(jù),刪除 version鍵值,并使用多線程調(diào)用send_data 方法發(fā)送數(shù)據(jù)
  for i in s_data:
   del i["_version_"]
   th = threading.Thread(target=send_data,args=(des_url,i))
   th_li.append(th)

  for t in th_li:
   t.start()
   t.join()

  start += 1000
  print(start)

if __name__ == "__main__":
 #源數(shù)據(jù),查詢數(shù)據(jù)的collection地址
 src_url = "http://ip:port/solr/src_connection"
 #導(dǎo)入數(shù)據(jù)導(dǎo)目的collection 的地址
 des_url = "http://ip:port/solr/des_connection"
 start_time = time.time()
 get_data(des_url,src_url)
 end_time = time.time()
 print("耗時:",end_time-start_time,"秒")

備注:

一、如果你的collection 不在同一個網(wǎng)絡(luò),不能實現(xiàn)在線傳輸,可以先把for循環(huán) 刪除了version鍵值的數(shù)據(jù),寫入一個文件中,然后copy到目的網(wǎng)絡(luò)的服務(wù)器上,循環(huán)讀取文件進行上傳,如下寫入文件(這個就根據(jù)各位大佬的喜好來寫了),但讀取后,需要把每一條數(shù)據(jù)都轉(zhuǎn)換成字典進行上傳:

file = open("solr.json","a+")
for i in s_data:
del i["version"]
file.write(str(i)+"\n")
file.close()

二、清除數(shù)據(jù)可使用一下方法,自測比較方便的一種

在你要清除collection里面

選擇 documents

document type 選擇xml

將一下內(nèi)容復(fù)制到如圖位置,最后點擊submit document 按鈕即可

#控制web界面刪除數(shù)據(jù)
<delete><query>:</query></delete>
<commit/>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論