-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
318cdab
commit ec281eb
Showing
12 changed files
with
188 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import MySQLdb | ||
|
||
connect = MySQLdb.connect( | ||
host='localhost', | ||
port=3306, | ||
user='root', | ||
passwd='qaz123', | ||
db='biodesignver' | ||
) | ||
cur = connect.cursor() | ||
cur2 = connect.cursor() | ||
|
||
connect1 = MySQLdb.connect( | ||
host='localhost', | ||
port=3306, | ||
user='root', | ||
passwd='qaz123', | ||
db='wiki' | ||
) | ||
cur1 = connect1.cursor() | ||
|
||
cur.execute("select * from simpepart") | ||
part = cur.fetchone() | ||
while part: | ||
teams = part[6] | ||
if teams!="": | ||
teams = teams.split(',') | ||
ids = list() | ||
for team in teams: | ||
print(team) | ||
position = team.find('_') | ||
year = int(team[0:position]) | ||
name = team[position+1:] | ||
cur1.execute("select * from team where year=%d and team_name='%s'"%(year, name)) | ||
team = cur1.fetchone() | ||
if team: | ||
print(team[18]) | ||
ids.append(team[18]) | ||
teamId = ','.join(ids) | ||
cur2.execute("update simpepart set teamId='%s' where part_id=%d"%(teamId, part[0])) | ||
part = cur.fetchone() | ||
connect.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
from elasticsearch import Elasticsearch | ||
import MySQLdb | ||
connect = MySQLdb.connect( | ||
host='localhost', | ||
port=3306, | ||
user='root', | ||
passwd='', | ||
db='wiki' | ||
) | ||
cur = connect.cursor() | ||
|
||
es = Elasticsearch() | ||
query = { | ||
"size":5000, | ||
"query": { | ||
"match_all": {} | ||
} | ||
} | ||
|
||
_searched = es.search(index='team_wiki', doc_type='wiki',body=query) | ||
teams = _searched['hits']['hits'] | ||
for team in teams: | ||
id = team["_id"] | ||
team = team["_source"] | ||
team_name = team["team_name"] | ||
year = team["year"] | ||
print(year, team_name) | ||
cur.execute("update team set _id='%s' where year = %d and team_name='%s'"%(id, int(year), team_name)) | ||
connect.commit() | ||
from elasticsearch import Elasticsearch | ||
import MySQLdb | ||
connect = MySQLdb.connect( | ||
host='localhost', | ||
port=3306, | ||
user='root', | ||
passwd='qaz123', | ||
db='wiki' | ||
) | ||
cur = connect.cursor() | ||
|
||
es = Elasticsearch() | ||
query = { | ||
"size":5000, | ||
"query": { | ||
"match_all": {} | ||
} | ||
} | ||
|
||
_searched = es.search(index='team_wiki', doc_type='wiki',body=query) | ||
teams = _searched['hits']['hits'] | ||
for team in teams: | ||
id = team["_id"] | ||
team = team["_source"] | ||
team_name = team["team_name"] | ||
year = team["year"] | ||
print(year, team_name) | ||
cur.execute("update team set _id='%s' where year = %d and team_name='%s'"%(id, int(year), team_name)) | ||
connect.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# -*- coding:utf-8 -*- | ||
from elasticsearch import Elasticsearch | ||
|
||
# copy data from mongodb to elasticsearch | ||
import pymysql | ||
|
||
# 打开数据库连接 | ||
db = pymysql.connect("localhost","root","qaz123","wiki") | ||
|
||
# 使用cursor()方法获取操作游标 | ||
cursor = db.cursor() | ||
|
||
# SQL 查询语句 | ||
sql = "SELECT * FROM team" | ||
|
||
def buildIndexAndMapping(_es): | ||
_index_mappings = { | ||
"mappings": { | ||
"wiki": { | ||
"properties": { | ||
"year" : {"type": "keyword"}, | ||
"team_name" : {"type": "text"}, | ||
"background" : {"type": "text"}, | ||
"description" : {"type": "text"}, | ||
"design" : {"type": "text"}, | ||
"human_practice" : {"type": "text"}, | ||
"modeling" : {"type": "text"}, | ||
"notebook" : {"type": "text"}, | ||
"protocol" : {"type": "text"}, | ||
"result" : {"type": "text"}, | ||
"track" : {"type": "keyword"}, | ||
"recommend": {"type": "text"}, | ||
"recommendWords": {"type": "text"}, | ||
"biobrick": {"type": "text"}, | ||
"keywords" : {"type": "text"}, | ||
"theme" : {"type": "keyword"}, | ||
"awards" : {"type": "text"}, | ||
"type": {"type": "text"}, | ||
"medal": {"type": "text"}, | ||
"calScore": {"type": "integer"}, | ||
"link": {"type": "text"} | ||
} | ||
}, | ||
} | ||
} | ||
if _es.indices.exists(index='team_wiki') is not True: | ||
_es.indices.create(index='team_wiki', body=_index_mappings) | ||
|
||
def writeData(_es): | ||
#这里的数据是测试用的 ,,, | ||
user_cursor = [] | ||
try: | ||
# 执行SQL语句 | ||
cursor.execute(sql) | ||
# 获取所有记录列表 | ||
results = cursor.fetchall() | ||
for row in results: | ||
tmp = { | ||
"year" : row[0].__str__(), | ||
"team_name" : row[1].__str__(), | ||
"background" : row[2].__str__(), | ||
"description" : row[3].__str__(), | ||
"design" : row[4].__str__(), | ||
"human_practice" : row[5].__str__(), | ||
"modeling" : row[6].__str__(), | ||
"notebook" : row[7].__str__(), | ||
"protocol" : row[8].__str__(), | ||
"result" : row[9].__str__(), | ||
"track" : row[10].__str__(), | ||
"recommend": row[11].__str__(), | ||
"recommendWords": row[12].__str__(), | ||
"biobrick": row[13].__str__(), | ||
"keywords" : row[14].__str__(), | ||
"theme" : row[15].__str__(), | ||
"awards" : row[16].__str__(), | ||
"type": row[17].__str__(), | ||
"medal": row[18].__str__(), | ||
"calScore": row[19].__str__(), | ||
"link": row[20].__str__() | ||
} | ||
user_cursor.append(tmp) | ||
except: | ||
print("Error: unable to fetch data") | ||
processed = 0 | ||
for _doc in user_cursor: | ||
try: | ||
_es.index(index='team_wiki', doc_type='wiki', refresh=True, body=_doc) | ||
processed += 1 | ||
print('Processed: ' + str(processed), flush=True) | ||
except Exception as e: | ||
print(e) | ||
|
||
|
||
def mainFunc(): | ||
print("copy data from mongodb to elasticsearch") | ||
es = Elasticsearch() | ||
buildIndexAndMapping(es) | ||
print("search finished") | ||
writeData(es) | ||
|
||
|
||
if __name__ == '__main__': | ||
mainFunc() |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.