Search in sources :

Example 1 with ParserException

use of com.codingchili.Model.ParserException 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());
        }
    });
}
Also used : FileParser(com.codingchili.Model.FileParser) ParserException(com.codingchili.Model.ParserException) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Example 2 with ParserException

use of com.codingchili.Model.ParserException 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());
        }
    });
}
Also used : FileParser(com.codingchili.Model.FileParser) ParserException(com.codingchili.Model.ParserException) JsonObject(io.vertx.core.json.JsonObject) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions)

Aggregations

FileParser (com.codingchili.Model.FileParser)2 ParserException (com.codingchili.Model.ParserException)2 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)2 JsonObject (io.vertx.core.json.JsonObject)1