/
FastLoadCSV.ts
121 lines (97 loc) · 3.79 KB
/
FastLoadCSV.ts
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
// Copyright 2023 by Teradata Corporation. All rights reserved.
// This sample program demonstrates how to FastLoad a CSV file.
// @ts-ignore
import { TeradataConnection, TeradataCursor } from "teradatasql";
import * as fs from "fs";
type Rows = any[];
const con: TeradataConnection = new TeradataConnection();
con.connect({ host: "whomooz", user: "guest", password: "please" });
const cur: TeradataCursor = con.cursor();
const sTableName: string = "FastLoadCSV";
let sRequest: string = "DROP TABLE " + sTableName;
console.log(sRequest);
cur.execute(sRequest, undefined, 3807); // ignoreErrors = 3807
sRequest = "DROP TABLE " + sTableName + "_ERR_1";
console.log(sRequest);
cur.execute(sRequest, undefined, 3807); // ignoreErrors = 3807
sRequest = "DROP TABLE " + sTableName + "_ERR_2";
console.log(sRequest);
cur.execute(sRequest, undefined, 3807); // ignoreErrors = 3807
const records: string[] = ["c1,c2", "1,", "2,abc", "3,def", "4,mno", "5,", "6,pqr", "7,uvw", "8,xyz", "9,"];
console.log("records", records);
const csvFileName: string = "dataJs.csv";
for (const record of records) {
fs.appendFileSync(csvFileName, record + "\n");
}
try {
sRequest = "CREATE TABLE " + sTableName + " (c1 INTEGER NOT NULL, c2 VARCHAR(10))";
console.log(sRequest);
cur.execute(sRequest);
try {
console.log("con.autocommit = false");
con.autocommit = false;
try {
const sInsert: string = "{fn teradata_require_fastload}{fn teradata_read_csv(" + csvFileName + ")}INSERT INTO " + sTableName + " (?, ?)";
console.log(sInsert);
cur.execute(sInsert);
// obtain the warnings and errors for transmitting the data to the database -- the acquisition phase
sRequest = "{fn teradata_nativesql}{fn teradata_get_warnings}" + sInsert;
console.log(sRequest);
cur.execute(sRequest);
let rows: Rows = cur.fetchall();
for (const row of rows) {
console.log(row);
}
sRequest = "{fn teradata_nativesql}{fn teradata_get_errors}" + sInsert;
console.log(sRequest);
cur.execute(sRequest);
rows = cur.fetchall();
for (const row of rows) {
console.log(row);
}
sRequest = "{fn teradata_nativesql}{fn teradata_logon_sequence_number}" + sInsert;
console.log(sRequest);
cur.execute(sRequest);
rows = cur.fetchall();
for (const row of rows) {
console.log(row);
}
console.log("con.commit()");
con.commit();
// obtain the warnings and errors for the apply phase
sRequest = "{fn teradata_nativesql}{fn teradata_get_warnings}" + sInsert;
console.log(sRequest);
cur.execute(sRequest);
rows = cur.fetchall();
for (const row of rows) {
console.log(row);
}
sRequest = "{fn teradata_nativesql}{fn teradata_get_errors}" + sInsert;
console.log(sRequest);
cur.execute(sRequest);
rows = cur.fetchall();
for (const row of rows) {
console.log(row);
}
} finally {
console.log("con.autocommit = true");
con.autocommit = true;
}
sRequest = "SELECT * FROM " + sTableName + " ORDER BY 1";
console.log(sRequest);
cur.execute(sRequest);
const rows: Rows = cur.fetchall();
for (const row of rows) {
console.log(row);
}
} finally {
sRequest = "DROP TABLE " + sTableName;
console.log(sRequest);
cur.execute(sRequest);
}
} finally {
console.log(`delete ${csvFileName}`);
fs.unlinkSync(csvFileName);
}
cur.close();
con.close();