山东大学创新实训2020/6/21

实现数据库的初步写入和查询demo

import time
import pymysql
import csv

def get_time():
    time_str =  time.strftime("%Y{}%m{}%d{} %X")
    return time_str.format("年","月","日")

def get_conn():
    """
    :return: 连接,游标
    """
    # 创建连接
    conn = pymysql.connect(host="47.98.***",
                           # port=3306,
                           user="test",
                           password="***",
                           db="innotrain",
                           charset="utf8mb4")
    # 创建游标
    cursor = conn.cursor()# 执行完毕返回的结果集默认以元组显示
    return conn, cursor

def close_conn(conn, cursor):
    cursor.close()
    conn.close()

def query(sql,*args):
    """
    封装通用查询
    :param sql:
    :param args:
    :return: 返回查询到的结果,((),(),)的形式
    """
    conn, cursor = get_conn()
    cursor.execute(sql)
    # cursor.execute(sql,args)
    res = cursor.fetchall()
    close_conn(conn, cursor)
    return res

def get_predict():
    sql = "select ds,emotion_val,topic from emotion_val where predict=1 order by ds"
    res = query(sql)
    return res

def get_emotion_val():
    sql = "select ds,emotion_val,topic,predict from emotion_val order by predict"
    res = query(sql)
    return res

def get_rawdata():
    sql = "select context,ds,topic from raw_data "
    res = query(sql)
    return res

def get_midrawdata():
    sql = "select ds,emotion_val,topic from emotion_val where predict=0 order by ds"
    res = query(sql)
    return res
在这里插入代码片
from elasticsearch import Elasticsearch, RequestsHttpConnection
import time*斜体样式*

from datetime import datetime

def get_time():
    time_str =  time.strftime("%Y{}%m{}%d{} %X")
    return time_str.format("年","月","日")

def get_conn():
    """
    :return: 连接,游标
    """
    # 创建连接
    es = Elasticsearch(
        ['es-cn-st21p5hmc00*****.public.elasticsearch.aliyuncs.com'],
        http_auth=('elastic', '****'),
        port=9200,
        use_ssl=False
    )
    return es

def close_conn(es):
    pass

def get_predict():
    es = get_conn()
    action = { "query": {
    "match": {
      "predict": "1"
    }
    },
    "sort": [
        {
      "ds": {
        "order": "asc"
                }
            }
        ],
        "size": 2000000
    }

    res = es.search(index="emotion_val", body=action)
    return res

def get_emotion_val():
    es = get_conn()
    action = {
        "sort": [
            {
                "predict": {
                    "order": "asc"
                }
            }
        ],
        "size": 2000000
    }

    res = es.search(index="emotion_val", body=action)
    return res

def get_rawdata():
    es = get_conn()
    action = {
        "size": 2000000
    }
    res = es.search(index="raw_data", body=action)
    return res

def get_midrawdata():
    es = get_conn()
    action = {"query": {
        "match": {
            "predict": "0"
        }
    },
        "sort": [
            {
                "ds": {
                    "order": "asc"
                }
            }
        ],
        "size": 200000
    }

    res = es.search(index="emotion_val", body=action)
    return res

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值