Showing
4 changed files
with
164 additions
and
0 deletions
code/elasticsearh-nodejs/count.js
0 → 100644
1 | +const esClient = require('./client'); | ||
2 | +const addmappingToIndex = async function(indexName, mappingType, mapping){ | ||
3 | + console.log(mapping); | ||
4 | + return await esClient.indices.putMapping({ | ||
5 | + index: indexName, | ||
6 | + type: mappingType, | ||
7 | + body: mapping | ||
8 | + }); | ||
9 | +} | ||
10 | + | ||
11 | +module.exports = addmappingToIndex; | ||
12 | + | ||
13 | + | ||
14 | +// test function to explain how to invoke. | ||
15 | +async function test() { | ||
16 | + const mapping = { | ||
17 | + properties: { | ||
18 | + date : {type : "date", | ||
19 | + format : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}, | ||
20 | + current_count : {type : "integer"}, | ||
21 | + total_count : {type : "integer"}, | ||
22 | + seconds : {type : "float"} | ||
23 | + } | ||
24 | + } | ||
25 | + try { | ||
26 | + const resp = await addmappingToIndex('count', 'count_info', mapping); | ||
27 | + console.log(resp); | ||
28 | + } catch (e) { | ||
29 | + console.log(e); | ||
30 | + } | ||
31 | +} | ||
32 | + | ||
33 | +test(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
code/elasticsearh-nodejs/countToIndex.js
0 → 100644
1 | +const axios = require("axios"); | ||
2 | +const log = console.log; | ||
3 | + | ||
4 | +const esClient = require('./client'); | ||
5 | +const fs = require('fs'); | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | +async function indexData() { | ||
10 | + try { | ||
11 | + const aptRaw = await fs.readFileSync('./count.json'); | ||
12 | + const aptInfo = JSON.parse(aptRaw); | ||
13 | + const apt_list = [] | ||
14 | + for(const item of aptInfo.data){ | ||
15 | + const tmp_data = { | ||
16 | + date : item.time.substring(0,19), | ||
17 | + current_count : item.now, | ||
18 | + total_count : item.total, | ||
19 | + seconds : item.s | ||
20 | + } | ||
21 | + apt_list.push(tmp_data) | ||
22 | + } | ||
23 | + const body = apt_list.flatMap(doc => [{ index: { _index: 'count', _type: 'count_info' } }, doc]) | ||
24 | + const { body: bulkResponse } = await esClient.bulk({ refresh: true, body }) | ||
25 | + | ||
26 | + if (bulkResponse.errors) { | ||
27 | + const erroredDocuments = [] | ||
28 | + bulkResponse.items.forEach((action, i) => { | ||
29 | + const operation = Object.keys(action)[0] | ||
30 | + if (action[operation].error) { | ||
31 | + erroredDocuments.push({ | ||
32 | + status: action[operation].status, | ||
33 | + error: action[operation].error, | ||
34 | + operation: body[i * 2], | ||
35 | + document: body[i * 2 + 1] | ||
36 | + }) | ||
37 | + } | ||
38 | + }) | ||
39 | + console.log(erroredDocuments) | ||
40 | + log('bulk error') | ||
41 | + } | ||
42 | + } catch (error) { | ||
43 | + log('에러') | ||
44 | + } | ||
45 | + } | ||
46 | + | ||
47 | +indexData(); | ||
48 | + |
code/elasticsearh-nodejs/position.js
0 → 100644
1 | +const esClient = require('./client'); | ||
2 | +const addmappingToIndex = async function(indexName, mappingType, mapping){ | ||
3 | + console.log(mapping); | ||
4 | + return await esClient.indices.putMapping({ | ||
5 | + index: indexName, | ||
6 | + type: mappingType, | ||
7 | + body: mapping | ||
8 | + }); | ||
9 | +} | ||
10 | + | ||
11 | +module.exports = addmappingToIndex; | ||
12 | + | ||
13 | + | ||
14 | +// test function to explain how to invoke. | ||
15 | +async function test() { | ||
16 | + const mapping = { | ||
17 | + properties: { | ||
18 | + x : {type : "integer"}, | ||
19 | + y : {type : "integer"}, | ||
20 | + tracking_id : {type : "integer"}, | ||
21 | + date : {type : "date", | ||
22 | + format : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}, | ||
23 | + seconds : {type : "float"} | ||
24 | + } | ||
25 | + } | ||
26 | + try { | ||
27 | + const resp = await addmappingToIndex('position', 'position_info', mapping); | ||
28 | + console.log(resp); | ||
29 | + } catch (e) { | ||
30 | + console.log(e); | ||
31 | + } | ||
32 | +} | ||
33 | + | ||
34 | +test(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
code/elasticsearh-nodejs/positionToIndex.js
0 → 100644
1 | +const axios = require("axios"); | ||
2 | +const log = console.log; | ||
3 | + | ||
4 | +const esClient = require('./client'); | ||
5 | +const fs = require('fs'); | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | +async function indexData() { | ||
10 | + try { | ||
11 | + const aptRaw = await fs.readFileSync('./position.json'); | ||
12 | + const aptInfo = JSON.parse(aptRaw); | ||
13 | + const apt_list = [] | ||
14 | + for(const item of aptInfo.data){ | ||
15 | + const tmp_data = { | ||
16 | + x : item.x, | ||
17 | + y : item.y, | ||
18 | + tracking_id : item.id, | ||
19 | + date : item.time.substring(0,19), | ||
20 | + seconds : item.s | ||
21 | + } | ||
22 | + apt_list.push(tmp_data) | ||
23 | + } | ||
24 | + const body = apt_list.flatMap(doc => [{ index: { _index: 'position', _type: 'position_info' } }, doc]) | ||
25 | + const { body: bulkResponse } = await esClient.bulk({ refresh: true, body }) | ||
26 | + | ||
27 | + if (bulkResponse.errors) { | ||
28 | + const erroredDocuments = [] | ||
29 | + bulkResponse.items.forEach((action, i) => { | ||
30 | + const operation = Object.keys(action)[0] | ||
31 | + if (action[operation].error) { | ||
32 | + erroredDocuments.push({ | ||
33 | + status: action[operation].status, | ||
34 | + error: action[operation].error, | ||
35 | + operation: body[i * 2], | ||
36 | + document: body[i * 2 + 1] | ||
37 | + }) | ||
38 | + } | ||
39 | + }) | ||
40 | + console.log(erroredDocuments) | ||
41 | + log('bulk error') | ||
42 | + } | ||
43 | + } catch (error) { | ||
44 | + log('에러') | ||
45 | + } | ||
46 | + } | ||
47 | + | ||
48 | +indexData(); | ||
49 | + |
-
Please register or login to post a comment