use of com.codingchili.Model.FileParser in project parser-excel-elasticsearch by codingchili.
the class ApplicationLauncher method importFile.
/**
* Imports a file from the commandline.
* @param fileName the name of the file to import.
* @param indexName the name of the index to import the file into.
*/
private void importFile(String fileName, String indexName) {
logger.info(String.format("Loading file %s from filesystem..", fileName));
vertx.fileSystem().readFile(fileName, file -> {
if (file.succeeded()) {
logger.info("Parsing XLSX file to JSON..");
try {
FileParser parser = new FileParser(file.result().getBytes(), 1, fileName);
logger.info(String.format("File parsed, starting import to %s..", indexName));
vertx.eventBus().send(Configuration.INDEXING_ELASTICSEARCH, parser.toImportable(indexName, getMapping(), clearExisting()), new DeliveryOptions().setSendTimeout(INDEXING_TIMEOUT), done -> {
if (done.succeeded()) {
logger.info("Import completed, shutting down.");
} else {
logger.log(Level.SEVERE, "Failed to import", done.cause());
}
// vertx.close gives an error: "result already completed: success"
System.exit(0);
});
} catch (ParserException e) {
logger.log(Level.SEVERE, String.format("Failed to import file %s", fileName));
}
} else {
logger.log(Level.SEVERE, String.format("Failed to load file %s", fileName), file.cause());
}
});
}
use of com.codingchili.Model.FileParser in project parser-excel-elasticsearch by codingchili.
the class Website method parse.
/**
* Parses a file upload request, converting the excel payload into json and waits
* for elasticsearch to complete indexing.
*
* @param buffer contains the excel file data
* @param params upload parameters
* @param fileName the name of the uploaded file
* @param future callback on completed parse + indexing.
*/
private void parse(Buffer buffer, MultiMap params, String fileName, Future<Integer> future) {
vertx.<Integer>executeBlocking(blocking -> {
try {
int columnRow = Integer.parseInt(params.get(OFFSET));
FileParser parser = new FileParser(buffer.getBytes(), columnRow, fileName);
JsonObject data = parser.toImportable(params.get(INDEX), getMappingByParams(params), params.get(OPTIONS).equals(CLEAR));
data.put(UPLOAD_ID, params.get(UPLOAD_ID));
vertx.eventBus().send(INDEXING_ELASTICSEARCH, data, new DeliveryOptions().setSendTimeout(INDEXING_TIMEOUT), reply -> {
if (reply.succeeded()) {
blocking.complete(parser.getImportedItems());
} else {
blocking.fail(reply.cause());
}
});
} catch (ParserException | NumberFormatException e) {
blocking.fail(e);
}
}, false, done -> {
if (done.succeeded()) {
future.complete(done.result());
} else {
future.fail(done.cause());
}
});
}
use of com.codingchili.Model.FileParser in project parser-excel-elasticsearch by codingchili.
the class TestParser method testParseFile.
private void testParseFile(TestContext context, String fileName) throws IOException, ParserException {
FileParser parser = new FileParser(Files.readAllBytes(Paths.get(fileName)), ROW_OFFSET, fileName);
JsonArray list = parser.toImportable("index", "mapping", false).getJsonArray(ITEMS);
context.assertEquals(2, list.size());
for (int i = 0; i < list.size(); i++) {
JsonObject json = list.getJsonObject(i);
context.assertTrue(json.containsKey("Column 1"));
context.assertTrue(json.containsKey("Column 2"));
context.assertTrue(json.containsKey("Column 3"));
context.assertEquals("cell " + (ROW_OFFSET + 1 + i) + "." + 1, json.getString("Column 1"));
context.assertEquals("cell " + (ROW_OFFSET + 1 + i) + "." + 2, json.getString("Column 2"));
context.assertEquals("cell " + (ROW_OFFSET + 1 + i) + "." + 3, json.getString("Column 3"));
}
}
Aggregations