-
Notifications
You must be signed in to change notification settings - Fork 0
/
genTable_sql_788705089.py
189 lines (174 loc) · 7.68 KB
/
genTable_sql_788705089.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import json
from random import random
USERS_NUM = 10000
ARTICLES_NUM = 200000
READS_NUM = 1000000
uid_region = {}
aid_lang = {}
# Beijing:60% Hong Kong:40%
# en:20% zh:80%
# 20 depts
# 3 roles
# 50 tags
# 0~99 credits
def gen_an_user (i):
timeBegin = 1506328859000
user = {}
user["timestamp"] = str(timeBegin + i)
user["uid"] = str(i)
user["name"] = "user%d" % i
user["gender"] = "male" if random() > 0.33 else "female"
user["email"] = "email%d" % i
user["phone"] = "phone%d" % i
user["dept"] = "dept%d" % int(random() * 20)
user["grade"] = "grade%d" % int(random() * 4 + 1)
user["language"] = "en" if random() > 0.8 else "zh"
user["region"] = "Beijing" if random() > 0.4 else "Hong Kong"
user["role"] = "role%d" % int(random() * 3)
user["preferTags"] = "tags%d" % int(random() * 50)
user["obtainedCredits"] = str(int(random() * 100))
uid_region[user["uid"]] = user["region"]
return "(" + \
"\"" + user["timestamp"] + "\", " + \
"\"" + user["uid"] + "\", " + \
"\"" + user["name"] + "\", " + \
"\"" + user["gender"] + "\", " + \
"\"" + user["email"] + "\", " + \
"\"" + user["phone"] + "\", " + \
"\"" + user["dept"] + "\", " + \
"\"" + user["grade"] + "\", " + \
"\"" + user["language"] + "\", " + \
"\"" + user["region"] + "\", " + \
"\"" + user["role"] + "\", " + \
"\"" + user["preferTags"] + "\", " + \
"\"" + user["obtainedCredits"] + "\")";
# science:45% technology:55%
# en:50% zh:50%
# 50 tags
# 2000 authors
def gen_an_article (i):
timeBegin = 1506000000000
article = {}
article["timestamp"] = str(timeBegin + i)
article["aid"] = str(i)
article["title"] = "title%d" % i
article["category"] = "science" if random() > 0.55 else "technology"
article["abstract"] = "abstract of article %d" % i
article["articleTags"] = "tags%d" % int(random() * 50)
article["authors"] = "author%d" % int(random() * 2000)
article["language"] = "en" if random() > 0.5 else "zh"
article["text"] = "text %d's location on hdfs" % i
article["image"] = "image %d's location on hdfs" % i
article["video"] = "video %d's location on hdfs" % i
aid_lang[article["aid"]] = article["language"]
return "(" + \
"\"" + article["timestamp"] + "\", " + \
"\"" + article["aid"] + "\", " + \
"\"" + article["title"] + "\", " + \
"\"" + article["category"] + "\", " + \
"\"" + article["abstract"] + "\", " + \
"\"" + article["articleTags"] + "\", " + \
"\"" + article["authors"] + "\", " + \
"\"" + article["language"] + "\", " + \
"\"" + article["text"] + "\", " + \
"\"" + article["image"] + "\", " + \
"\"" + article["video"] + "\")";
# user in Beijing read/agree/comment/share an english article with the probability 0.6/0.2/0.2/0.1
# user in Hong Kong read/agree/comment/share an Chinese article with the probability 0.8/0.2/0.2/0.1
p = {}
p["Beijing"+"en"] = [0.6,0.2,0.2,0.1]
p["Beijing"+"zh"] = [1,0.3,0.3,0.2]
p["Hong Kong"+"en"] = [1,0.3,0.3,0.2]
p["Hong Kong"+"zh"] = [0.8,0.2,0.2,0.1]
def gen_an_read (i):
timeBegin = 1506332297000
read = {}
read["timestamp"] = str(timeBegin + i*10000)
read["uid"] = str(int(random() * USERS_NUM))
read["aid"] = str(int(random() * ARTICLES_NUM))
region = uid_region[read["uid"]]
lang = aid_lang[read["aid"]]
ps = p[region + lang]
if (random() > ps[0]):
# read["readOrNot"] = "0";
return gen_an_read (i)
else:
read["readOrNot"] = "1"
read["readTimeLength"] = str(int(random() * 100))
read["readSequence"] = str(int(random() * 4))
read["agreeOrNot"] = "1" if random() < ps[1] else "0"
read["commentOrNot"] = "1" if random() < ps[2] else "0"
read["shareOrNot"] = "1" if random() < ps[3] else "0"
read["commentDetail"] = "comments to this article: (" + read["uid"] + "," + read["aid"] + ")"
return "(" + \
"\"" + read["timestamp"] + "\", " + \
"\"" + read["uid"] + "\", " + \
"\"" + read["aid"] + "\", " + \
"\"" + read["readOrNot"] + "\", " + \
"\"" + read["readTimeLength"] + "\", " + \
"\"" + read["readSequence"] + "\", " + \
"\"" + read["agreeOrNot"] + "\", " + \
"\"" + read["commentOrNot"] + "\", " + \
"\"" + read["shareOrNot"] + "\", " + \
"\"" + read["commentDetail"] + "\")"
with open("user.sql", "w+") as f:
f.write("DROP TABLE IF EXISTS `user`;\n")
f.write("CREATE TABLE `user` (\n" + \
" `timestamp` char(14) DEFAULT NULL,\n" + \
" `uid` char(5) DEFAULT NULL,\n" + \
" `name` char(9) DEFAULT NULL,\n" + \
" `gender` char(7) DEFAULT NULL,\n" + \
" `email` char(10) DEFAULT NULL,\n" + \
" `phone` char(10) DEFAULT NULL,\n" + \
" `dept` char(9) DEFAULT NULL,\n" + \
" `grade` char(7) DEFAULT NULL,\n" + \
" `language` char(3) DEFAULT NULL,\n" + \
" `region` char(10) DEFAULT NULL,\n" + \
" `role` char(6) DEFAULT NULL,\n" + \
" `preferTags` char(7) DEFAULT NULL,\n" + \
" `obtainedCredits` char(3) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n")
f.write("LOCK TABLES `user` WRITE;\n")
f.write("INSERT INTO `user` VALUES\n")
for i in range (USERS_NUM-1):
f.write(" " + gen_an_user(i) + ",\n")
f.write(" " + gen_an_user(USERS_NUM-1) + ";\n")
f.write("UNLOCK TABLES;\n\n\n")
with open("article.sql", "w+") as f:
f.write("DROP TABLE IF EXISTS `article`;\n")
f.write("CREATE TABLE `article` (\n" + \
" `timestamp` char(14) DEFAULT NULL,\n" + \
" `aid` char(7) DEFAULT NULL,\n" + \
" `title` char(15) DEFAULT NULL,\n" + \
" `category` char(11) DEFAULT NULL,\n" + \
" `abstract` char(30) DEFAULT NULL,\n" + \
" `articleTags` char(14) DEFAULT NULL,\n" + \
" `authors` char(13) DEFAULT NULL,\n" + \
" `language` char(3) DEFAULT NULL,\n" + \
" `text` char(31) DEFAULT NULL,\n" + \
" `image` char(32) DEFAULT NULL,\n" + \
" `video` char(32) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n")
f.write("LOCK TABLES `article` WRITE;\n")
f.write("INSERT INTO `article` VALUES\n")
for i in range (ARTICLES_NUM-1):
f.write(" " + gen_an_article(i) + ",\n")
f.write(" " + gen_an_article(ARTICLES_NUM-1) + ";\n")
f.write("UNLOCK TABLES;\n\n\n")
with open("user_read.sql", "w+") as f:
f.write("DROP TABLE IF EXISTS `user_read`;\n")
f.write("CREATE TABLE `user_read` (\n" + \
" `timestamp` char(14) DEFAULT NULL,\n" + \
" `uid` char(5) DEFAULT NULL,\n" + \
" `aid` char(7) DEFAULT NULL,\n" + \
" `readOrNot` char(2) DEFAULT NULL,\n" + \
" `readTimeLength` char(3) DEFAULT NULL,\n" + \
" `readSequence` char(2) DEFAULT NULL,\n" + \
" `agreeOrNot` char(2) DEFAULT NULL,\n" + \
" `commentOrNot` char(2) DEFAULT NULL,\n" + \
" `shareOrNot` char(2) DEFAULT NULL,\n" + \
" `commentDetail` char(100) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n")
f.write("LOCK TABLES `user_read` WRITE;\n")
f.write("INSERT INTO `user_read` VALUES\n")
for i in range (READS_NUM-1):
f.write(" " + gen_an_read(i) + ",\n")
f.write(" " + gen_an_read(READS_NUM-1) + ";\n")
f.write("UNLOCK TABLES;\n\n\n")