python3引入mysql elasticsearch

pip install  pymysql

pip install elasticsearch 

es = Elasticsearch(["192.168.56.61"])


def get_skin_list(page):
    db = pymysql.connect("192.168.56.61","dbuser","dbpwd","hyjiang" )
    cursor = db.cursor()
    db.set_charset("utf8")
    cursor.execute('SET NAMES utf8;')
    cursor.execute('SET CHARACTER SET utf8;')
    cursor.execute('SET character_set_connection=utf8;')
    start = (page-1)*1000
    sql = "select * from hy_skin"\
            " order by id desc limit %s,%s"
    cursor.execute(sql, (start, 1000))
    data = cursor.fetchall()
    return data
def import_db_video(page):
    skin_list = get_skin_list(page)

    if not skin_list:
        return False
    actions = []
    for fields in skin_list:
        action = {
            "_index": "hyjiang",
            "_type": "skin",
            "_id": fields[0],
            "_source": {
                "uid": int(fields[1]),
                "username": fields[2],
                "skin_name": fields[3],
                "tags": fields[6].split(","),
                "content": fields[7],
                "list_img": fields[8],
                "info_img": fields[9],
                "money": int(fields[10]),
                "download_num": int(fields[11]),
                "status": int(fields[12]),
                "update_time":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
            }
        }
        actions.append(action)

        helpers.bulk(es, actions)
        print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+":成功上传video"+str(len(actions)))

    return True

猜你喜欢

转载自my.oschina.net/PHPDOTAER/blog/1789676
今日推荐