Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
A
airflow-dags-hub
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
唐香港
airflow-dags-hub
Commits
2cf07028
Commit
2cf07028
authored
Jun 29, 2020
by
吴升宇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix pgsql
parent
c9ca7e35
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
5 deletions
+5
-5
create_table_per_day.py
dags/psql/create_table_per_day.py
+5
-5
No files found.
dags/psql/create_table_per_day.py
View file @
2cf07028
# coding: utf-8
# coding: utf-8
from
datetime
import
timedelta
,
datetime
from
datetime
import
timedelta
,
datetime
import
calendar
from
airflow
import
DAG
from
airflow
import
DAG
from
airflow.operators.postgres_operator
import
PostgresOperator
from
airflow.operators.postgres_operator
import
PostgresOperator
...
@@ -23,21 +24,19 @@ dag = DAG(
...
@@ -23,21 +24,19 @@ dag = DAG(
schedule_interval
=
'0 0 1 * * 0'
,
# 每月一号零点执行一次
schedule_interval
=
'0 0 1 * * 0'
,
# 每月一号零点执行一次
)
)
import
datetime
,
calendar
# 获取当前日期
# 获取当前日期
now_time
=
datetime
.
datetime
.
now
()
now_time
=
datetime
.
now
()
# 获取当前时间的星期数和月数
# 获取当前时间的星期数和月数
week
,
days_num
=
calendar
.
monthrange
(
now_time
.
year
,
now_time
.
month
)
week
,
days_num
=
calendar
.
monthrange
(
now_time
.
year
,
now_time
.
month
)
# 获取本月的最后一天
# 获取本月的最后一天
end_day_in_mouth
=
now_time
.
replace
(
day
=
days_num
)
end_day_in_mouth
=
now_time
.
replace
(
day
=
days_num
)
# 获取下月的第一天
# 获取下月的第一天
start_day
=
end_day_in_mouth
+
datetime
.
timedelta
(
days
=
1
)
start_day
=
end_day_in_mouth
+
timedelta
(
days
=
1
)
month
=
start_day
.
strftime
(
'y
%
Ym
%
m'
)
month
=
start_day
.
strftime
(
'y
%
Ym
%
m'
)
# 获取下月的星期数和月数
# 获取下月的星期数和月数
week
,
days_num
=
calendar
.
monthrange
(
start_day
.
year
,
start_day
.
month
)
week
,
days_num
=
calendar
.
monthrange
(
start_day
.
year
,
start_day
.
month
)
# 获取下个月的最后一天
# 获取下个月的最后一天
end_day
=
start_day
+
datetime
.
timedelta
(
days
=
(
days_num
-
1
))
end_day
=
start_day
+
timedelta
(
days
=
(
days_num
-
1
))
end_str
=
str
(
end_day
.
date
())
end_str
=
str
(
end_day
.
date
())
start_str
=
str
(
start_day
.
date
())
start_str
=
str
(
start_day
.
date
())
...
@@ -48,6 +47,7 @@ CREATE TABLE events_{month} partition of events
...
@@ -48,6 +47,7 @@ CREATE TABLE events_{month} partition of events
"""
.
format
(
month
=
month
,
start_str
=
start_str
,
end_str
=
end_str
)
"""
.
format
(
month
=
month
,
start_str
=
start_str
,
end_str
=
end_str
)
print
(
sql1
)
print
(
sql1
)
sql2
=
"""
sql2
=
"""
ALTER TABLE events_{month} ADD CONSTRAINT pk_events_{month} PRIMARY KEY(id);
ALTER TABLE events_{month} ADD CONSTRAINT pk_events_{month} PRIMARY KEY(id);
"""
.
format
(
month
=
month
)
"""
.
format
(
month
=
month
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment