python 動態(tài)遷移solr數(shù)據(jù)過程解析
前言
上項目的時候,遇見一次需求,需要把在線的 其中一個 collection 里面的數(shù)據(jù)遷移到另外一個collection下,于是就百度了看到好多文章,其中大部分都是使用導入的方法,沒有找到在線數(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 是需要查詢多少行,這里設置為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 需設置為 {"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("導入出錯",data)
#獲取數(shù)據(jù),調用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ù)轉換成字典
se_dict = json.loads(se_data)
#獲取數(shù)據(jù)里的docs數(shù)據(jù)
s_data = (se_dict["response"]["docs"])
#循環(huán)得到的數(shù)據(jù),刪除 version鍵值,并使用多線程調用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"
#導入數(shù)據(jù)導目的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)絡,不能實現(xiàn)在線傳輸,可以先把for循環(huán) 刪除了version鍵值的數(shù)據(jù),寫入一個文件中,然后copy到目的網(wǎng)絡的服務器上,循環(huán)讀取文件進行上傳,如下寫入文件(這個就根據(jù)各位大佬的喜好來寫了),但讀取后,需要把每一條數(shù)據(jù)都轉換成字典進行上傳:
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)容復制到如圖位置,最后點擊submit document 按鈕即可
#控制web界面刪除數(shù)據(jù) <delete><query>:</query></delete> <commit/>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 使用python和Django完成博客數(shù)據(jù)庫的遷移方法
- Python依賴包遷移到斷網(wǎng)環(huán)境操作
- 如何把外網(wǎng)python虛擬環(huán)境遷移到內(nèi)網(wǎng)
- 如何將你的應用遷移到Python3的三個步驟
- 詳解Python3遷移接口變化采坑記
- python django生成遷移文件的實例
- Python依賴包整體遷移方法詳解
- pycharm使用正則表達式批量添加print括號完美從python2遷移到python3
- python虛擬環(huán)境遷移方法
- python實現(xiàn)數(shù)據(jù)庫跨服務器遷移
- 用python寫個博客遷移工具
相關文章
Python3變量與基本數(shù)據(jù)類型用法實例分析
這篇文章主要介紹了Python3變量與基本數(shù)據(jù)類型用法,結合實例形式分析了Python3保留字、標識符、變量、基本數(shù)據(jù)類型及相關操作技巧,需要的朋友可以參考下2020-02-02
pycharm配置Anaconda虛擬環(huán)境全過程
這篇文章主要介紹了pycharm配置Anaconda虛擬環(huán)境全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
python Web flask 視圖內(nèi)容和模板實現(xiàn)代碼
這篇文章主要介紹了python Web flask 視圖內(nèi)容和模板實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
利用soaplib搭建webservice詳細步驟和實例代碼
這篇文章主要介紹了使用python soaplib搭建webservice詳細步驟和實例代碼,大家可以參考使用2013-11-11
python相對包導入報“Attempted?relative?import?in?non-package”錯誤
這篇文章主要介紹了python相對包導入報“Attempted?relative?import?in?non-package”錯誤,本文要在原理上解決?python當中相對包導入出現(xiàn)的問題,需要的朋友可以參考下2023-02-02

