forked from itversity/databricks-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07 Copy Data into Delta Tables in Databricks SQL Warehouse.sql
81 lines (58 loc) · 2.02 KB
/
07 Copy Data into Delta Tables in Databricks SQL Warehouse.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
SHOW DATABASES;
SELECT table_name, record
FROM JSON.`dbfs:/FileStore/lms_dl/course_catalog`;
USE lms_bronze; SHOW TABLES;
DROP TABLE IF EXISTS lms_bronze.course_catalog;
CREATE TABLE IF NOT EXISTS lms_bronze.course_catalog (
table_name STRING,
record STRING
);
SELECT * FROM lms_bronze.course_catalog;
SELECT input_file_name() AS input_file_name, table_name, record
FROM JSON.`dbfs:/FileStore/lms_dl/course_catalog`;
-- Run twice, Data will not be copied twice as details about processed files are tracked
COPY INTO lms_bronze.course_catalog
FROM 'dbfs:/FileStore/lms_dl/course_catalog'
FILEFORMAT = JSON
FILES = ('part-00000.json');
SELECT * FROM lms_bronze.course_catalog;
TRUNCATE TABLE lms_bronze.course_catalog;
COPY INTO lms_bronze.course_catalog
FROM 'dbfs:/FileStore/lms_dl/course_catalog'
FILEFORMAT = JSON
FILES = ('part-00000.json', 'part-00001.json');
SELECT * FROM lms_bronze.course_catalog;
TRUNCATE TABLE lms_bronze.course_catalog;
COPY INTO lms_bronze.course_catalog
FROM 'dbfs:/FileStore/lms_dl/course_catalog'
FILEFORMAT = JSON
PATTERN = 'part-0000[0-9].json'
COPY_OPTIONS ('force' = 'true');
SELECT * FROM lms_bronze.course_catalog;
DROP TABLE IF EXISTS lms_bronze.course_catalog;
CREATE TABLE IF NOT EXISTS lms_bronze.course_catalog (
table_name STRING,
record STRING,
created_ts TIMESTAMP
);
SELECT * FROM lms_bronze.course_catalog;
SELECT input_file_name() AS input_file_name,
table_name,
record,
current_timestamp() AS created_ts
FROM JSON.`dbfs:/FileStore/lms_dl/course_catalog`;
COPY INTO lms_bronze.course_catalog
FROM (
SELECT table_name,
record,
current_timestamp() AS created_ts
FROM 'dbfs:/FileStore/lms_dl/course_catalog'
) FILEFORMAT = JSON
PATTERN = 'part-0000[0-9].json';
SELECT * FROM lms_bronze.course_catalog;
WITH icte AS (
SELECT table_name,
from_json(record, 'struct<instructor_id:INT, instructor_name:STRING>') AS record
FROM lms_bronze.course_catalog
WHERE table_name = 'instructors'
) SELECT table_name, record.* FROM icte;