Example usage for com.fasterxml.jackson.databind ObjectReader readValues

List of usage examples for com.fasterxml.jackson.databind ObjectReader readValues

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectReader readValues.

Prototype

public <T> MappingIterator<T> readValues(URL src) throws IOException, JsonProcessingException 

Source Link

Document

Overloaded version of #readValue(InputStream) .

Usage

From source file:hrytsenko.gscripts.io.CsvFiles.java

/**
 * Load records from file./*from   w  w w . j  a v a  2  s. c om*/
 * 
 * @param args
 *            the named arguments.
 * 
 * @return the list of text records.
 * 
 * @throws AppException
 *             if file could not be loaded.
 */
public static List<Map<String, String>> loadCsv(Map<String, ?> args) {
    Path path = NamedArgs.findPath(args);
    LOGGER.info("Load {}.", path.getFileName());

    CsvSchema schema = schemaFrom(args).setUseHeader(true).build();

    try (InputStream dataStream = Files.newInputStream(path);
            InputStream bomStream = new BOMInputStream(dataStream);
            Reader dataReader = new InputStreamReader(bomStream, charsetFrom(args))) {
        CsvMapper mapper = new CsvMapper();
        ObjectReader reader = mapper.readerFor(Map.class).with(schema);
        return Lists.newArrayList(reader.readValues(dataReader));
    } catch (Exception exception) {
        throw new AppException(String.format("Could not load file %s.", path.getFileName()), exception);
    }
}

From source file:hrytsenko.csv.IO.java

/**
 * Gets records from file.//  ww w  .jav a  2  s.  c o  m
 * 
 * <p>
 * If closure is given, then it will be applied to each record.
 * 
 * @param args
 *            the named arguments {@link IO}.
 * @param closure
 *            the closure to be applied to each record.
 * 
 * @return the loaded records.
 * 
 * @throws IOException
 *             if file could not be read.
 */
public static List<Record> load(Map<String, ?> args, Closure<?> closure) throws IOException {
    Path path = getPath(args);
    LOGGER.info("Load: {}.", path.getFileName());

    try (InputStream dataStream = newInputStream(path, StandardOpenOption.READ);
            InputStream bomStream = new BOMInputStream(dataStream);
            Reader dataReader = new InputStreamReader(bomStream, getCharset(args))) {
        CsvSchema csvSchema = getSchema(args).setUseHeader(true).build();
        CsvMapper csvMapper = new CsvMapper();
        ObjectReader csvReader = csvMapper.reader(Map.class).with(csvSchema);

        Iterator<Map<String, String>> rows = csvReader.readValues(dataReader);
        List<Record> records = new ArrayList<>();
        while (rows.hasNext()) {
            Map<String, String> row = rows.next();
            Record record = new Record();
            record.putAll(row);
            records.add(record);

            if (closure != null) {
                closure.call(record);
            }
        }
        return records;
    }
}

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 {/*from  ww w .  j av  a 2  s  .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:ro.fortsoft.dataset.json.JsonDataSet.java

protected void init() {
    ObjectMapper mapper = new ObjectMapper();
    ObjectReader reader = mapper.reader(Map.class);

    try {//from  w w w . ja  v a  2  s. com
        rows = reader.readValues(inputStream);
    } catch (IOException e) {
        throw new DataSetException(e);
    }
}

From source file:web.StreamMeetupComTask.java

@Override
public Void call() {
    try {/*from   w  w w  . ja va  2s  .c  om*/
        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:ro.fortsoft.dataset.csv.CsvDataSet.java

protected MappingIterator<Map<String, Object>> createMappingIterator(InputStream inputStream) {
    CsvMapper mapper = createCsvMapper();
    CsvSchema schema = createCsvSchema();
    ObjectReader reader = mapper.reader(Map.class).with(schema);

    try {/* w w w.  j a v  a  2 s  . co  m*/
        return reader.readValues(inputStream);
    } catch (IOException e) {
        throw new DataSetException(e);
    }
}

From source file:logfile.LogfileStreamer.java

public void run() throws Exception {
    startElasticsearchIfNecessary();// w w w .java 2s . c o  m
    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.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/*from   w w  w.j a  v  a 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: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 w w  .j  av a2  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();//from  w ww. j  a  v  a 2  s.com
    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));
}