-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredshift-table-encode.js
More file actions
161 lines (140 loc) · 5.19 KB
/
redshift-table-encode.js
File metadata and controls
161 lines (140 loc) · 5.19 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
152
153
154
155
156
157
158
159
160
161
/**
* redshift-table-encode
*
* steps:
* 1. connect to redshift using pg
* 2. analyze copmpression - to get the recommended encoding to all columns
* 3. get current status of columns
* 4. filter only the columns that are not encoded
* 5. encode every column separately:
* 5.1 create a new encoded column with a temp name
* 5.2 update the new column with all the data from the original column
* 5.3 drop the original column
* 5.4 rename new column to original name
*
*/
var async = require('async');
var _ = require('lodash');
var util = require('util');
var redshift = require('./redshift');
var redshift_table_encode = function(){};
/**
* options should be in this format:
{
'schema': '',
'table': '',
'connectionString': '',
'password': only required if contains ':'
}
*/
redshift_table_encode.prototype.tabularEncode = function(options, parentCallback){
var self = this;
if (!options.schema || !options.table || !options.connectionString) {
return parentCallback('Error: missing one of the following fields:' +
' schema, table or connectionString');
}
async.waterfall([
function connectToRedshift(callback){
redshift.connect(options, callback);
},
function runAnalyzeCompression(connection, callback){
redshift.analyzeCompression(options, callback);
},
function getTableCurrentDetails(encodingRecommendation, callback){
redshift.getTableDetails(options, function(err, result) {
if (err) {
callback(err);
} else{
callback(null, encodingRecommendation, result);
}
});
},
function startEncodeWholeTable(encodingRecommendation, tableDetails, callback){
self.changeColumnEncoding(encodingRecommendation,tableDetails);
redshift.runEncodeTable(options, encodingRecommendation,
tableDetails, callback);
}
], function(err, data){
if (err) {
parentCallback(err);
} else {
parentCallback(null, 'finished encoding');
}
});
};
redshift_table_encode.prototype.columnarEncode = function(options, parentCallback){
var self = this;
if (!options.schema || !options.table || !options.connectionString) {
return parentCallback('Error: missing one of the following fields:' +
' schema, table or connectionString');
}
async.waterfall([
function connectToRedshift(callback){
redshift.connect(options, callback);
},
function runAnalyzeCompression(connection, callback){
redshift.analyzeCompression(options, callback);
},
function getTableCurrentDetails(encodingRecommendation, callback){
redshift.getTableDetails(options, function(err, result) {
if (err) {
callback(err);
} else{
callback(null, encodingRecommendation, result);
}
});
},
function findColumnsToEncode(encodingRecommendation, tableDetails, callback){
console.log('-----------');
console.log('total columns:' + encodingRecommendation.length);
var columns = self.filterColumnsToEncode(encodingRecommendation, tableDetails);
callback(null, columns);
},
function iterateOverColumnsAndEncode(columns, encodeTableCallback){
console.log('-----------');
console.log('total columns to encode: ' + columns.length);
console.log('-----------');
async.eachSeries(columns, function(column, callback){
redshift.runEncodeColumn(column, callback);
}, function(err){
if (err) {
encodeTableCallback(err);
} else {
encodeTableCallback();
}
});
}
], function(err, data){
if (err) {
parentCallback(err);
} else {
parentCallback(null, 'finished encoding all columns');
}
});
};
redshift_table_encode.prototype.changeColumnEncoding = function(recommended, current){
_(recommended).forEach(function(recommendedColumnData){
var column = _.findWhere(current, function(currentColumnData){
return recommendedColumnData.column === currentColumnData.column;
});
if (column) {
column.encoding = recommendedColumnData.encoding;
}
});
};
redshift_table_encode.prototype.filterColumnsToEncode = function(recommended, current){
var columnsToEncode = [];
_(recommended).forEach(function(recommendedColumnData){
var column = _.findWhere(current, function(currentColumnData){
return recommendedColumnData.column === currentColumnData.column &&
recommendedColumnData.encoding !== currentColumnData.encoding
});
if (column) {
column.encoding = recommendedColumnData.encoding;
columnsToEncode.push(column);
}
});
return columnsToEncode;
};
module.exports = new redshift_table_encode();
//encodeTable(options);