-
Notifications
You must be signed in to change notification settings - Fork 1
/
snowflake.sql
316 lines (282 loc) · 10.2 KB
/
snowflake.sql
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
USE ROLE ACCOUNTADMIN;
CREATE OR REPLACE WAREHOUSE RIDESHARE_INGEST;
ALTER WAREHOUSE RIDESHARE_INGEST SET WAREHOUSE_SIZE='MEDIUM';
CREATE OR REPLACE DATABASE RIDESHARE_DEMO;
ALTER SESSION SET TIMEZONE = 'UTC';
CREATE OR REPLACE ROLE RIDESHARE_INGEST;
GRANT ALL ON WAREHOUSE RIDESHARE_INGEST TO ROLE RIDESHARE_INGEST;
GRANT EXECUTE TASK ON account TO ROLE RIDESHARE_INGEST;
GRANT CREATE INTEGRATION ON account TO ROLE RIDESHARE_INGEST;
GRANT CREATE EXTERNAL VOLUME ON account TO ROLE RIDESHARE_INGEST;
GRANT OWNERSHIP ON DATABASE RIDESHARE_DEMO TO ROLE RIDESHARE_INGEST;
GRANT OWNERSHIP ON SCHEMA RIDESHARE_DEMO.PUBLIC TO ROLE RIDESHARE_INGEST;
CREATE OR REPLACE USER RIDESHARE_INGEST PASSWORD='RIDESHARE_INGEST' LOGIN_NAME='RIDESHARE_INGEST' MUST_CHANGE_PASSWORD=FALSE, DISABLED=FALSE, DEFAULT_WAREHOUSE='RIDESHARE_INGEST', DEFAULT_NAMESPACE='RIDESHARE_DEMO.PUBLIC', DEFAULT_ROLE='RIDESHARE_INGEST';
GRANT ROLE RIDESHARE_INGEST TO USER RIDESHARE_INGEST;
-- change to your current logged in user
GRANT ROLE RIDESHARE_INGEST TO USER BHARAT;
ALTER USER RIDESHARE_INGEST SET RSA_PUBLIC_KEY='YOUR-PUBLIC-KEY';
USE ROLE RIDESHARE_INGEST;
USE DATABASE RIDESHARE_DEMO;
-- query kafka connector created tables
SELECT * FROM RIDERS_STAGE;
SELECT * FROM DRIVERS_STAGE;
SELECT * FROM TRIPS_STAGE;
-- deduplicated riders table
CREATE OR REPLACE TABLE RIDERS (
ID VARCHAR(255) NOT NULL,
FIRST_NAME VARCHAR(255),
LAST_NAME VARCHAR(255),
EMAIL VARCHAR(255),
PHONE_NUMBER VARCHAR(50),
DATE_OF_BIRTH TIMESTAMP_NTZ(6),
CREATED_AT TIMESTAMP_NTZ(6),
LOCATION_CITY VARCHAR(255),
LOCATION_LAT FLOAT,
LOCATION_LONG FLOAT,
STATUS VARCHAR(50),
PRIMARY KEY (ID)
);
-- Create a stream on the RIDERS_STAGE table
CREATE OR REPLACE STREAM RIDERS_STREAM ON TABLE RIDERS_STAGE;
-- Create a task to merge changes into the RIDERS table
CREATE OR REPLACE TASK MERGE_RIDERS_TASK
WAREHOUSE = RIDESHARE_INGEST
SCHEDULE = '1 minute'
WHEN
SYSTEM$STREAM_HAS_DATA('RIDERS_STREAM')
AS
MERGE INTO RIDESHARE_DEMO.PUBLIC.RIDERS AS target
USING (
SELECT
ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
PHONE_NUMBER,
TRY_TO_TIMESTAMP_NTZ(DATE_OF_BIRTH) AS DATE_OF_BIRTH,
TRY_TO_TIMESTAMP_NTZ(CREATED_AT) AS CREATED_AT,
LOCATION_CITY,
LOCATION_LAT,
LOCATION_LONG,
STATUS,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY TRY_TO_TIMESTAMP(CREATED_AT) DESC) AS rn
FROM RIDERS_STREAM
) AS source
ON target.ID = source.ID
WHEN MATCHED AND source.rn = 1 THEN
UPDATE SET
target.FIRST_NAME = source.FIRST_NAME,
target.LAST_NAME = source.LAST_NAME,
target.EMAIL = source.EMAIL,
target.PHONE_NUMBER = source.PHONE_NUMBER,
target.DATE_OF_BIRTH = source.DATE_OF_BIRTH,
target.CREATED_AT = source.CREATED_AT,
target.LOCATION_CITY = source.LOCATION_CITY,
target.LOCATION_LAT = source.LOCATION_LAT,
target.LOCATION_LONG = source.LOCATION_LONG,
target.STATUS = source.STATUS
WHEN NOT MATCHED AND source.rn = 1 THEN
INSERT (ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, DATE_OF_BIRTH, CREATED_AT, LOCATION_CITY, LOCATION_LAT, LOCATION_LONG, STATUS)
VALUES (source.ID, source.FIRST_NAME, source.LAST_NAME, source.EMAIL, source.PHONE_NUMBER, source.DATE_OF_BIRTH, source.CREATED_AT, source.LOCATION_CITY, source.LOCATION_LAT, source.LOCATION_LONG, source.STATUS);
-- Start the task
ALTER TASK MERGE_RIDERS_TASK RESUME;
-- deduplicated drivers table
CREATE OR REPLACE TABLE DRIVERS (
ID VARCHAR(255) NOT NULL,
FIRST_NAME VARCHAR(255),
LAST_NAME VARCHAR(255),
EMAIL VARCHAR(255),
PHONE_NUMBER VARCHAR(50),
DATE_OF_BIRTH TIMESTAMP_NTZ(6),
CREATED_AT TIMESTAMP_NTZ(6),
LOCATION_CITY VARCHAR(255),
LOCATION_LAT FLOAT,
LOCATION_LONG FLOAT,
STATUS VARCHAR(50),
PRIMARY KEY (ID)
);
-- Create a stream on the DRIVERS_STAGE table
CREATE OR REPLACE STREAM DRIVERS_STREAM ON TABLE DRIVERS_STAGE;
-- Create a task to merge changes into the DRIVERS table
CREATE OR REPLACE TASK MERGE_DRIVERS_TASK
WAREHOUSE = RIDESHARE_INGEST
SCHEDULE = '1 minute'
WHEN
SYSTEM$STREAM_HAS_DATA('DRIVERS_STREAM')
AS
MERGE INTO RIDESHARE_DEMO.PUBLIC.DRIVERS AS target
USING (
SELECT
ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
PHONE_NUMBER,
TRY_TO_TIMESTAMP_NTZ(DATE_OF_BIRTH) AS DATE_OF_BIRTH,
TRY_TO_TIMESTAMP_NTZ(CREATED_AT) AS CREATED_AT,
LOCATION_CITY,
LOCATION_LAT,
LOCATION_LONG,
STATUS,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY TRY_TO_TIMESTAMP(CREATED_AT) DESC) AS rn
FROM DRIVERS_STREAM
) AS source
ON target.ID = source.ID
WHEN MATCHED AND source.rn = 1 THEN
UPDATE SET
target.FIRST_NAME = source.FIRST_NAME,
target.LAST_NAME = source.LAST_NAME,
target.EMAIL = source.EMAIL,
target.PHONE_NUMBER = source.PHONE_NUMBER,
target.DATE_OF_BIRTH = source.DATE_OF_BIRTH,
target.CREATED_AT = source.CREATED_AT,
target.LOCATION_CITY = source.LOCATION_CITY,
target.LOCATION_LAT = source.LOCATION_LAT,
target.LOCATION_LONG = source.LOCATION_LONG,
target.STATUS = source.STATUS
WHEN NOT MATCHED AND source.rn = 1 THEN
INSERT (ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, DATE_OF_BIRTH, CREATED_AT, LOCATION_CITY, LOCATION_LAT, LOCATION_LONG, STATUS)
VALUES (source.ID, source.FIRST_NAME, source.LAST_NAME, source.EMAIL, source.PHONE_NUMBER, source.DATE_OF_BIRTH, source.CREATED_AT, source.LOCATION_CITY, source.LOCATION_LAT, source.LOCATION_LONG, source.STATUS);
-- Start the task
ALTER TASK MERGE_DRIVERS_TASK RESUME;
-- deduplicated trips table
CREATE OR REPLACE TABLE TRIPS (
ID VARCHAR(255) NOT NULL,
DRIVER_ID VARCHAR(255),
RIDER_ID VARCHAR(255),
STATUS VARCHAR(50),
REQUEST_TIME TIMESTAMP_NTZ(6),
ACCEPT_TIME TIMESTAMP_NTZ(6),
PICKUP_TIME TIMESTAMP_NTZ(6),
DROPOFF_TIME TIMESTAMP_NTZ(6),
FARE INTEGER,
DISTANCE FLOAT,
PICKUP_LAT FLOAT,
PICKUP_LONG FLOAT,
DROPOFF_LAT FLOAT,
DROPOFF_LONG FLOAT,
CITY VARCHAR(255),
PRIMARY KEY (ID)
);
-- Create a stream on the TRIPS_STAGE table
CREATE OR REPLACE STREAM TRIPS_STREAM ON TABLE TRIPS_STAGE;
-- Create a task to merge changes into the TRIPS table
CREATE OR REPLACE TASK MERGE_TRIPS_TASK
WAREHOUSE = RIDESHARE_INGEST
SCHEDULE = '1 minute'
WHEN
SYSTEM$STREAM_HAS_DATA('TRIPS_STREAM')
AS
MERGE INTO RIDESHARE_DEMO.PUBLIC.TRIPS AS target
USING (
SELECT
ID,
DRIVER_ID,
RIDER_ID,
STATUS,
TRY_TO_TIMESTAMP_NTZ(REQUEST_TIME) AS REQUEST_TIME,
TRY_TO_TIMESTAMP_NTZ(ACCEPT_TIME) AS ACCEPT_TIME,
TRY_TO_TIMESTAMP_NTZ(PICKUP_TIME) AS PICKUP_TIME,
TRY_TO_TIMESTAMP_NTZ(DROPOFF_TIME) AS DROPOFF_TIME,
FARE,
DISTANCE,
PICKUP_LAT,
PICKUP_LONG,
DROPOFF_LAT,
DROPOFF_LONG,
CITY,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY TRY_TO_TIMESTAMP_NTZ(RECORD_METADATA:CreateTime::STRING) DESC) AS rn
FROM TRIPS_STREAM
) AS source
ON target.ID = source.ID
WHEN MATCHED AND source.rn = 1 THEN
UPDATE SET
target.DRIVER_ID = source.DRIVER_ID,
target.RIDER_ID = source.RIDER_ID,
target.STATUS = source.STATUS,
target.REQUEST_TIME = source.REQUEST_TIME,
target.ACCEPT_TIME = source.ACCEPT_TIME,
target.PICKUP_TIME = source.PICKUP_TIME,
target.DROPOFF_TIME = source.DROPOFF_TIME,
target.FARE = source.FARE,
target.DISTANCE = source.DISTANCE,
target.PICKUP_LAT = source.PICKUP_LAT,
target.PICKUP_LONG = source.PICKUP_LONG,
target.DROPOFF_LAT = source.DROPOFF_LAT,
target.DROPOFF_LONG = source.DROPOFF_LONG,
target.CITY = source.CITY
WHEN NOT MATCHED AND source.rn = 1 THEN
INSERT (ID, DRIVER_ID, RIDER_ID, STATUS, REQUEST_TIME, ACCEPT_TIME, PICKUP_TIME, DROPOFF_TIME, FARE, DISTANCE, PICKUP_LAT, PICKUP_LONG, DROPOFF_LAT, DROPOFF_LONG, CITY)
VALUES (source.ID, source.DRIVER_ID, source.RIDER_ID, source.STATUS, source.REQUEST_TIME, source.ACCEPT_TIME, source.PICKUP_TIME, source.DROPOFF_TIME, source.FARE, source.DISTANCE, source.PICKUP_LAT, source.PICKUP_LONG, source.DROPOFF_LAT, source.DROPOFF_LONG, source.CITY);
-- Start the task
ALTER TASK MERGE_TRIPS_TASK RESUME;
-- verify deduplicated tables
SELECT COUNT(*) FROM RIDERS;
SELECT COUNT(*) FROM DRIVERS;
SELECT COUNT(*) FROM TRIPS;
-- (optional) ingest trips csv from s3
-- follow the instructions here: https://docs.snowflake.com/en/user-guide/data-load-s3
CREATE STORAGE INTEGRATION rideshare_s3_int
TYPE = EXTERNAL_STAGE
STORAGE_PROVIDER = 'S3'
ENABLED = TRUE
STORAGE_AWS_ROLE_ARN = 'arn:aws:iam::'
STORAGE_ALLOWED_LOCATIONS = ('s3://');
USE ROLE ACCOUNTADMIN;
GRANT USAGE ON INTEGRATION rideshare_s3_int TO ROLE RIDESHARE_INGEST;
USE ROLE RIDESHARE_INGEST;
DESC INTEGRATION rideshare_s3_int;
CREATE OR REPLACE STAGE rideshare_s3_stage
STORAGE_INTEGRATION = rideshare_s3_int
URL = 's3://';
SHOW STAGES;
COPY INTO trips
FROM @rideshare_s3_stage
FILES = ( 'trips.csv' );
SELECT COUNT(*) FROM trips;
-- create iceberg volume on s3
-- following the instructions here: https://docs.snowflake.com/en/user-guide/tables-iceberg-configure-external-volume
CREATE OR REPLACE EXTERNAL VOLUME s3_rideshare_iceberg_volume
STORAGE_LOCATIONS = (
(
NAME = 's3_rideshare_iceberg_volume'
STORAGE_PROVIDER = 'S3'
STORAGE_BASE_URL = 's3://'
STORAGE_AWS_ROLE_ARN = 'arn:aws:iam::'
STORAGE_AWS_EXTERNAL_ID = ''
)
);
DESC EXTERNAL VOLUME s3_rideshare_iceberg_volume;
-- create iceberg trips table
CREATE OR REPLACE ICEBERG TABLE trips_ice LIKE trips
CATALOG = 'SNOWFLAKE'
EXTERNAL_VOLUME = 's3_rideshare_iceberg_volume'
BASE_LOCATION = 'iceberg';
-- copy trips into iceberg table
INSERT INTO trips_ice
SELECT * FROM trips;
SELECT COUNT(*) FROM trips_ice;
-- cleanup query to remove any orphaned trips and reset the riders and drivers tables
DELETE FROM RIDERS;
DELETE FROM DRIVERS;
DELETE FROM TRIPS WHERE status != 'completed';
-- sample queries
SELECT 'trips' as entity, status, COUNT(*) as count
FROM trips
GROUP BY status
UNION ALL
SELECT 'riders' as entity, status, COUNT(*) as count
FROM riders
GROUP BY status
UNION ALL
SELECT 'drivers' as entity, status, COUNT(*) as count
FROM drivers
GROUP BY status
ORDER BY entity, status;
-- cleanup demo
USE ROLE ACCOUNTADMIN;
DROP WAREHOUSE IF EXISTS RIDESHARE_INGEST;
DROP USER IF EXISTS RIDESHARE_INGEST;
DROP ROLE IF EXISTS RIDESHARE_INGEST;
DROP DATABASE IF EXISTS RIDESHARE_DEMO;
DROP EXTERNAL VOLUME s3_rideshare_iceberg_volume;