org.ojai.DocumentStream Java Examples

The following examples show how to use org.ojai.DocumentStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapRJsonTargetIT.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private int recordCount() {
  Table tab;
  try {
    tab = MapRDB.getTable(TABLE_NAME);
    DocumentStream stream = tab.find();
    Iterator<Document> iter = stream.iterator();
    int i = 0 ;
    while(iter.hasNext()) {
      iter.next();    // don't care about the returned Document.
      i++;
    }
    tab.close();
    return i;

  } catch (DBException ex) {
    throw ex;
  }
}
 
Example #2
Source File: JsonDocumentStream.java    From ojai with Apache License 2.0 6 votes vote down vote up
static DocumentStream newDocumentStream(FileSystem fs,
    Path path, Map<FieldPath, Type> map, Events.Delegate delegate)
        throws IllegalArgumentException, IOException {
  final InputStream in = fs.open(path);
  return new JsonDocumentStream(in, map, delegate) {
    @Override
    public void close() {
      try {
        super.close();
      } finally {
        try {
          in.close();
        } catch (IOException e) {
          throw new OjaiException(e);
        }
      }
    }
  };
}
 
Example #3
Source File: TestJsonDocumentStream.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testDocumentIterator() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/multidocument.json");
       DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    Iterator<Document> it = stream.iterator();
    Document document;
    while (it.hasNext()) {
      document = it.next();
      testDocumentElements(document);
      documentCount++;
    }
    assertEquals(4, documentCount);
  }
}
 
Example #4
Source File: TestDocumentReaderWithProjection.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testProjectChildParentAndChild() throws Exception {
  try (InputStream testJson = getJsonStream("org/ojai/test/data/test1.json");
      DocumentStream stream = Json.newDocumentStream(testJson);) {
    DocumentReader reader = stream.iterator().next().asReader();
    FieldProjector projector = new FieldProjector("k.l.l1", "k", "k.l.l1");

    DocumentReaderWithProjection r = new DocumentReaderWithProjection(reader, projector);

    assertMapEvent(r, EventType.START_MAP, null);

    assertMapEvent(r, EventType.START_MAP, "k");
    assertMapEvent(r, EventType.START_MAP, "l");
    assertMapEvent(r, EventType.START_MAP, "l1");
    assertMapEvent(r, EventType.DOUBLE, "l2");
    assertMapEvent(r, EventType.END_MAP, "l1");
    assertMapEvent(r, EventType.DOUBLE, "l3");
    assertMapEvent(r, EventType.END_MAP, "l");
    assertMapEvent(r, EventType.END_MAP, "k");

    assertMapEvent(r, EventType.END_MAP, null);

    assertNull(r.next());
  }
}
 
Example #5
Source File: TestJsonDocumentEquals.java    From ojai with Apache License 2.0 6 votes vote down vote up
private void testResources(String resource) throws IOException {
  try (InputStream testJson = getJsonStream(resource);
       DocumentStream stream = Json.newDocumentStream(testJson);
       InputStream testJson1 = getJsonStream(resource);
       DocumentStream stream1 = Json.newDocumentStream(testJson1);
       InputStream testJson2 = getJsonStream(resource);
       DocumentStream stream2 = Json.newDocumentStream(testJson2);) {
    for (Document document : stream) {
      assertEquals(document, document); // self comparison
    }
    // same documents extracted from different streams
    Iterator<Document> itr1 = stream1.iterator();
    Iterator<Document> itr2 = stream2.iterator();
    while (itr1.hasNext()) {
      assertEquals(itr1.next(), itr2.next());
    }
  }
}
 
Example #6
Source File: CdcStatisticService.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void recomputeStatistics() {

    MaprDbDao.OjaiStoreAction<Long> countAction = ((connection, store) -> {

        long total = 0;
        DocumentStream documentStream = store.find("_id");
        for (Document document : documentStream) {
            total++;
        }

        return total;
    });

    long albumsTotal = albumDao.processStore(countAction);
    Statistic albumsStatistic = getStatisticForTable(ALBUMS_TABLE_NAME);
    albumsStatistic.setDocumentNumber(albumsTotal);
    statisticDao.update(ALBUMS_TABLE_NAME, albumsStatistic);

    long artistsTotal = artistDao.processStore(countAction);
    Statistic artistsStatistic = getStatisticForTable(ARTISTS_TABLE_NAME);
    artistsStatistic.setDocumentNumber(artistsTotal);
    statisticDao.update(ARTISTS_TABLE_NAME, artistsStatistic);
}
 
Example #7
Source File: MaprDbDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of document according to specified <code>offset</code> and <code>limit</code> values using
 * projection. Documents will be ordered according to the specified {@link SortOption} options.
 *
 * @param offset      offset value.
 * @param limit       limit value.
 * @param sortOptions define the order of documents.
 * @param fields      list of fields that will present in document.
 * @return list of documents.
 */
public List<T> getList(long offset, long limit, List<SortOption> sortOptions, String... fields) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        Query query = buildQuery(connection, offset, limit, fields, sortOptions);

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<T> documents = new ArrayList<>();
        for (Document document : documentStream) {
            T doc = mapOjaiDocument(document);
            if (doc != null) {
                documents.add(doc);
            }
        }

        log.debug("Get list of '{}' documents from '{}' table with offset: '{}', limit: '{}', sortOptions: '{}', " +
                        "fields: '{}'. Elapsed time: {}", documents.size(), tablePath, offset, limit, sortOptions,
                (fields != null) ? Arrays.asList(fields) : "[]", stopwatch);

        return documents;
    });
}
 
Example #8
Source File: MaprDbDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of all documents.
 *
 * @return list of documents.
 */
public List<T> getList() {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Fetch all OJAI Documents from this store
        DocumentStream documentStream = store.find();
        List<T> documents = new ArrayList<>();
        for (Document document : documentStream) {
            T doc = mapOjaiDocument(document);
            if (doc != null) {
                documents.add(doc);
            }
        }

        log.debug("Get list of '{}' documents from '{}' table. Elapsed time: {}", documents.size(), tablePath,
                stopwatch);

        return documents;
    });
}
 
Example #9
Source File: TestJsonDocumentStreamFormat.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseStreamWithManyArrays() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/manyarray.json");
      DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    for (DocumentReader reader : stream.documentReaders()) {
      documentCount++;
      if (documentCount == 1) {
        validateDocumentReaderOne(reader);
      }
      if (documentCount == 2) {
        validateDocumentReaderTwo(reader);
      }
      if (documentCount == 3){
        validateDocumentReaderThree(reader);
      }
      if (documentCount == 5) {
        validateDocumentReaderFive(reader);
      }
    }
    assertEquals(5, documentCount);
  }
}
 
Example #10
Source File: TestDocumentReaderWithProjection.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testProjectScalarFieldAsContainer() throws Exception {
  try (InputStream testJson = getJsonStream("org/ojai/test/data/test1.json");
      DocumentStream stream = Json.newDocumentStream(testJson);) {
    DocumentReader reader = stream.iterator().next().asReader();
    FieldProjector projector = new FieldProjector("c.c1", "f.g.h[2]");

    DocumentReaderWithProjection r = new DocumentReaderWithProjection(reader, projector);

    assertMapEvent(r, EventType.START_MAP, null);

    assertMapEvent(r, EventType.START_MAP, "c");
    assertMapEvent(r, EventType.END_MAP, "c");

    assertMapEvent(r, EventType.START_MAP, "f");
    assertMapEvent(r, EventType.START_MAP, "g");
    assertMapEvent(r, EventType.START_ARRAY, "h");
    assertMapEvent(r, EventType.END_ARRAY, "h");
    assertMapEvent(r, EventType.END_MAP, "g");
    assertMapEvent(r, EventType.END_MAP, "f");

    assertMapEvent(r, EventType.END_MAP, null);

    assertNull(r.next());
  }
}
 
Example #11
Source File: TestJsonDocumentStreamFormat.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testFetchAndParseJsonDocumentStream() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/manydocs.json");
      DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    for (DocumentReader reader : stream.documentReaders()) {
      documentCount++;
      if (documentCount == 1) {
        validateDocumentReaderOne(reader);
      } else if (documentCount == 2) {
        validateDocumentReaderTwo(reader);
      } else {
        validateDocumentReaderThree(reader);
      }
    }
    assertEquals(3, documentCount);
  }
}
 
Example #12
Source File: TestDocumentReaderWithProjection.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testProjectParentAndChild() throws Exception {
  try (InputStream testJson = getJsonStream("org/ojai/test/data/test1.json");
      DocumentStream stream = Json.newDocumentStream(testJson);) {
    DocumentReader reader = stream.iterator().next().asReader();
    FieldProjector projector = new FieldProjector("k", "k.l.l1");

    DocumentReaderWithProjection r = new DocumentReaderWithProjection(reader, projector);

    assertMapEvent(r, EventType.START_MAP, null);

    assertMapEvent(r, EventType.START_MAP, "k");
    assertMapEvent(r, EventType.START_MAP, "l");
    assertMapEvent(r, EventType.START_MAP, "l1");
    assertMapEvent(r, EventType.DOUBLE, "l2");
    assertMapEvent(r, EventType.END_MAP, "l1");
    assertMapEvent(r, EventType.DOUBLE, "l3");
    assertMapEvent(r, EventType.END_MAP, "l");
    assertMapEvent(r, EventType.END_MAP, "k");

    assertMapEvent(r, EventType.END_MAP, null);

    assertNull(r.next());
  }
}
 
Example #13
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of Artist rates by artist identifier.
 *
 * @param artistId artist's identifier.
 * @return list of Artist rates.
 */
public List<ArtistRate> getByArtistId(String artistId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        Query query = connection.newQuery().where(
                connection.newCondition()
                        .is("document_id", QueryCondition.Op.EQUAL, artistId)
                        .build()
        ).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<ArtistRate> rates = new ArrayList<>();
        for (Document document : documentStream) {
            ArtistRate rate = mapOjaiDocument(document);
            if (rate != null) {
                rates.add(rate);
            }
        }

        log.debug("Get '{}' rates by artist id '{}' took {}", rates.size(), artistId, stopwatch);

        return rates;
    });
}
 
Example #14
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of Artist rates by user identifier.
 *
 * @param userId user's identifier.
 * @return list of Artist rates.
 */
public List<ArtistRate> getByUserId(String userId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        Query query = connection.newQuery().where(
                connection.newCondition()
                        .is("user_id", QueryCondition.Op.EQUAL, userId)
                        .build()
        ).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<ArtistRate> rates = new ArrayList<>();
        for (Document document : documentStream) {
            ArtistRate rate = mapOjaiDocument(document);
            if (rate != null) {
                rates.add(rate);
            }
        }

        log.debug("Get '{}' rates by user id '{}' took {}", rates.size(), userId, stopwatch);

        return rates;
    });
}
 
Example #15
Source File: AlbumDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns number of albums according to the specified language.
 *
 * @param language language code.
 * @return number of albums with specified language code.
 */
public long getTotalNumByLanguage(String language) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        QueryCondition languageEqualsCondition = connection.newCondition()
                .is("language", QueryCondition.Op.EQUAL, language)
                .build();

        Query query = connection.newQuery()
                .select("_id")
                .where(languageEqualsCondition)
                .build();

        DocumentStream documentStream = store.findQuery(query);
        long totalNum = 0;
        for (Document ignored : documentStream) {
            totalNum++;
        }

        log.debug("Counting '{}' albums by language '{}' took {}", totalNum, language, stopwatch);

        return totalNum;
    });
}
 
Example #16
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Returns Artist rate according to the specified user identifier and artist identifier.
 *
 * @param userId   user identifier.
 * @param artistId artist identifier.
 * @return artist rate.
 */
public ArtistRate getRate(String userId, String artistId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        QueryCondition condition = connection.newCondition()
                .and()
                .is("user_id", QueryCondition.Op.EQUAL, userId)
                .is("document_id", QueryCondition.Op.EQUAL, artistId)
                .close()
                .build();

        Query query = connection.newQuery().where(condition).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        Iterator<Document> documentIterator = documentStream.iterator();

        if (!documentIterator.hasNext()) {
            return null;
        }

        log.debug("Get rate by artist id '{}' and user id '{}' took {}", artistId, userId, stopwatch);

        return mapOjaiDocument(documentIterator.next());
    });
}
 
Example #17
Source File: AlbumRateDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Returns Album rate according to the specified user identifier and album identifier.
 *
 * @param userId  user identifier.
 * @param albumId album identifier.
 * @return album rate.
 */
public AlbumRate getRate(String userId, String albumId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        QueryCondition condition = connection.newCondition()
                .and()
                .is("user_id", QueryCondition.Op.EQUAL, userId)
                .is("document_id", QueryCondition.Op.EQUAL, albumId)
                .close()
                .build();

        Query query = connection.newQuery().where(condition).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        Iterator<Document> documentIterator = documentStream.iterator();

        if (!documentIterator.hasNext()) {
            return null;
        }

        log.debug("Get rate by album id '{}' and user id '{}' took {}", albumId, userId, stopwatch);

        return mapOjaiDocument(documentIterator.next());
    });
}
 
Example #18
Source File: AlbumRateDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Returns list of Album rates by album identifier.
 *
 * @param albumId album's identifier.
 * @return list of Album rates.
 */
public List<AlbumRate> getByAlbumId(String albumId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        Query query = connection.newQuery().where(
                connection.newCondition()
                        .is("document_id", QueryCondition.Op.EQUAL, albumId)
                        .build()
        ).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<AlbumRate> rates = new ArrayList<>();
        for (Document document : documentStream) {
            AlbumRate rate = mapOjaiDocument(document);
            if (rate != null) {
                rates.add(rate);
            }
        }

        log.debug("Get '{}' rates by album id '{}' took {}", rates.size(), albumId, stopwatch);

        return rates;
    });
}
 
Example #19
Source File: TestTypeMappedJsonDocumentReader.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeMappingsMultiLevelWithTags() throws IOException {
  Map<FieldPath, Value.Type> fieldPathTypeMap = Maps.newHashMap();
  fieldPathTypeMap.put(FieldPath.parseFrom("map.boolean"), Value.Type.BOOLEAN);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.string"), Value.Type.STRING);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.byte"), Value.Type.BYTE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.short"), Value.Type.SHORT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.int"), Value.Type.INT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.long"), Value.Type.LONG);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.float"), Value.Type.FLOAT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.double"), Value.Type.DOUBLE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.decimal"), Value.Type.DECIMAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.date"), Value.Type.DATE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.time"), Value.Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.timestamp"), Value.Type.TIMESTAMP);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.interval"), Value.Type.INTERVAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.binary"), Value.Type.BINARY);

  try (InputStream in = getJsonStream("org/ojai/test/data/test4.json");
       DocumentStream jsonRecordStream = Json.newDocumentStream(in, fieldPathTypeMap)) {
    Iterator<Document> docIterator = jsonRecordStream.iterator();
    assertTrue(docIterator.hasNext());
    Document doc = docIterator.next();
    assertTrue(doc.getBoolean("map.boolean"));
    assertEquals("eureka", doc.getString("map.string"));
    assertEquals((byte) 127, doc.getByte("map.byte"));
    assertEquals((short) 32767, doc.getShort("map.short"));
    assertEquals(2147483647, doc.getInt("map.int"));
    assertEquals(9223372036854775807L, doc.getLong("map.long"));
    assertEquals((float) 3.4028235, doc.getFloat("map.float"), 0);
    assertEquals(1.7976931348623157e308, doc.getDouble("map.double"), 0);
    assertEquals(ODate.parse("2012-10-20"), doc.getDate("map.date"));
    assertEquals(OTime.parse("07:42:46.123"), doc.getTime("map.time"));
    assertEquals(OTimestamp.parse("2012-10-20T07:42:46.123-07:00"), doc.getTimestamp("map.timestamp"));
    assertEquals(new OInterval(172800000), doc.getInterval("map.interval"));
    assertEquals(Values.parseBinary("YWJjZA=="), doc.getBinary("map.binary"));
  }
}
 
Example #20
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testToString() throws IOException {
  try (InputStream in = getJsonStream("org/ojai/test/data/test.json");
      DocumentStream stream = Json.newDocumentStream(in)) {
    Document doc = stream.iterator().next();
    assertEquals(docStrWithoutTags, doc.toString());
    assertEquals(docStrWithoutTags, doc.asJsonString());
    assertEquals(docStrWithTags, doc.asJsonString(JsonOptions.WITH_TAGS));
  }

}
 
Example #21
Source File: TestJsonDocumentReader.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDOMReader() throws Exception {
  try (InputStream testJson = getJsonStream("org/ojai/test/data/test2.json");
       DocumentStream stream = Json.newDocumentStream(testJson);) {
    DocumentReader r = stream.iterator().next().asReader();
    testReader(r);
  }
}
 
Example #22
Source File: TestJsonUtil.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonSerialization() throws Exception {
  try (InputStream in = getJsonStream("multidocument.json");
       DocumentStream stream = Json.newDocumentStream(in)) {
    for (DocumentReader reader : stream.documentReaders()) {
      DocumentBuilder builder = Json.newDocumentBuilder();
      Documents.writeReaderToBuilder(reader, builder);
      logger.info(builder.toString());
    }
  }
}
 
Example #23
Source File: TestJsonDocumentStream.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentListenerError() throws IOException {
  final Document status = Json.newDocument();
  try (InputStream in = getJsonStream("org/ojai/test/data/multidocument.json");
      DocumentStream stream = Json.newDocumentStream(in)) {

    stream.iterator(); // open an iterator and ignore it

    stream.streamTo(new DocumentListener() {
      @Override
      public boolean documentArrived(Document document) {
        status.set("documentArrived", true);
        return false;
      }

      @Override
      public void failed(Exception e) {
        status.set("failed", true);
      }

      @Override
      public void eos() {
        status.set("eos", true);
      }
    });

    assertNull(status.getBooleanObj("documentArrived"));
    assertEquals(true, status.getBoolean("failed"));
    assertNull(status.getBooleanObj("eos"));
  }
}
 
Example #24
Source File: TestJsonDocumentStream.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentListener() throws IOException {
  final Document status = Json.newDocument();

  try (InputStream in = getJsonStream("org/ojai/test/data/multidocument.json");
      DocumentStream stream = Json.newDocumentStream(in)) {
    stream.streamTo(new DocumentListener() {
      int documentProcessed = 0;
      @Override
      public boolean documentArrived(Document document) {
        documentProcessed++;
        status.set("documentArrived", true);
        logger.info("Document arrived: %s", document.asJsonString());
        if ("id3".equals(document.getString("business_id"))) {
          return false;
        } else {
          return true;
        }
      }

      @Override
      public void failed(Exception e) {
        status.set("failed", true);
      }

      @Override
      public void eos() {
        status.set("eos", true);
        assertEquals(3, documentProcessed);
      }
    });

    assertEquals(true, status.getBoolean("documentArrived"));
    assertNull(status.getBooleanObj("failed"));
    assertEquals(true, status.getBoolean("eos"));
  }
}
 
Example #25
Source File: TestEmptyDocumentStream.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void test_EmptyDocumentStream() {
  // Document iterator
  final DocumentStream stream1 = new EmptyDocumentStream();
  assertFalse(stream1.iterator().hasNext());
  stream1.close();

  // DocumentReader iterator
  final DocumentStream stream2 = new EmptyDocumentStream();
  assertFalse(stream2.documentReaders().iterator().hasNext());
  stream2.close();

  // streamTo
  final AtomicReference<Boolean> eosCalled = new AtomicReference<Boolean>(false);
  final AtomicReference<Boolean> failedCalled = new AtomicReference<Boolean>(false);
  final AtomicReference<Boolean> documentArrivedCalled = new AtomicReference<Boolean>(false);
  final DocumentStream stream3 = new EmptyDocumentStream();
  stream3.streamTo(new DocumentListener() {
    @Override
    public void eos() {
      eosCalled.set(true);
    }

    @Override
    public void failed(Exception e) {
      failedCalled.set(true);
    }

    @Override
    public boolean documentArrived(Document document) {
      documentArrivedCalled.set(true);
      return false;
    }
  });
  assertTrue(eosCalled.get());
  assertFalse(failedCalled.get());
  assertFalse(documentArrivedCalled.get());
  stream3.close();
}
 
Example #26
Source File: TestJsonDocumentStream.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testFetchAndParsePartiallyJsonDocumentStream() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/business.json");
       DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    for (DocumentReader reader : stream.documentReaders()) {
      documentCount++;
      logger.debug("First event in the DocumentReader: " + reader.next());
    }
    assertEquals(5, documentCount);
  }
}
 
Example #27
Source File: TestJsonDocumentStream.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testFetchAndParseTypeMappedJsonDocumentStream() throws Exception {
  Map<FieldPath, Type> fieldPathTypeMap = Maps.newHashMap();
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Monday.open"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Monday.close"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Tuesday.open"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Tuesday.close"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Wednesday.open"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Wednesday.close"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Thursday.open"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Thursday.close"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Friday.open"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Friday.close"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Saturday.open"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Saturday.close"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Sunday.close"), Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("hours.Sunday.close"), Type.TIME);

  try (InputStream in = getJsonStream("org/ojai/test/data/business.json");
       DocumentStream stream = Json.newDocumentStream(in, fieldPathTypeMap)) {

    int documentCount = 0;
    for (DocumentReader reader : stream.documentReaders()) {
      documentCount++;
      logger.debug(Json.toJsonString(reader, JsonOptions.WITH_TAGS));
    }
    assertEquals(5, documentCount);
  }
}
 
Example #28
Source File: TestJsonDocumentStream.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testFetchAndParseJsonDocumentStream() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/business.json");
       DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    for (DocumentReader reader : stream.documentReaders()) {
      documentCount++;
      testDocumentReaderFromIterator(reader);
    }
    assertEquals(5, documentCount);
  }
}
 
Example #29
Source File: TestJsonDocumentStreamFormat.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseStreamWithManyMiscDocs() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/manymiscdocs.json");
      DocumentStream stream = Json.newDocumentStream(in)) {

    int documentCount = 0;
    for (DocumentReader reader : stream.documentReaders()) {
      documentCount++;
      if (documentCount == 1) {
        validateDocumentReaderOne(reader);
      }
      if (documentCount == 2) {
        validateDocumentReaderTwo(reader);
      }
      if (documentCount == 3){
        validateDocumentReaderThree(reader);
      }
      if (documentCount == 5) {
        validateMapDocBetweenArrays(reader);
      }
      if (documentCount == 6) {
        validateDocumentReaderFive(reader);
      }
    }
    assertEquals(7, documentCount);
  }
}
 
Example #30
Source File: TestJsonDocumentStreamFormat.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyJsonFile() throws Exception {
  try (InputStream in = getJsonStream("org/ojai/test/data/nodocs.json");
       DocumentStream stream = Json.newDocumentStream(in)) {
    int documentCount = getDocumentCount(stream.documentReaders());
    assertEquals(0, documentCount);
  }
}