Example usage for com.fasterxml.jackson.databind MappingIterator next

List of usage examples for com.fasterxml.jackson.databind MappingIterator next

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind MappingIterator next.

Prototype

@Override
    public T next() 

Source Link

Usage

From source file:io.fabric8.forge.rest.model.Models.java

/**
 * Saves the json object to the given file
 *//*from   ww w . j  ava  2s . c  o  m*/
public static <T> List<T> loadJsonValues(File json, Class<T> clazz) throws IOException {
    List<T> answer = new ArrayList<>();
    if (json.exists() && json.isFile()) {
        MappingIterator<T> iter = objectMapper.readerFor(clazz).readValues(json);
        while (iter.hasNext()) {
            answer.add(iter.next());
        }
    }
    return answer;
}

From source file:io.fabric8.devops.ProjectConfigs.java

static <T> List<T> parseYamlValues(File file, Class<T> clazz) throws IOException {
    ObjectMapper mapper = createObjectMapper();
    MappingIterator<T> iter = mapper.readerFor(clazz).readValues(file);
    List<T> answer = new ArrayList<>();
    while (iter.hasNext()) {
        answer.add(iter.next());
    }/*from   www .j  a v a 2s . c om*/
    return answer;
}

From source file:io.github.binout.jaxrs.csv.CsvSchemaFactoryTest.java

@Test
public void test_reader() throws IOException {
    byte[] csv = inputStreamToString(
            Thread.currentThread().getContextClassLoader().getResourceAsStream("persons.csv")).getBytes();
    MappingIterator<Person> iterator = mapper.reader(Person.class).with(schema).readValues(csv);
    Person benoit = iterator.next();
    assertThat(benoit.getFirstName()).isEqualTo("Benoit");
    Person alexis = iterator.next();/*from  ww  w.  ja v  a  2  s  .  c o m*/
    assertThat(alexis.getFirstName()).isEqualTo("Alexis");
}

From source file:org.jongo.spike.MongoDumpTest.java

@Test
public void importBsonDumpFileIntoCollection() throws Exception {

    InputStream bsonDump = getClass().getClassLoader().getResourceAsStream("1000friends.bson");
    BsonFactory bsonFactory = new BsonFactory();
    //bsonFactory.enable(BsonParser.Feature.HONOR_DOCUMENT_LENGTH); // fails when enabled
    ObjectReader reader = new ObjectMapper(bsonFactory).reader(BasicBSONObject.class);

    MappingIterator<BSONObject> iterator = reader.readValues(bsonDump);
    try {//  w ww.j  av  a2s .  c om
        while (iterator.hasNext()) {
            BSONObject bsonObject = iterator.next();
            collection.withWriteConcern(WriteConcern.SAFE).save(bsonObject);
        }
    } finally {
        iterator.close();
    }

    assertThat(collection.count()).isEqualTo(1000);
}

From source file:org.usrz.libs.riak.response.JsonContentHandler.java

@Override
protected T read(PartialResponse<T> partial, InputStream input) throws Exception {

    /* Use a MappingIterator, as we don't want to fail on empty JSON */
    final JsonParser parser = mapper.getFactory().createParser(input);
    final MappingIterator<T> iterator = mapper.readValues(parser, type);

    /* Read only the first value, then close */
    while (iterator.hasNextValue())
        try {// w ww .  j  ava  2s  . c  o  m
            return iterator.next();
        } finally {
            iterator.close();
        }

    /* Didn't even get the first value */
    return null;
}

From source file:org.usrz.libs.riak.response.ChunkedContentHandler.java

@Override
protected Boolean read(PartialResponse<Boolean> partial, InputStream input) throws Exception {
    try {/*  w  ww  .  j av a2  s  . com*/
        final JsonParser parser = mapper.getFactory().createParser(input);
        final MappingIterator<? extends Chunk<T, H>> iterator = mapper.readValues(parser, chunkType);
        while (iterator.hasNextValue()) {
            final Chunk<T, H> chunk = iterator.next();
            if (chunk.putAll(partial, thisInstance))
                continue;
            else
                return false;
        }
        return puttable.close();
    } catch (Throwable throwable) {
        puttable.fail(throwable);
        if (throwable instanceof Exception)
            throw (Exception) throwable;
        throw new ExecutionException(throwable);
    }
}

From source file:edu.cmu.cs.lti.discoursedb.io.bazaar.converter.BazaarConverter.java

private void convert(String messageFileDir, String roomFileDir, String agentname)
        throws ParseException, IOException {

    Map<String, String> roomIdNameMap = new HashMap<>();
    List<String> messages = new ArrayList<>();

    //Read input file and preprocess
    String lineFragment = null;/*  w ww .  ja v  a  2 s.  co m*/
    for (String line : FileUtils.readLines(new File(messageFileDir))) {
        //line fragments occur in case we have line feeds in a column
        if (lineFragment != null) {
            line = lineFragment + line;
            lineFragment = null;
        }
        if (line.endsWith("\\") || line.endsWith("\\\r\f")) {
            line = line.replaceAll("\\\r\f", "");
            lineFragment = line;
        } else {
            if (line.contains("\\\"We're Ready\\\"")) {
                line = line.replaceAll("\"We're Ready\\\\\"", "We're Ready\\\\");
            }
            if (line.contains("\\\"ready\\\"")) {
                line = line.replaceAll("\\\\\"ready\\\\\"", "\\\\ready\\\\");
            }
            if (line.contains("\\\"" + agentname + "\\\"")) {
                line = line.replaceAll("\\\\\"" + agentname + "\\\\\"", "\\\\" + agentname + "\\\\");
            }
            messages.add(line);
        }
    }

    // Phase 1: read through input room file once and map all entities
    try (InputStream in = new FileInputStream(roomFileDir)) {
        CsvMapper mapper = new CsvMapper();
        CsvSchema schema = mapper.schemaFor(Room.class).withColumnSeparator(',');
        MappingIterator<Room> rIter = mapper.readerFor(Room.class).with(schema).readValues(in);
        while (rIter.hasNextValue()) {
            Room r = rIter.next();
            if (!roomIdNameMap.containsKey(r.getId()))
                roomIdNameMap.put(r.getId(), r.getName());
            converterService.mapRoom(r, dataSetName, discourseName);
        }
    } catch (IOException e) {
        log.error("Error reading room file", e);
    }

    // Phase 2: read through input message file and map relationships between room and message
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(Message.class).withColumnSeparator(',');
    for (String message : messages) {
        Message m = mapper.readerFor(Message.class).with(schema).readValue(message);
        if (m.getType().equals("text") || m.getType().equals("image") || m.getType().equals("private")) {
            converterService.mapMessage(m, dataSetName, discourseName, roomIdNameMap);
        } else {
            converterService.mapInteraction(m, dataSetName, discourseName, roomIdNameMap);
        }
    }
}

From source file:com.marklogic.entityservices.e2e.CSVLoader.java

public void go() throws InterruptedException {

    logger.info("job started.");

    File dir = new File(projectDir + "/data/superstore-csv");

    WriteHostBatcher batcher = moveMgr.newWriteHostBatcher().withBatchSize(100).withThreadCount(10)
            .onBatchSuccess((client, batch) -> logger.info(getSummaryReport(batch)))
            .onBatchFailure((client, batch, throwable) -> {
                logger.warn("FAILURE on batch:" + batch.toString() + "\n", throwable);
                throwable.printStackTrace();
            });//w  w w .  ja  va 2s  . c  o  m

    ticket = moveMgr.startJob(batcher);

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath(), "*.csv")) {
        for (Path entry : stream) {
            logger.debug("Adding " + entry.getFileName().toString());

            MappingIterator<ObjectNode> it = csvMapper.readerFor(ObjectNode.class).with(bootstrapSchema)
                    .readValues(entry.toFile());
            long i = 0;
            while (it.hasNext()) {
                ObjectNode jsonNode = it.next();
                String jsonString = mapper.writeValueAsString(jsonNode);

                String uri = entry.toUri().toString() + "-" + Long.toString(i++) + ".json";
                DocumentMetadataHandle metadata = new DocumentMetadataHandle() //
                        .withCollections("raw", "csv") //
                        .withPermission("nwind-reader", Capability.READ) //
                        .withPermission("nwind-writer", Capability.INSERT, Capability.UPDATE);
                batcher.add(uri, metadata, new StringHandle(jsonString));
                if (i % 1000 == 0)
                    logger.debug("Inserting JSON document " + uri);
            }
            it.close();
        }
    }

    catch (IOException e)

    {
        e.printStackTrace();
    }

    batcher.flush();
}

From source file:com.marklogic.entityservices.examples.CSVLoader.java

public void go() throws InterruptedException {

    logger.info("job started.");

    File dir = new File(projectDir + "/data/third-party/csv");

    WriteHostBatcher batcher = moveMgr.newWriteHostBatcher().withBatchSize(100).withThreadCount(10)
            .onBatchSuccess((client, batch) -> logger.info(getSummaryReport(batch)))
            .onBatchFailure((client, batch, throwable) -> {
                logger.warn("FAILURE on batch:" + batch.toString() + "\n", throwable);
                throwable.printStackTrace();
            });/*from  w  w w . j a  va2s .c  om*/

    ticket = moveMgr.startJob(batcher);

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath(), "*.csv")) {
        for (Path entry : stream) {
            logger.debug("Adding " + entry.getFileName().toString());

            MappingIterator<ObjectNode> it = csvMapper.readerFor(ObjectNode.class).with(bootstrapSchema)
                    .readValues(entry.toFile());
            long i = 0;
            while (it.hasNext()) {
                ObjectNode jsonNode = it.next();
                String jsonString = mapper.writeValueAsString(jsonNode);

                String uri = entry.toUri().toString() + "-" + Long.toString(i++) + ".json";
                DocumentMetadataHandle metadata = new DocumentMetadataHandle() //
                        .withCollections("raw", "csv") //
                        .withPermission("race-reader", Capability.READ) //
                        .withPermission("race-writer", Capability.INSERT, Capability.UPDATE);
                batcher.add(uri, metadata, new StringHandle(jsonString));
                if (i % 1000 == 0)
                    logger.debug("Inserting JSON document " + uri);
            }
            it.close();
        }
    }

    catch (IOException e)

    {
        e.printStackTrace();
    }

    batcher.flush();
}

From source file:com.couchbase.devex.CSVConfig.java

@Override
public Observable<Document> startImport() {
    FileInputStream csvFile;// w ww  . j  a  va 2 s.  co  m
    try {
        csvFile = new FileInputStream(getCsvFilePath());
        CsvMapper mapper = new CsvMapper();
        mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
        CsvSchema csvSchema = CsvSchema.emptySchema().withColumnSeparator(getColumnSeparator())
                .withQuoteChar(getQuoteChar());
        ObjectReader reader = mapper.reader(String[].class);
        MappingIterator<String[]> it = reader.with(csvSchema).readValues(csvFile);
        if (!getSkipFirstLineForNames()) {
            String[] firstline = it.next();
            updateColumnNames(firstline);
        }
        return Observable.from(new Iterable<String[]>() {
            @Override
            public Iterator<String[]> iterator() {
                return it;
            }
        }).flatMap(line -> createNode(line));
    } catch (FileNotFoundException e) {
        return Observable.error(e);
    } catch (IOException e) {
        return Observable.error(e);
    }
}