溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python 中怎么動態(tài)遷移solr數(shù)據(jù)

發(fā)布時(shí)間:2021-06-17 17:09:31 來源:億速云 閱讀:129 作者:Leah 欄目:開發(fā)技術(shù)

python 中怎么動態(tài)遷移solr數(shù)據(jù),很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

一、使用http的接口先進(jìn)行查詢

使用如下格式查詢:

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

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

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

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

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

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

python 中怎么動態(tài)遷移solr數(shù)據(jù)

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

python 中怎么動態(tài)遷移solr數(shù)據(jù)

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

wt:使用json格式提交

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

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

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

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

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

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

三、實(shí)現(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)入出錯(cuò)",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ù)為一個(gè)循環(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("耗時(shí):",end_time-start_time,"秒")

備注:

一、如果你的collection 不在同一個(gè)網(wǎng)絡(luò),不能實(shí)現(xiàn)在線傳輸,可以先把for循環(huán) 刪除了version鍵值的數(shù)據(jù),寫入一個(gè)文件中,然后copy到目的網(wǎng)絡(luò)的服務(wù)器上,循環(huán)讀取文件進(jìn)行上傳,如下寫入文件(這個(gè)就根據(jù)各位大佬的喜好來寫了),但讀取后,需要把每一條數(shù)據(jù)都轉(zhuǎn)換成字典進(jì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ù)制到如圖位置,最后點(diǎn)擊submit document 按鈕即可

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

python 中怎么動態(tài)遷移solr數(shù)據(jù)

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI