-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_dataflow_dataset_import.R
196 lines (153 loc) · 5.5 KB
/
post_dataflow_dataset_import.R
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
190
191
192
193
194
195
196
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#|Script to import spatially-joined data from BigQuery|#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#########
#Library#
#########
library(bigrquery)
project <- "mlab-sandbox"
load("legislative_mlab")
##################
#BigQuery queries#
##################
#`oti_usob.D_deserts_final_minrtt_state`
query_agg_ip<-"#standardSQL
SELECT day,
APPROX_QUANTILES(med_rtt, 1000)[OFFSET(500)] as med_rtt,
APPROX_QUANTILES(med_speed, 1000)[OFFSET(500)] as med_speed,
SUM(count_ip) as tract_test_counts, tract
FROM
`oti_usob.dataflow_county_final_copy`
GROUP BY
day, tract"
query_agg_up<-"#standardSQL
SELECT day,
APPROX_QUANTILES(upload_med_speed, 1000)[OFFSET(500)] as upload_med_speed,
SUM(count_ip) as tract_test_counts, tract
FROM
`oti_usob.test_UL`
GROUP BY
day, tract"
query_house<-"#standardSQL
SELECT day,
APPROX_QUANTILES(med_rtt, 1000)[OFFSET(500)] as med_rtt,
APPROX_QUANTILES(med_speed, 1000)[OFFSET(500)] as med_speed,
SUM(count_ip) as tract_test_counts,
client_lat, client_lon, tract
FROM
`oti_usob.dataflow_lower_test_final`
GROUP BY
client_lat, client_lon, day, tract"
query_house_up<-"#standardSQL
SELECT day,
APPROX_QUANTILES(upload_med_speed, 1000)[OFFSET(500)] as med_up_speed,
tract
FROM
`oti_usob.mixed_house`
GROUP BY
day, tract"
query_senate<-"#standardSQL
SELECT day,
APPROX_QUANTILES(med_rtt, 1000)[OFFSET(500)] as med_rtt,
APPROX_QUANTILES(med_speed, 1000)[OFFSET(500)] as med_speed,
APPROX_QUANTILES(upload_med_speed, 1000)[OFFSET(500)] as med_up_speed,
SUM(count_ip) as tract_test_counts,
client_lat, client_lon, tract
FROM
`oti_usob.dataflow_upper_test_final`
GROUP BY
client_lat, client_lon, day, tract"
query_senate_up<-"#standardSQL
SELECT day,
APPROX_QUANTILES(upload_med_speed, 1000)[OFFSET(500)] as med_up_speed,
senate_tract
FROM
`oti_usob.UL_test_senate_joined`
GROUP BY
day, senate_tract"
query_477<-"#standardSQL
SELECT
IF(CHAR_LENGTH(CAST(Census_Block_FIPS_CODE AS STRING))=14,SUBSTR(CONCAT('0',CAST(Census_Block_FIPS_CODE AS STRING)),1,11),
SUBSTR(CAST(Census_Block_FIPS_CODE AS STRING),1,11)) AS FIPS_tract,
COUNT(DISTINCT DBA_Name) AS num_con_prov,
APPROX_QUANTILES(Max_Advertised_Downstream_Speed__mbps_,1000)[OFFSET(500)] AS med_dl,
APPROX_QUANTILES( Max_Advertised_Upstream_Speed__mbps_ ,1000)[OFFSET(500)] AS med_ul
FROM
`TABLE`
WHERE
Consumer = '1'
GROUP BY
FIPS_tract"
query_477_prov<-"#standardSQL
SELECT
APPROX_QUANTILES(Max_Advertised_Downstream_Speed__mbps_,
1000)[ OFFSET (500)] AS med_dl,
APPROX_QUANTILES(Max_Advertised_Upstream_Speed__mbps_,
1000)[ OFFSET (500)] AS med_ul,
Provider_name,
SUBSTR(IF(CHAR_LENGTH(CAST(Census_Block_FIPS_CODE AS STRING))=14,SUBSTR(CONCAT('0',CAST(Census_Block_FIPS_CODE AS STRING)),1,11),
SUBSTR(CAST(Census_Block_FIPS_CODE AS STRING),1,11)),1,5) AS FIPS_tract
FROM
`TABLE`
WHERE
Consumer = '1'
GROUP BY
Provider_name,
FIPS_tract
"
tract_count_query <- "#standardSQL
SELECT DATE_TRUNC(partition_date, MONTH) AS day, COUNT(connection_spec.client_ip) as count_ip, tract
FROM
`oti_usob.ndt_spatial`
GROUP BY
tract, day"
#`thieme.dataflow_upper_final_int`
senate_count_query <- "#standardSQL
SELECT DATE_TRUNC(partition_date, MONTH) AS day, COUNT(connection_spec.client_ip) as count_ip, tract
FROM
`oti_usob.dataflow_upper_final_int`
GROUP BY
tract, day"
house_count_query <- "#standardSQL
SELECT DATE_TRUNC(partition_date, MONTH) AS day, COUNT(connection_spec.client_ip) as count_ip, tract
FROM
`oti_usob.dataflow_lower_test_int_final`
GROUP BY
tract, day"
#####
#I/O#
#####
todo_copies <- load_time_chunks(query_agg_ip)
todo_copies_up <- load_time_chunks(query_agg_up)
tract_count_count<-query_exec(tract_count_query, project = project, use_legacy_sql=FALSE, max_pages = Inf)
D<-left_join(todo_copies, tract_count_count, by = c("day", "tract"))
D_477_2017_jun_prov <- load_477_data(query_477_prov, "oti_usob.477_jun_2017")
D_477_2016_dec_prov <- load_477_data(query_477_prov, "oti_usob.477_dec_2016")
D_477_2016_jun_prov <- load_477_data(query_477_prov, "oti_usob.477_jun_2016")
D_477_2015_dec_prov <- load_477_data(query_477_prov, "oti_usob.477_dec_2015")
D_477_2015_jun_prov <- load_477_data(query_477_prov, "oti_usob.477_jun_2015")
D_477_2014_dec_prov <- load_477_data(query_477_prov, "oti_usob.477_dec_2014")
D_state_house <- load_time_chunks(query_house)
D_state_house_up <- load_time_chunks(query_house_up)
house_count<-query_exec(house_count_query, project = project, use_legacy_sql=FALSE, max_pages = Inf)
names(D_state_house)[8]<-"GEOID"
names(house_count)[3]<-"GEOID"
D_state_senate <- load_time_chunks(query_senate)
D_state_senate_up <- load_time_chunks(query_senate_up)
senate_count<-query_exec(senate_count_query,project = project, use_legacy_sql=FALSE, max_pages = Inf)
names(D_state_senate)[8]<-"GEOID"
names(senate_count)[3]<-"GEOID"
save(D, file="MLab_data_census_tract1")
save(todo_copies_up, file = "MLab_data_census_tract1_up" )
save(D_477_2017_jun_prov, file="D_477_2017_jun_prov")
save(D_477_2016_dec_prov, file="D_477_2016_dec_prov")
save(D_477_2016_jun_prov, file="D_477_2016_jun_prov")
save(D_477_2015_dec_prov, file="D_477_2015_dec_prov")
save(D_477_2015_jun_prov, file="D_477_2015_jun_prov")
save(D_477_2014_dec_prov, file="D_477_2014_dec_prov")
save(D_state_house, file="MLab_data_state_house_2")
save(D_state_house_up, file="MLab_data_state_house_up")
save(house_count, file="house_counts_2")
save(D_state_senate, file="MLab_data_state_senate_2")
save(D_state_senate_up, file="MLab_data_state_senate_up")
save(senate_count, file="senate_counts_2")