Repository navigation
Expand file tree
/
Copy pathfastloadjson.R
More file actions
151 lines (115 loc) · 4.87 KB
/
Copy pathfastloadjson.R
File metadata and controls
151 lines (115 loc) · 4.87 KB
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
# Copyright 2026 by Teradata Corporation. All Rights Reserved.
# This sample program demonstrates how to FastLoad a JSON file.
# It also illustrates dual treatment of a nested JSON object: the raw JSON
# string is stored verbatim in the "address" column, while the flattened
# sub-field "city" is stored in a separate column.
options (warning.length = 8000L)
options (width = 1000)
main <- function () {
con <- DBI::dbConnect (teradatasql::TeradataDriver (), host = "whomooz", user = "guest", password = "please")
tryCatch ({
sTableName <- "FastLoadJSON"
tryCatch ({
sRequest <- paste0 ("DROP TABLE ", sTableName)
cat (paste0 (sRequest, "\n"))
DBI::dbExecute (con, sRequest)
}, error = function (e) {
cat (paste0 ("Ignoring ", strsplit (trimws (e), "\n") [[1]][[1]], "\n"))
})
tryCatch ({
sRequest <- paste0 ("DROP TABLE ", sTableName, "_ERR_1")
cat (paste0 (sRequest, "\n"))
DBI::dbExecute (con, sRequest)
}, error = function (e) {
cat (paste0 ("Ignoring ", strsplit (trimws (e), "\n") [[1]][[1]], "\n"))
})
tryCatch ({
sRequest <- paste0 ("DROP TABLE ", sTableName, "_ERR_2")
cat (paste0 (sRequest, "\n"))
DBI::dbExecute (con, sRequest)
}, error = function (e) {
cat (paste0 ("Ignoring ", strsplit (trimws (e), "\n") [[1]][[1]], "\n"))
})
# Each record has three top-level keys:
# id -- scalar integer
# name -- scalar string
# address -- nested object; its raw JSON string maps to the "address" column
# and its sub-field "city" maps to the "city" column.
sJSONData <- paste0 (
'[',
'{"id":1,"name":"Alice","address":{"city":"Boston"}},',
'{"id":2,"name":"Bob","address":{"city":"Austin"}},',
'{"id":3,"name":"Carol","address":{"city":"Chicago"}},',
'{"id":4,"name":"Dave","address":{"city":"Denver"}},',
'{"id":5,"name":"Erin","address":{"city":"Eugene"}},',
'{"id":6,"name":"Frank","address":{"city":"Fresno"}},',
'{"id":7,"name":"Grace","address":{"city":"Houston"}},',
'{"id":8,"name":"Hank","address":{"city":"Irvine"}},',
'{"id":9,"name":"Iris","address":{"city":"Jacksonville"}}',
']')
jsonFileName <- "dataR.json"
cat (paste0 ("Writing ", jsonFileName, "\n"))
writeLines (sJSONData, jsonFileName, useBytes = TRUE)
tryCatch ({
sRequest <- paste0 ("CREATE TABLE ", sTableName, " (id INTEGER, name VARCHAR(20), address VARCHAR(200), city VARCHAR(20)) UNIQUE PRIMARY INDEX (id)")
cat (paste0 (sRequest, "\n"))
DBI::dbExecute (con, sRequest)
tryCatch ({
cat ("DBI::dbBegin\n")
DBI::dbBegin (con)
tryCatch ({
sInsert <- paste0 (
"{fn teradata_require_fastload}",
"{fn teradata_read_json(", jsonFileName, ")}",
"INSERT INTO ", sTableName, " (id, name, address, city) VALUES (?, ?, ?, ?)")
cat (paste0 (sInsert, "\n"))
DBI::dbExecute (con, sInsert)
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_get_warnings}", sInsert)
cat (paste0 (sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_get_errors}", sInsert)
cat (paste0 (sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_logon_sequence_number}", sInsert)
cat (paste0 (sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
}, finally = {
cat ("DBI::dbCommit\n")
DBI::dbCommit (con)
}) # end finally
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_get_warnings}", sInsert)
cat (paste0 (sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_get_errors}", sInsert)
cat (paste0 (sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
sRequest <- paste0 ("SELECT id, name, address, city FROM ", sTableName, " ORDER BY 1")
cat (paste0 (sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = TRUE, row.names = FALSE)
}, finally = {
sRequest <- paste0 ("DROP TABLE ", sTableName)
cat (paste0 (sRequest, "\n"))
tryCatch ({
DBI::dbExecute (con, sRequest)
}, error = function (e) {
cat (paste0 ("Ignoring ", strsplit (trimws (e), "\n") [[1]][[1]], "\n"))
})
}) # end finally
}, finally = {
cat (paste0 ("file.remove(", jsonFileName, ")\n"))
tryCatch ({
file.remove (jsonFileName)
}, error = function (e) {
cat (paste0 ("file.remove failed: ", e, "\n"))
})
}) # end finally
invisible (TRUE)
}, finally = {
DBI::dbDisconnect (con)
}) # end finally
} # end main
withCallingHandlers (main (), error = function (e) {
listStackFrames <- head (tail (sys.calls (), -1), -2) # omit first one and last two
nStackFrameCount <- length (listStackFrames)
cat (paste0 ("[", 1 : nStackFrameCount, "/", nStackFrameCount, "] ", listStackFrames, "\n\n", collapse = ""))
})