Browse Source

pgsql_util

yufeng0528 4 years ago
parent
commit
66e3e7dddf
5 changed files with 192 additions and 0 deletions
  1. 0 0
      bbztx/get_data.py
  2. 2 0
      util/__init__.py
  3. 39 0
      util/config.cfg
  4. 34 0
      util/config.py
  5. 117 0
      util/pgsql_util.py

+ 0 - 0
bbztx/get_data.py


+ 2 - 0
util/__init__.py

@@ -0,0 +1,2 @@
1
+#!/usr/bin/python
2
+# -*- coding:utf-8 -*-

+ 39 - 0
util/config.cfg

@@ -0,0 +1,39 @@
1
+[mongodb]
2
+host = 127.0.0.1
3
+port = 27017
4
+db = stock
5
+user = stock_root
6
+password = hj123456
7
+
8
+[redis]
9
+host = 192.168.0.217
10
+password = camp@redis63
11
+port = 6279
12
+database = 0
13
+
14
+[mysql]
15
+host = 121.41.17.212
16
+user = root
17
+password = huojutech!23
18
+db = stock_data
19
+
20
+
21
+[logstash]
22
+host = 192.168.0.218
23
+port = 8082
24
+
25
+
26
+[consul]
27
+host = 10.168.244.48
28
+port = 8500
29
+scheme = http
30
+
31
+[application]
32
+servername = ai_voice_engine
33
+host = 10.168.244.48
34
+port = 18088
35
+
36
+[common]
37
+servername = ai_voice_engine
38
+host = 10.168.244.48
39
+port = 18088

+ 34 - 0
util/config.py

@@ -0,0 +1,34 @@
1
+#!/usr/bin/env python
2
+#coding=utf-8
3
+
4
+import sys
5
+import configparser
6
+
7
+
8
+class Config(object):
9
+    config = None
10
+
11
+    def __init__(self):
12
+        try:
13
+            # 读取配置文件
14
+            config = configparser.ConfigParser()
15
+            config.read('/data/quantization/config.cfg', encoding='utf-8')
16
+            self.config = config
17
+        except IOError as e:
18
+            print('cant open file -> ~/data/quantization/config.cfg', e)
19
+            sys.exit(1)
20
+
21
+    def get(self, section, position):
22
+        return self.config.get(section, position)
23
+
24
+    def read(self, section, position, filter={}):
25
+        mongo_client = pymongo.MongoClient(self.get('mongodb', 'host'), int(self.get('mongodb', 'port')))
26
+        db = mongo_client[self.get('mongodb', 'db')]
27
+        db.authenticate(self.get('mongodb', 'user'), self.get('mongodb', 'password'))
28
+
29
+        s = db[section]
30
+        result = s.find_one(filter)
31
+        return result[position]
32
+
33
+
34
+config = Config()

+ 117 - 0
util/pgsql_util.py

@@ -0,0 +1,117 @@
1
+#!/usr/bin/python
2
+# -*- coding:utf-8 -*-
3
+
4
+import sys
5
+import os
6
+from pgdb import Connection
7
+import json
8
+
9
+sys.path.append(os.path.abspath('..'))
10
+from util.config import config
11
+
12
+
13
+def get_conn():
14
+    conn = Connection(database=config.get('pgsql', 'db'), host=config.get('pgsql', 'host'),
15
+                            user=config.get('pgsql', 'user'), password=config.get('pgsql', 'password'))
16
+    return conn
17
+
18
+
19
+def insert(sql):
20
+    try:
21
+        conn = get_conn()
22
+        cur = conn.cursor()
23
+        cur.execute(sql)
24
+        conn.commit()
25
+    except Exception as e:
26
+        print(e)
27
+    finally:
28
+        cur.close()
29
+        conn.close()
30
+
31
+
32
+def update(sql):
33
+    try:
34
+        conn = get_conn()
35
+        conn.execute(sql)
36
+        conn.commit()
37
+    except Exception as e:
38
+        print(e)
39
+    finally:
40
+        conn.close()
41
+
42
+
43
+def get_rows(sql):
44
+    try:
45
+        conn = get_conn()
46
+        return conn.query(sql)
47
+    except Exception as e:
48
+        print(e)
49
+    finally:
50
+        conn.close()
51
+
52
+
53
+def get_total(sql):
54
+    conn = None
55
+    try:
56
+        conn = get_conn()
57
+        result = conn.query(sql)
58
+        return int(result[0]['count'])
59
+    except Exception as e:
60
+        print(e)
61
+    finally:
62
+        conn.close()
63
+
64
+
65
+def demo():
66
+    sql = "select aid,other_info FROM articles where aid = '5afa6e5a-9004-4182-a743-87d77c97b451'"
67
+    rows = get_rows(sql)
68
+    for row in rows:
69
+        print(row)
70
+        other_info = row['other_info']
71
+        print(other_info)
72
+        other_info_json = json.loads(other_info)
73
+        print(other_info_json.get('abstract'))
74
+
75
+
76
+def demo1():
77
+    sql = "select count(*) FROM articles"
78
+    total = get_total(sql)
79
+    print(total)
80
+
81
+
82
+PAGE_SIZE = 50
83
+
84
+
85
+def demo2(offset):
86
+    sql = "select aid,other_info FROM articles order by aid LIMIT %s OFFSET %s"
87
+    update_sql = '''
88
+        UPDATE articles
89
+        set other_info = %s
90
+        WHERE aid = %s
91
+    '''
92
+    rows = get_rows(sql % (PAGE_SIZE, 0))
93
+
94
+    conn = get_conn()
95
+
96
+    for row in rows:
97
+        aid = row['aid']
98
+        other_info = json.loads(row['other_info'])
99
+        if 'abstract' in other_info:
100
+            del other_info['abstract']
101
+        if 'notes' in other_info:
102
+            del other_info['notes']
103
+        if 'content' in other_info:
104
+            del other_info['content']
105
+        print(json.dumps(other_info), aid)
106
+
107
+        conn.execute(update_sql, (json.dumps(other_info), aid))
108
+    conn.close()
109
+
110
+
111
+def demo3():
112
+    for i in range(0, 100, 50):
113
+        demo2(i)
114
+
115
+
116
+if __name__ == "__main__":
117
+    demo1()