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

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

Introduction

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

Prototype

@Override
    public void close() throws IOException 

Source Link

Usage

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 {/*from  w  ww.  j a v a2  s.com*/
            return iterator.next();
        } finally {
            iterator.close();
        }

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

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 a  va2s .  c  o m
        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.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  v  a2s .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: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  w  w. j a v  a 2  s.  c  om*/
@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();//from   w  ww.  j  a  v a 2 s.co 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();/*from   w w w.  j av  a 2 s.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));
}

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

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

    DirectoryManagerFactory.setDirectoryManagerClass(MockDirectoryManager.class);

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

    ItemIdValue subject1 = Datamodel.makeWikidataItemIdValue("Q42");
    ItemIdValue subject2 = Datamodel.makeWikidataItemIdValue("Q43");
    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());

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

    PropertyDocument pd1 = Datamodel.makePropertyDocument(Datamodel.makeWikidataPropertyIdValue("P31"),
            Arrays.asList(mtv1), Collections.<MonolingualTextValue>emptyList(), Arrays.asList(mtv1),
            Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_MONOLINGUAL_TEXT));

    jsa.open();/*  www.j  a v  a 2 s  .com*/
    jsa.processItemDocument(id1);
    jsa.processPropertyDocument(pd1);
    jsa.processItemDocument(id2);
    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", CompressionType.NONE));

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

    assertEquals(3, results.size());
    assertEquals(id1, results.get(0));
    assertEquals(pd1, results.get(1));
    assertEquals(id2, results.get(2));

}

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  ww . ja va  2  s .co  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("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.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 .j a  va  2  s .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("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.unisa.storm.bolt.ExchMsgParserBolt.java

public void execute(Tuple input, BasicOutputCollector collector) {
    LOGGER.debug("Parsing incomming Exchange Message:");
    String msg = input.getString(0);
    try {//from   ww  w . j  a va2 s . c om
        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);
    }
}