python 操作es数据库(增,删,改,查)

连接数据库

def connect_es():
"""连接es"""
        username = "elastic"
        password = "elastic"
        es = Elasticsearch(hosts="http://192.168.5.125:9200", http_auth=(username, password))
return es

必须先连接数据库,才可进行下一步操作

查询数据库

# 搜索文档
def search_es():
        index='pos_bill',
        body={
       "query": {
              "bool": {
                    "must": [
                         {
                          "term": {
                              "shopEntityId": {
                                       "value": "1FGIHNM3QRPBTT725C889LSKTH001574"
                                            }
                                        }
                                  }
                               ]
                             }
                               }
                             }

         es=connect_es()
         res = es.search(index=index, body=body)
        print(res['hits']['hits'][0]['_source'])

删除数据

def delete_es():
        index = 'pos_bill'
        es = connect_es()
        data={
"query": {
"term": {
"_id": "77FCA5C399FA9E74F048AD8A131D8BE5"
}
}
}
        es.delete(index=index,id='CHdkHYsBNYUhKaSss1uK',routing="1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK")


插入数据

def insert_es():
    index="pos_bill"
    data={
    "id": "1FGIHNM3QRPBTT725C889LSKTH001574202309150001",
    "billDate": "20230915",
        "billSerialNumber": "1FGIHNM3QRPBTT725C889LSKTH001574202309150001",
        "billType": "601",
        "source": "2",
        "cTimeStamp": "20230915143000",
        "createTime": "20230915143000",
        "exactBillType": "60101",
        "receivableAmount": 200.0,
        "saleTime": "20230915143000",
        "shopEntityId": "1FGIHNM3QRPBTT725C889LSKTH001574",
        "shopEntityName": "果果商户",
        "shopId": "1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK",
        "shopName": "郭氏机构mall04",
        "terminalNumber": "BBBB920AAEAC",
        "thirdPartyOrderNo": "D09090D902",
        "totalFee": 200.0,
        "totalNum": 1.0
        }
        es = connect_es()
        es.index(index='pos_bill',         body=data,routing="1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK")

 更新数据

def update_es():
        index="pos_bill"
        data={
"doc":{
"totalFee": 9999.0,
"totalNum": 99.0
}
}
        es = connect_es()
        es.update(index='pos_bill', body=data,routing="1FGIGMHLFE23PQ4EPNFCDTBBAG001QLK",id="CXdtHYsBNYUhKaSs-1uH")




if __name__ == '__main__':
update_es()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python通过使用`elasticsearch`库和`mysql-connector-python`库可以实现MySQL数据库同步到Elasticsearch的功能。下面是一个300字的回答。 首先,安装所需的库。可以使用以下命令安装`elasticsearch`库和`mysql-connector-python`库: ``` pip install elasticsearch mysql-connector-python ``` 接下来,导入所需的库并连接到MySQL数据库Elasticsearch: ```python import mysql.connector from elasticsearch import Elasticsearch # 连接到MySQL数据库 conn = mysql.connector.connect( host="localhost", user="root", password="password", database="mydatabase" ) # 连接到Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) ``` 然后,执行MySQL询语句来获取数据,并将其插入到Elasticsearch中: ```python # 创建MySQL游标对象 cursor = conn.cursor() # 执行MySQL询语句 cursor.execute("SELECT * FROM mytable") # 获取询结果 results = cursor.fetchall() # 将结果插入到Elasticsearch for row in results: document = { 'id': row[0], # 假设MySQL表中有一个id列 'name': row[1], # 假设MySQL表中有一个name列 # 添加其他需要同步的字段 } es.index(index='myindex', doc_type='mytype', body=document) ``` 最后,关闭MySQL数据库连接和Elasticsearch连接: ```python # 关闭MySQL数据库连接 conn.close() # 关闭Elasticsearch连接 es.close() ``` 以上是用Python实现MySQL数据库同步到Elasticsearch的基本步骤。可以根据具体需求对代码进行更和优化,例如使用配置文件来管理数据库连接信息和Elasticsearch的索引名称等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值