【发布时间】:2021-09-18 12:29:02
【问题描述】:
我需要做以下事情:
-
使用第三方 api 抓取推文并将推文内容和其他详细信息存储在 mysql 数据库中。
-
在 db 表中添加新行之前,我需要获取推文来源位置的 temp 并在表中添加 temp 值。
为了完成第二个任务,我编写了 2 个 python 脚本。第一个 python 脚本将 mysql 触发器添加到数据库中。在 mysql 触发器内部,我在阅读 http://crazytechthoughts.blogspot.sg/2011/12/call-external-program-from-mysql.html 后使用 sys.eval() 调用第二个使用 api 获取温度数据的 python 脚本。
两个脚本单独工作都非常好,但触发器无法从第二个脚本中获取温度数据。我该如何解决?
Script to add trigger
def create_Triggers(self):
try:
mysql_trig = """
CREATE TRIGGER mysql_Trigger
BEFORE INSERT ON tweets FOR EACH ROW BEGIN
DECLARE loc CHAR(255); DECLARE result Double; DECLARE city CHAR(255);
DECLARE station CHAR(255); DECLARE dtime CHAR(255); SET city = NEW.city;
SET station = "OYSN:9:YE"; SET dtime = NEW.dtime;
SET loc = CONCAT('python D:test case scripts/Weather_enrichment_triggers/weather_enrichment.py',city,station,dtime);
SET result = sys_eval(loc);
SET NEW.Temperature = result;
END;
"""
self.curr.execute(mysql_trig)
print("trigger executed")
self.connection.commit()
#self.curr.execute(postgre_trig)
#self.connection.commit()
except Exception as e:
print(e)
def test_triggers(self, query):
self.curr.execute(query)
self.connection.commit()
Script to fetch temp data(called from inside the trigger)
city = sys.argv[1]
station = sys.argv[2]
dtime = sys.argv[3]
def weather_info(city,station,dtime):
#Get weather information for a given city and date
template_request = "https://api.weather.com/v1/location/{station}/observations/historical.json?apiKey=apikey&units=m&startDate={start_date}&endDate={end_date}"
df_header = ["City", "Year", "Month", "Day", "Hour", "Temperature(C)", "Condition"]
def get_weather_data(city, year, month, day, station):
start_date = "%d%02d%02d" % (year, month, day)
end_date = "%d%02d%02d" % (year, month, day)
request = template_request.format(station=station, start_date=start_date, end_date=end_date)
request_data = json.loads(requests.get(request).content)
weather_data = []
last_dt = None
for observation in request_data["observations"]:
dt = datetime.fromtimestamp(observation["valid_time_gmt"]+3600)
if last_dt and dt.hour > (last_dt.hour + 1):
last_row = deepcopy(weather_data[-1])
last_row[4] = last_row[4]+1
weather_data.append(last_row)
weather_data.append([city, year, month, dt.day, dt.hour, observation["temp"], observation["wx_phrase"]])
last_dt = dt
return weather_data
dtime = datetime.strptime(dtime, '%Y-%m-%d %H:%M:%S%z')
data = get_weather_data(city, dtime.year, dtime.month, dtime.day, station)
weather_df = pd.DataFrame(data, columns=df_header).drop_duplicates(subset=["City", "Year", "Month", "Day", "Hour"])
avg = (weather_df["Temperature(C)"].values).mean()
weather_df = pd.DataFrame()
return float(avg)
temp = weather_info(city, station, dtime)
temp = str(temp)
sys.stdout.write(temp)
【问题讨论】:
-
不要为此使用mysql触发器,在没有任何触发器的情况下使用python。
-
是的,我同意你的观点,但我手头的任务是将 mysql 的性能与另一个数据库进行比较,因此我不得不使用 mysql。
-
您可以使用 mysql 作为数据存储,但不要使用触发器。
标签: python mysql user-defined-functions