-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparquet_reader.test.js
More file actions
320 lines (281 loc) · 8.58 KB
/
parquet_reader.test.js
File metadata and controls
320 lines (281 loc) · 8.58 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const fs = require('fs');
const os = require('os');
const path = require('path');
const retry = require('async-retry');
const parquet = require('@dsnp/parquetjs');
const zlib = require('zlib');
const { PARQUET_COMPRESSION_METHODS } = require('@dsnp/parquetjs/dist/lib/compression');
function registerZstdCompression() {
if (PARQUET_COMPRESSION_METHODS.ZSTD) {
return;
}
if (
typeof zlib.zstdCompressSync !== 'function' ||
typeof zlib.zstdDecompressSync !== 'function'
) {
throw new Error('ZSTD compression requires Node.js with zstd support.');
}
PARQUET_COMPRESSION_METHODS.ZSTD = {
deflate(value) {
return zlib.zstdCompressSync(value);
},
inflate(value) {
return zlib.zstdDecompressSync(value);
},
};
}
registerZstdCompression();
const transformer = require('../dist/node-es-transformer.cjs');
const deleteIndex = require('./utils/delete_index');
const { elasticsearchUrl, getElasticsearchClient } = require('./utils/elasticsearch');
const client = getElasticsearchClient();
const indexes = {
single: 'parquet_file_reader_single',
noTransform: 'parquet_file_reader_no_transform',
wildcard: 'parquet_file_reader_wildcard',
stream: 'parquet_stream_reader',
zstd: 'parquet_file_reader_zstd',
};
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'node-es-transformer-parquet-'));
const sampleFile1 = path.join(tempDir, 'sample_data_parquet_1.parquet');
const sampleFile2 = path.join(tempDir, 'sample_data_parquet_2.parquet');
const sampleFileZstd = path.join(tempDir, 'sample_data_parquet_zstd.parquet');
async function runTransformerAndWait(options) {
const { events } = await transformer(options);
await new Promise((resolve, reject) => {
events.on('finish', resolve);
events.on('error', reject);
});
}
function buildField(type, compression) {
return compression ? { type, compression } : { type };
}
async function createParquetFile(filePath, rows, compression) {
const schema = new parquet.ParquetSchema({
the_index: buildField('INT64', compression),
code: buildField('INT64', compression),
url: buildField('UTF8', compression),
text: buildField('UTF8', compression),
});
const writer = await parquet.ParquetWriter.openFile(schema, filePath);
try {
for (const row of rows) {
// eslint-disable-next-line no-await-in-loop
await writer.appendRow(row);
}
} finally {
await writer.close();
}
}
describe('indexes parquet sources', () => {
beforeAll(async () => {
await createParquetFile(sampleFile1, [
{
the_index: 1,
code: 100,
url: 'https://example.com/p1',
text: 'parquet-one',
},
{
the_index: 2,
code: 200,
url: 'https://example.com/p2',
text: 'parquet-two',
},
{
the_index: 3,
code: 300,
url: 'https://example.com/p3',
text: 'parquet-three',
},
]);
await createParquetFile(sampleFile2, [
{
the_index: 4,
code: 400,
url: 'https://example.com/p4',
text: 'parquet-four',
},
{
the_index: 5,
code: 500,
url: 'https://example.com/p5',
text: 'parquet-five',
},
]);
await createParquetFile(
sampleFileZstd,
[
{
the_index: 6,
code: 600,
url: 'https://example.com/p6',
text: 'parquet-zstd-one',
},
{
the_index: 7,
code: 700,
url: 'https://example.com/p7',
text: 'parquet-zstd-two',
},
],
'ZSTD',
);
});
afterAll(async () => {
await deleteIndex(client, indexes.single)();
await deleteIndex(client, indexes.noTransform)();
await deleteIndex(client, indexes.wildcard)();
await deleteIndex(client, indexes.stream)();
await deleteIndex(client, indexes.zstd)();
await client.close();
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('should index a parquet file', async () => {
await runTransformerAndWait({
fileName: sampleFile1,
sourceFormat: 'parquet',
targetIndexName: indexes.single,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
transform(doc) {
return {
...doc,
the_index: Number(doc.the_index),
code: Number(doc.code),
};
},
verbose: false,
});
await client.indices.refresh({ index: indexes.single });
await retry(async () => {
const res = await fetch(`${elasticsearchUrl}/${indexes.single}/_search?q=the_index:2`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.hits?.total?.value).toBe(1);
expect(body?.hits?.hits?.[0]?._source?.text).toBe('parquet-two');
});
});
it('should index a parquet file without a transform', async () => {
await runTransformerAndWait({
fileName: sampleFile1,
sourceFormat: 'parquet',
targetIndexName: indexes.noTransform,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
verbose: false,
});
await client.indices.refresh({ index: indexes.noTransform });
await retry(async () => {
const res = await fetch(`${elasticsearchUrl}/${indexes.noTransform}/_search?q=the_index:2`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.hits?.total?.value).toBe(1);
expect(body?.hits?.hits?.[0]?._source?.text).toBe('parquet-two');
});
});
it('should index a ZSTD-compressed parquet file', async () => {
await runTransformerAndWait({
fileName: sampleFileZstd,
sourceFormat: 'parquet',
targetIndexName: indexes.zstd,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
transform(doc) {
return {
...doc,
the_index: Number(doc.the_index),
code: Number(doc.code),
};
},
verbose: false,
});
await client.indices.refresh({ index: indexes.zstd });
await retry(async () => {
const res = await fetch(`${elasticsearchUrl}/${indexes.zstd}/_search?q=the_index:6`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.hits?.total?.value).toBe(1);
expect(body?.hits?.hits?.[0]?._source?.text).toBe('parquet-zstd-one');
});
});
it('should index parquet files through wildcard patterns', async () => {
await runTransformerAndWait({
fileName: path.join(tempDir, 'sample_data_parquet_*.parquet'),
sourceFormat: 'parquet',
targetIndexName: indexes.wildcard,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
transform(doc) {
return {
...doc,
the_index: Number(doc.the_index),
code: Number(doc.code),
};
},
verbose: false,
});
await client.indices.refresh({ index: indexes.wildcard });
await retry(async () => {
const res = await fetch(`${elasticsearchUrl}/${indexes.wildcard}/_count`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.count).toBe(7);
});
});
it('should index a parquet stream', async () => {
await runTransformerAndWait({
stream: fs.createReadStream(sampleFile1),
sourceFormat: 'parquet',
targetIndexName: indexes.stream,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
transform(doc) {
return {
...doc,
the_index: Number(doc.the_index),
code: Number(doc.code),
};
},
verbose: false,
});
await client.indices.refresh({ index: indexes.stream });
await retry(async () => {
const res = await fetch(`${elasticsearchUrl}/${indexes.stream}/_search?q=the_index:3`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.hits?.total?.value).toBe(1);
expect(body?.hits?.hits?.[0]?._source?.text).toBe('parquet-three');
});
});
});