-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigQuery.gs
102 lines (95 loc) · 3.22 KB
/
BigQuery.gs
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
const REPORT_SCHEMA = [
{name: 'reviewer', type: 'STRING'},
{name: 'assigned_reason', type: 'STRING'},
{name: 'ldap', type: 'STRING'},
{name: 'case_subject', type: 'STRING'},
{name: 'site', type: 'STRING'},
{name: 'case_status_shortened', type: 'STRING'},
{name: 'priority', type: 'BOOLEAN'},
{name: 'escalated', type: 'BOOLEAN'},
{name: 'case_age', type: 'case_age'},
{name: 'specialization', type: 'STRING'},
{name: 'eng_bugs_links', type: 'STRING'},
{name: 'flags_links', type: 'STRING'},
{name: 'hard_consult_count', type: 'STRING'},
{name: 'live_consult_count', type: 'STRING'},
{name: 'rcr', type: 'STRING'},
{name: 'completed', type: 'STRING'},
{name: 'driver', type: 'STRING'},
{name: 'action_taken1', type: 'STRING'},
{name: 'action_taken2', type: 'STRING'},
{name: 'time_taken', type: 'STRING'},
{name: 'notes', type: 'STRING'},
{name: 'scrub_date', type: 'STRING'},
{name: 'version', type: 'STRING'}
];
//['reviewer', 'assigned_reason', 'ldap', 'case_subject', 'site', 'case_status_shortened', 'priority', 'escalated', 'case_age', 'specialization', 'case_number', 'eng_bugs_links', 'flags_links', 'hard_consult_count', 'live_consult_count', 'rcr', 'completed', 'driver', 'action_taken1', 'action_taken2', 'time_taken', 'notes', 'scrub_date', 'version']
function loadDataToBigQuery_(projectId, datasetId, tableId, schema, raws) {
let table = {
tableReference: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
schema: {
fields: [
{name: 'week', type: 'STRING'},
{name: 'cat', type: 'INTEGER'},
{name: 'dog', type: 'INTEGER'},
{name: 'bird', type: 'INTEGER'}
]
}
};
try {
table = BigQuery.Tables.insert(table, projectId, datasetId);
console.log('Table created: %s', table.id);
} catch (err) {
console.log('unable to create table');
}
// Load CSV data from Drive and convert to the correct format for upload.
const file = DriveApp.getFileById(csvFileId);
const data = file.getBlob().setContentType('application/octet-stream');
// Create the data upload job.
const job = {
configuration: {
load: {
destinationTable: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
skipLeadingRows: 1
}
}
};
try {
const jobResult = BigQuery.Jobs.insert(job, projectId, data);
console.log(`Load job started. Status: ${jobResult.status.state}`);
} catch (err) {
console.log('unable to insert job');
}
}
function convertRawsToCVS_(raws) {
// var rowsCSV = raws.join("\n");
return Utilities.newBlob(raws.join("\n"), "text/csv").setContentType('application/octet-stream');
// return blob.setContentType('application/octet-stream');
}
/**
* var rowsCSV = rows.join("\n");
var blob = Utilities.newBlob(rowsCSV, "text/csv");
var data = blob.setContentType('application/octet-stream');
// Create the data upload job.
var job = {
configuration: {
load: {
destinationTable: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
skipLeadingRows: 1,
writeDisposition: 'WRITE_APPEND'
}
}
};
*/