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

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

Introduction

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

Prototype

public T nextValue() throws IOException 

Source Link

Usage

From source file:io.fabric8.forge.camel.commands.project.dto.NodeDtos.java

protected static <T> List<T> toList(MappingIterator<T> iter) throws java.io.IOException {
    List<T> answer = new ArrayList<>();
    while (iter != null && iter.hasNextValue()) {
        T value = iter.nextValue();
        answer.add(value);/*from w w  w.ja  v a 2 s.  c  o  m*/
    }
    return answer;
}

From source file:com.github.fge.jackson.JsonNodeReader.java

private static JsonNode readNode(final MappingIterator<JsonNode> iterator) throws IOException {
    final Object source = iterator.getParser().getInputSource();
    final JsonParseExceptionBuilder builder = new JsonParseExceptionBuilder(source);

    builder.setMessage(BUNDLE.getMessage("read.noContent"));

    if (!iterator.hasNextValue())
        throw builder.build();

    final JsonNode ret = iterator.nextValue();

    builder.setMessage(BUNDLE.getMessage("read.trailingData")).setLocation(iterator.getCurrentLocation());

    try {/*from   w ww . j  a v a2  s.  c om*/
        if (iterator.hasNextValue())
            throw builder.build();
    } catch (JsonParseException e) {
        throw builder.setLocation(e.getLocation()).build();
    }

    return ret;
}

From source file:com.unisa.storm.bolt.ExchMsgParserBolt.java

public void execute(Tuple input, BasicOutputCollector collector) {
    LOGGER.debug("Parsing incomming Exchange Message:");
    String msg = input.getString(0);
    try {/*  ww w.  ja  v a  2s. c  o  m*/
        MappingIterator<Map<?, ?>> it = mapper.reader(schema).withType(Map.class).readValues(msg);
        Map<?, ?> result = it.nextValue();
        ExchangeMsg exchMsg = new ExchangeMsg(result.get("dateTime").toString(),
                result.get("clientIP").toString(), result.get("clientHostname").toString(),
                result.get("serverIP").toString(), result.get("serverHostname").toString(),
                result.get("sourceContext").toString(), result.get("connectorId").toString(),
                result.get("source").toString(), result.get("eventId").toString(),
                result.get("internalMessageId").toString(), result.get("messageId").toString(),
                result.get("networkMessageId").toString(), result.get("recipientAddress").toString(),
                result.get("recipientStatus").toString(), result.get("totalBytes").toString(),
                result.get("recipientCount").toString(), result.get("relatedRecipientAddress").toString(),
                result.get("reference").toString(), result.get("messageSubject").toString(),
                result.get("senderAddress").toString(), result.get("returnPath").toString(),
                result.get("messageInfo").toString(), result.get("directionality").toString(),
                result.get("tenantId").toString(), result.get("originalClientIP").toString(),
                result.get("originalServerIP").toString(), result.get("customData").toString());

        //Debug
        LOGGER.debug(exchMsg.toString());
        //System.out.println(exchMsg.toString());
        it.close();

        collector.emit(new Values(exchMsg.getDateTime(), exchMsg.getClientIP(), exchMsg.getClientHostname(),
                exchMsg.getServerIP(), exchMsg.getServerHostname(), exchMsg.getSourceContext(),
                exchMsg.getConnectorId(), exchMsg.getSource(), exchMsg.getEventId(),
                exchMsg.getInternalMessageId(), exchMsg.getMessageId(), exchMsg.getNetworkMessageId(),
                exchMsg.getRecipientAddress(), exchMsg.getRecipientStatus(), exchMsg.getTotalBytes(),
                exchMsg.getRecipientCount(), exchMsg.getRelatedRecipientAddress(), exchMsg.getReference(),
                exchMsg.getMessageSubject(), exchMsg.getSenderAddress(), exchMsg.getReturnPath(),
                exchMsg.getMessageInfo(), exchMsg.getDirectionality(), exchMsg.getTenantId(),
                exchMsg.getOriginalClientIP(), exchMsg.getOriginalServerIP(), exchMsg.getCustomData()));
    }

    catch (IOException ex) {
        LOGGER.error("IO error while reading source", ex);
        LOGGER.trace(null, ex);
    }
}

From source file:ro.fortsoft.dada.csv.CsvGenericDao.java

public long readFromCsv() {
    File file = new File(csvFile);
    if (!file.exists() || !file.isFile()) {
        return 0;
    }// ww  w.j a va2s .  c om

    Class<T> persistentClass = getPersistentClass();

    // create mapper and schema
    CsvMapper mapper = new CsvMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    CsvSchema schema = mapper.schemaFor(persistentClass).withHeader();

    this.entities = new ArrayList<>();

    // read entities
    long count = 0;
    try {
        MappingIterator<T> it = mapper.reader(persistentClass).with(schema).readValues(file);
        while (it.hasNextValue()) {
            entities.add(it.nextValue());
            count++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return count;
}

From source file:web.StreamMeetupComTask.java

@Override
public Void call() {
    try {/*from   ww  w  . j  ava2 s .  c o m*/
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectReader reader = objectMapper.reader(Map.class);
        MappingIterator<Map<String, Object>> iterator = reader.readValues(getInputStream());

        while (iterator.hasNextValue()) {
            Map<String, Object> entry = iterator.nextValue();

            // monitor the distribution of countries
            if (entry.containsKey("group") && entry.get("group") instanceof Map) {
                Map<String, Object> group = (Map<String, Object>) entry.get("group");
                if (group.containsKey("group_country")) {
                    metrics.meter("meetup.country." + group.get("group_country")).mark();
                    metrics.meter("meetup.country.total").mark();
                }
            }

            // monitor the distribution of the number of guests
            if (entry.containsKey("guests") && entry.get("guests") instanceof Long) {
                metrics.histogram("meetup.guests").update((Long) entry.get("guests"));
            }

            // monitor reservation time upfront, 1d, 4d, 1w, 2w, 1m, 2m, -
            if (entry.containsKey("event") && entry.get("event") instanceof Map) {
                Map<String, Object> event = (Map<String, Object>) entry.get("event");
                if (event.get("time") instanceof Long) {
                    metrics.counter("meetup.reservation.time.total").inc();
                    metrics.counter(
                            "meetup.reservation.time." + getUpfrontReservationTime((Long) event.get("time")))
                            .inc();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.wikidata.wdtk.datamodel.json.jackson.JsonSerializerTest.java

@Test
public void testSerializer() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonSerializer serializer = new JsonSerializer(out);

    ItemDocument id1 = Datamodel.makeItemDocument(DataObjectFactoryImplTest.getTestItemIdValue(1),
            Collections/*ww  w.j a va 2  s  . c o m*/
                    .<MonolingualTextValue>singletonList(Datamodel.makeMonolingualTextValue("Label1", "lang1")),
            Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(),
            DataObjectFactoryImplTest.getTestStatementGroups(1, 24, 1, EntityIdValue.ET_ITEM),
            Collections.<String, SiteLink>emptyMap());
    ItemDocument id2 = Datamodel.makeItemDocument(DataObjectFactoryImplTest.getTestItemIdValue(2),
            Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(),
            Collections.<MonolingualTextValue>emptyList(),
            DataObjectFactoryImplTest.getTestStatementGroups(2, 23, 1, EntityIdValue.ET_ITEM),
            Collections.<String, SiteLink>singletonMap("enwiki",
                    Datamodel.makeSiteLink("Title2", "enwiki", Collections.<String>emptyList())));
    PropertyDocument pd1 = Datamodel.makePropertyDocument(DataObjectFactoryImplTest.getTestPropertyIdValue(1),
            Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(),
            Collections
                    .<MonolingualTextValue>singletonList(Datamodel.makeMonolingualTextValue("Alias1", "lang1")),
            Collections.<StatementGroup>emptyList(),
            Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_COMMONS_MEDIA));

    serializer.open();
    serializer.processItemDocument(id1);
    serializer.processItemDocument(id2);
    serializer.processPropertyDocument(pd1);
    serializer.close();

    ArrayList<EntityDocument> inputDocuments = new ArrayList<>();
    inputDocuments.add(id1);
    inputDocuments.add(id2);
    inputDocuments.add(pd1);

    ArrayList<EntityDocument> outputDocuments = new ArrayList<>();

    ObjectMapper mapper = new ObjectMapper();
    ObjectReader documentReader = mapper.reader(JacksonTermedStatementDocument.class);

    MappingIterator<JacksonTermedStatementDocument> documentIterator = documentReader
            .readValues(out.toString());
    while (documentIterator.hasNextValue()) {
        JacksonTermedStatementDocument document = documentIterator.nextValue();
        document.setSiteIri("foo:");
        outputDocuments.add(document);
    }
    documentIterator.close();

    for (int i = 0; i < outputDocuments.size(); i++) {
        assertEquals(inputDocuments.get(i), outputDocuments.get(i));
    }
    assertEquals(serializer.getEntityDocumentCount(), 3);
}

From source file:logfile.LogfileStreamer.java

public void run() throws Exception {
    startElasticsearchIfNecessary();// ww w.j av  a 2s . com
    createIndexAndMappingIfNecessary();

    // index into the metrics index without date formatting
    ElasticsearchReporter reporter = ElasticsearchReporter.forRegistry(registry).hosts("localhost:9200")
            .indexDateFormat("").percolationNotifier(new HttpNotifier()).percolationFilter(MetricFilter.ALL)
            .build();
    reporter.start(60, TimeUnit.SECONDS);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    ObjectReader reader = objectMapper.reader(Map.class);
    MappingIterator<Map<String, Object>> iterator = reader.readValues(getInputStream());

    try {
        while (iterator.hasNextValue()) {
            Map<String, Object> entry = iterator.nextValue();
            if (entry.containsKey("_heartbeat_")) {
                heartbeatCounter.inc();
                continue;
            }

            if (entry.containsKey("ll") && entry.containsKey("t")) {
                long timestamp = ((Integer) entry.get("t")).longValue();
                List<Number> location = (List<Number>) entry.get("ll");
                double latitude = location.get(0).doubleValue();
                double longitude = location.get(1).doubleValue();

                addToBulkRequest(timestamp, latitude, longitude);
                entryMeter.mark(1);
            }
        }
    } finally {
        executeBulkRequest();
    }
}

From source file:org.wikidata.wdtk.dumpfiles.JsonDumpFileProcessor.java

/**
 * Process dump file data from the given input stream. This method uses the
 * efficient Jackson {@link MappingIterator}. However, this class cannot
 * recover from processing errors. If an error occurs in one entity, the
 * (presumably) less efficient processing method
 * {@link #processDumpFileContentsRecovery(InputStream)} is used instead.
 *
 * @see MwDumpFileProcessor#processDumpFileContents(InputStream, MwDumpFile)
 *//*  w  ww .j a v a 2  s  .c o m*/
@Override
public void processDumpFileContents(InputStream inputStream, MwDumpFile dumpFile) {

    logger.info("Processing JSON dump file " + dumpFile.toString());

    try {
        try {
            MappingIterator<JacksonTermedStatementDocument> documentIterator = documentReader
                    .readValues(inputStream);
            documentIterator.getParser().disable(Feature.AUTO_CLOSE_SOURCE);

            while (documentIterator.hasNextValue()) {
                JacksonTermedStatementDocument document = documentIterator.nextValue();
                handleDocument(document);
            }
            documentIterator.close();
        } catch (JsonProcessingException e) {
            logJsonProcessingException(e);
            processDumpFileContentsRecovery(inputStream);
        }
    } catch (IOException e) {
        throw new RuntimeException("Cannot read JSON input: " + e.getMessage(), e);
    }

}

From source file:org.wikidata.wdtk.client.JsonSerializationActionTest.java

@Test
public void testJsonGzipOutput() throws IOException {
    String[] args = new String[] { "-a", "json", "-o", "/path/to/output.json", "-z", "gz" };

    DirectoryManagerFactory.setDirectoryManagerClass(MockDirectoryManager.class);

    ClientConfiguration config = new ClientConfiguration(args);
    JsonSerializationAction jsa = (JsonSerializationAction) config.getActions().get(0);

    ItemIdValue subject1 = Datamodel.makeWikidataItemIdValue("Q42");
    MonolingualTextValue mtv1 = Datamodel.makeMonolingualTextValue("Test1", "en");
    MonolingualTextValue mtv2 = Datamodel.makeMonolingualTextValue("Test2", "fr");

    ItemDocument id1 = Datamodel.makeItemDocument(subject1, Arrays.asList(mtv1, mtv2), Arrays.asList(mtv1),
            Collections.<MonolingualTextValue>emptyList(), Collections.<StatementGroup>emptyList(),
            Collections.<String, SiteLink>emptyMap());

    jsa.open();/*w  w  w  .j ava 2  s .c o m*/
    jsa.processItemDocument(id1);
    jsa.close();

    MockDirectoryManager mdm = new MockDirectoryManager(Paths.get("/path/to/"), false);

    ObjectMapper mapper = new ObjectMapper();
    ObjectReader documentReader = mapper.reader(JacksonTermedStatementDocument.class);
    MappingIterator<JacksonTermedStatementDocument> documentIterator = documentReader
            .readValues(mdm.getInputStreamForFile("output.json.gz", CompressionType.GZIP));

    List<EntityDocument> results = new ArrayList<>();
    while (documentIterator.hasNextValue()) {
        JacksonTermedStatementDocument document = documentIterator.nextValue();
        document.setSiteIri(Datamodel.SITE_WIKIDATA);
        results.add(document);
    }
    documentIterator.close();

    assertEquals(1, results.size());
    assertEquals(id1, results.get(0));
}

From source file:org.wikidata.wdtk.client.JsonSerializationActionTest.java

@Test
public void testJsonBz2Output() throws IOException {
    String[] args = new String[] { "-a", "json", "-o", "output.json", "-z", "bz2" };

    DirectoryManagerFactory.setDirectoryManagerClass(MockDirectoryManager.class);

    ClientConfiguration config = new ClientConfiguration(args);
    JsonSerializationAction jsa = (JsonSerializationAction) config.getActions().get(0);

    ItemIdValue subject1 = Datamodel.makeWikidataItemIdValue("Q42");
    MonolingualTextValue mtv1 = Datamodel.makeMonolingualTextValue("Test1", "en");
    MonolingualTextValue mtv2 = Datamodel.makeMonolingualTextValue("Test2", "fr");

    ItemDocument id1 = Datamodel.makeItemDocument(subject1, Arrays.asList(mtv1, mtv2), Arrays.asList(mtv1),
            Collections.<MonolingualTextValue>emptyList(), Collections.<StatementGroup>emptyList(),
            Collections.<String, SiteLink>emptyMap());

    jsa.open();// w  w  w.j  av a  2s . co  m
    jsa.processItemDocument(id1);
    jsa.close();

    MockDirectoryManager mdm = new MockDirectoryManager(Paths.get("."), false);

    ObjectMapper mapper = new ObjectMapper();
    ObjectReader documentReader = mapper.reader(JacksonTermedStatementDocument.class);
    MappingIterator<JacksonTermedStatementDocument> documentIterator = documentReader
            .readValues(mdm.getInputStreamForFile("output.json.bz2", CompressionType.BZ2));

    List<EntityDocument> results = new ArrayList<>();
    while (documentIterator.hasNextValue()) {
        JacksonTermedStatementDocument document = documentIterator.nextValue();
        document.setSiteIri(Datamodel.SITE_WIKIDATA);
        results.add(document);
    }
    documentIterator.close();

    assertEquals(1, results.size());
    assertEquals(id1, results.get(0));
}