-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharrow_reader.test.js
More file actions
174 lines (150 loc) · 4.77 KB
/
arrow_reader.test.js
File metadata and controls
174 lines (150 loc) · 4.77 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
const fs = require('fs');
const os = require('os');
const path = require('path');
const retry = require('async-retry');
const { tableFromArrays, tableToIPC } = require('apache-arrow');
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: 'arrow_file_reader_single',
wildcard: 'arrow_file_reader_wildcard',
stream: 'arrow_stream_reader',
};
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'node-es-transformer-arrow-'));
const sampleFile1 = path.join(tempDir, 'sample_data_arrow_1.arrow');
const sampleFile2 = path.join(tempDir, 'sample_data_arrow_2.arrow');
async function runTransformerAndWait(options) {
const { events } = await transformer(options);
await new Promise((resolve, reject) => {
events.on('finish', resolve);
events.on('error', reject);
});
}
function createArrowFile(filePath, rows) {
const table = tableFromArrays({
the_index: Int32Array.from(rows.map(row => row.the_index)),
code: Int32Array.from(rows.map(row => row.code)),
url: rows.map(row => row.url),
text: rows.map(row => row.text),
});
const arrowBuffer = Buffer.from(tableToIPC(table, 'file'));
fs.writeFileSync(filePath, arrowBuffer);
}
describe('indexes arrow IPC sources', () => {
beforeAll(() => {
createArrowFile(sampleFile1, [
{
the_index: 1,
code: 100,
url: 'https://example.com/a1',
text: 'arrow-one',
},
{
the_index: 2,
code: 200,
url: 'https://example.com/a2',
text: 'arrow-two',
},
{
the_index: 3,
code: 300,
url: 'https://example.com/a3',
text: 'arrow-three',
},
]);
createArrowFile(sampleFile2, [
{
the_index: 4,
code: 400,
url: 'https://example.com/a4',
text: 'arrow-four',
},
{
the_index: 5,
code: 500,
url: 'https://example.com/a5',
text: 'arrow-five',
},
]);
});
afterAll(async () => {
await deleteIndex(client, indexes.single)();
await deleteIndex(client, indexes.wildcard)();
await deleteIndex(client, indexes.stream)();
await client.close();
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('should index an arrow file', async () => {
await runTransformerAndWait({
fileName: sampleFile1,
sourceFormat: 'arrow',
targetIndexName: indexes.single,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
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('arrow-two');
});
});
it('should index arrow files through wildcard patterns', async () => {
await runTransformerAndWait({
fileName: path.join(tempDir, 'sample_data_arrow_*.arrow'),
sourceFormat: 'arrow',
targetIndexName: indexes.wildcard,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
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(5);
});
});
it('should index an arrow stream', async () => {
await runTransformerAndWait({
stream: fs.createReadStream(sampleFile1),
sourceFormat: 'arrow',
targetIndexName: indexes.stream,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
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('arrow-three');
});
});
});