org.ojai.Document Java Examples

The following examples show how to use org.ojai.Document. 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: MaprDbDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Creates single document.
 *
 * @param entity contains info for document, which will be created.
 * @return created document.
 */
public T create(T entity) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create an OJAI Document form the Java bean (there are other ways too)
        final Document createdOjaiDoc = connection.newDocument(entity);

        // Set update info if available
        getUpdateInfo().ifPresent(updateInfo -> createdOjaiDoc.set("update_info", updateInfo));

        // Insert the document into the OJAI store
        store.insertOrReplace(createdOjaiDoc);

        log.debug("Create document '{}' at table: '{}'. Elapsed time: {}", createdOjaiDoc, tablePath, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(createdOjaiDoc);
    });
}
 
Example #2
Source File: MaprMusicElasticSearchService.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
private void indexJSONTableDocuments(TransportClient client, String indexName, String typeName, String tablePath, String... fields) {

        loginTestUser(TEST_USER_NAME, TEST_USER_GROUP);

        // Create an OJAI connection to MapR cluster
        Connection connection = DriverManager.getConnection(CONNECTION_URL);

        // Get an instance of OJAI DocumentStore
        final DocumentStore store = connection.getStore(tablePath);

        DocumentStream documentStream = store.find(fields);
        for (Document document : documentStream) {

            IndexResponse response = client.prepareIndex(indexName, typeName, document.getId().getString())
                    .setSource(document.asJsonString(), XContentType.JSON)
                    .get();

            log.info("Elasticsearch Index Response: '{}'", response);
        }

        // Close this instance of OJAI DocumentStore
        store.close();

        // Close the OJAI connection and release any resources held by the connection
        connection.close();
    }
 
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: TestJsonDocument.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsReaderLeaf() {
  Document document = Json.newDocument();
  document.set(FIELD_MAP_BYTE, (byte)127);
  document.set("map.num", 12345);
  Map<String, Object> m = new HashMap<String, Object>();
  m.put("first", "John");
  m.put("last", "Doe");
  m.put("age", (short)45);
  document.set("map.info", m);
  DocumentReader myReader = document.asReader("map.info.age");
  EventType event;
  int numtokens = 0;
  while ((event = myReader.next()) != null) {
    if (event == EventType.SHORT) {
      numtokens++;
      assertEquals((short)45, myReader.getShort());
    }
  }
  assertEquals(1, numtokens);
}
 
Example #5
Source File: TestValues.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void test_asNumber() throws IOException {
  URL url = Resources.getResource("org/ojai/test/data/test5.json");
  String content = Resources.toString(url, Charsets.UTF_8);
  Document document = Json.newDocument(content);

  assertEquals(127, (byte)Values.asNumber(document.getValue("map.byteTrue")));
  assertEquals(32767, (short)Values.asNumber(document.getValue("map.shortTrue")));
  assertEquals(2147483647, (int)Values.asNumber(document.getValue("map.intTrue")));
  assertEquals(3.015625f, (float)Values.asNumber(document.getValue("map.floatTrue")), 0.0);
  assertEquals(9223372036854775807L, (long)Values.asNumber(document.getValue("map.longTrue")));
  assertEquals(1.7976931348623157E308, (double)Values.asNumber(document.getValue("map.doubleTrue")), 0.0);
  assertEquals(new BigDecimal("123456789012345678901234567890123456789012345678901.23456789"),
      Values.asNumber(document.getValue("map.decimalTrue")));

  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.null")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.booleanTrue")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.string")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.date")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.time")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.timestamp")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.interval")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.binary")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.array")); });
  expectException(TYPE_EXCEPTION, () -> { Values.asNumber(document.getValue("map.map")); });
}
 
Example #6
Source File: StatisticDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param id        identifier of document, which will be updated.
 * @param id        identifier of document, which will be updated.
 * @param statistic statistic.
 * @return updated statistic.
 */
@Override
public Statistic update(String id, Statistic statistic) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create a DocumentMutation to update non-null fields
        DocumentMutation mutation = connection.newMutation();

        // Update only non-null fields
        if (statistic.getDocumentNumber() != null) {
            mutation.set("document_number", statistic.getDocumentNumber());
        }

        // Update the OJAI Document with specified identifier
        store.update(id, mutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #7
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param id         identifier of document, which will be updated.
 * @param artistRate artist rate.
 * @return updated artist rate.
 */
@Override
public ArtistRate update(String id, ArtistRate artistRate) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        // Create a DocumentMutation to update non-null fields
        DocumentMutation mutation = connection.newMutation();

        // Update only non-null fields
        if (artistRate.getRating() != null) {
            mutation.set("rating", artistRate.getRating());
        }

        // Update the OJAI Document with specified identifier
        store.update(id, mutation);

        Document updatedOjaiDoc = store.findById(id);

        log.debug("Update document from table '{}' with id: '{}'. Elapsed time: {}", tablePath, id, stopwatch);

        // Map Ojai document to the actual instance of model class
        return mapOjaiDocument(updatedOjaiDoc);
    });
}
 
Example #8
Source File: TestJsonDocumentEquals.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateTimeEquals() {
  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

  // original time
  cal.set(2015, 12, 31, 11, 59, 59);
  long ts1 = cal.getTime().getTime();

  // change date, keep the time same
  cal.set(2015, 12, 30, 11, 59, 59);
  long ts2 = cal.getTime().getTime();

  // change time, keep the date same
  cal.set(2015, 12, 31, 11, 59, 58);
  long ts3 = cal.getTime().getTime();


  Document r = Json.newDocument()
      .set("date1", new ODate(ts1))
      .set("date2", new ODate(ts2))
      .set("date3", new ODate(ts3))
      .set("time1", new OTime(ts1))
      .set("time2", new OTime(ts2))
      .set("time3", new OTime(ts3));

  assertEquals(r.getValue("date1"), new ODate(ts1));
  assertNotEquals(r.getValue("date1"), new ODate(ts2));
  assertEquals(r.getValue("date1"), new ODate(ts3));
  assertEquals(r.getValue("date1"), r.getValue("date3"));
  assertNotEquals(r.getValue("date1"), r.getValue("date2"));

  assertEquals(r.getValue("time1"), new OTime(ts1));
  assertEquals(r.getValue("time1"), new OTime(ts2));
  assertNotEquals(r.getValue("time1"), new OTime(ts3));
  assertEquals(r.getValue("time1"), r.getValue("time2"));
  assertNotEquals(r.getValue("time1"), r.getValue("time3"));
}
 
Example #9
Source File: JsonValueBuilder.java    From ojai with Apache License 2.0 5 votes vote down vote up
public static JsonValue initFrom(Document value) {
  if (value instanceof JsonDocument) {
    return ((JsonDocument) value).shallowCopy();
  }

  JsonDocument r = new JsonDocument();
  for (Map.Entry<String, Value> e : value) {
    r.set(e.getKey(), initFromObject(e.getValue()));
  }
  return r;
}
 
Example #10
Source File: DocumentStreamBase.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public void streamTo(DocumentListener docListener) {
  try {
    for(Document doc : this) {
      docListener.documentArrived(doc);
    }
    docListener.eos();
  } catch(Exception ex) {
    docListener.failed(ex);
  }
}
 
Example #11
Source File: TestValues.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void test_asString() throws IOException {
  URL url = Resources.getResource("org/ojai/test/data/test5.json");
  String content = Resources.toString(url, Charsets.UTF_8);
  Document document = Json.newDocument(content);

  assertEquals(null, Values.asString(document.getValue("map.null")));
  assertEquals("true", Values.asString(document.getValue("map.booleanTrue")));
  assertEquals("false", Values.asString(document.getValue("map.booleanFalse")));
  assertEquals("True", Values.asString(document.getValue("map.stringTrue")));
  assertEquals("faLse", Values.asString(document.getValue("map.stringFalse")));
  assertEquals("127", Values.asString(document.getValue("map.byteTrue")));
  assertEquals("0", Values.asString(document.getValue("map.byteFalse")));
  assertEquals("32767", Values.asString(document.getValue("map.shortTrue")));
  assertEquals("0", Values.asString(document.getValue("map.shortFalse")));
  assertEquals("2147483647", Values.asString(document.getValue("map.intTrue")));
  assertEquals("0", Values.asString(document.getValue("map.intFalse")));
  assertEquals("9223372036854775807", Values.asString(document.getValue("map.longTrue")));
  assertEquals("0", Values.asString(document.getValue("map.longFalse")));
  assertEquals("3.015625", Values.asString(document.getValue("map.floatTrue")));
  assertEquals("0.0", Values.asString(document.getValue("map.floatFalse")));
  assertEquals("1.7976931348623157E308", Values.asString(document.getValue("map.doubleTrue")));
  assertEquals("0", Values.asString(document.getValue("map.doubleFalse")));
  assertEquals("123456789012345678901234567890123456789012345678901.23456789", Values.asString(document.getValue("map.decimalTrue")));
  assertEquals("0.0", Values.asString(document.getValue("map.decimalFalse")));

  assertEquals("eureka", Values.asString(document.getValue("map.string")));
  assertEquals("", Values.asString(document.getValue("map.stringEmpty")));
  assertEquals("2012-10-20", Values.asString(document.getValue("map.date")));
  assertEquals("07:42:46.123", Values.asString(document.getValue("map.time")));
  assertEquals("2012-10-20T14:42:46.123Z", Values.asString(document.getValue("map.timestamp")));
  assertEquals("172800000", Values.asString(document.getValue("map.interval")));
  assertEquals("\"YWJjZA==\"", Values.asString(document.getValue("map.binary")));
  assertEquals("[42,\"open sesame\",3.14,\"2015-01-21\"]", Values.asString(document.getValue("map.array")));
  assertEquals("{\"a\":4,\"b\":\"c\"}", Values.asString(document.getValue("map.map")));
}
 
Example #12
Source File: TestJsonDocumentBuilder.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayAndMapWithinMap() {
  JsonDocumentBuilder w = (JsonDocumentBuilder)Json.newDocumentBuilder();
  w.addNewMap();
  w.putNewArray("array");
  w.add("abcd");
  Document r = Json.newDocument();
  List<Object> l = new ArrayList<Object>();
  l.add(123);
  l.add(456);
  r.set("list", l);
  w.add(r.getValue("list"));
  w.endArray();
  Map<String, Object> m = new HashMap<String, Object>();
  Map<String, Object> m2 = new HashMap<String, Object>();
  m2.put("a1", true);
  m2.put("a2", 11.456);
  List<Object> ll = new ArrayList<Object>();
  ll.add(new BigDecimal(111.11111));
  m2.put("arr2",ll);
  m.put("a3", 5555);
  m.put("map", m2);
  w.put("f", m);
  w.endMap();
  r = w.getDocument();

  assertEquals(5555, r.getInt("f.a3"));
  assertEquals(true, r.getBoolean("f.map.a1"));

}
 
Example #13
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 #14
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 #15
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testJSONDocumentWithArrayAbsIndex() {
  Document doc = Json.newDocument().set("c[0]",true).set("c[3]", "mapr").set("a.b", 10);

  assertTrue(doc.getBoolean("c[0]"));
  assertEquals(Type.NULL, doc.getValue("c[1]").getType());
  assertEquals(Type.NULL, doc.getValue("c[2]").getType());
  assertEquals("mapr", doc.getString("c[3]"));
}
 
Example #16
Source File: JsonDocumentIterator.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public Document next() {
  if (!hasNext()) {
    throw new NoSuchElementException("next() called after hasNext() returned false");
  }
  Document rec = getDocumentFromStreamReader();
  reader = null;
  return rec;
}
 
Example #17
Source File: MapRJsonOriginSourceIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private Document createDocument(String json, int key, boolean binary) {
  Document doc = MapRDB.newDocument(json);
  if(binary) {
    byte[] barr = Bytes.toBytes(key);
    doc.setId(ByteBuffer.wrap(barr));
  } else {
    doc.setId("" + key);
  }
  return doc;
}
 
Example #18
Source File: TestJsonDocumentBuilder.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unused")
public void testPutListAsValue() {
  DocumentBuilder jsonDocumentBuilder = Json.newDocumentBuilder();
  jsonDocumentBuilder.addNewMap();
  List<Object> list = new ArrayList<>();
  list.add(1);
  list.add("2");
  Value v = JsonValueBuilder.initFrom(list);
  jsonDocumentBuilder.put("value", v);
  jsonDocumentBuilder.endMap();
  Document r = jsonDocumentBuilder.getDocument();

}
 
Example #19
Source File: DocumentBase.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document setArray(String fieldPath, int[] values) {
  return setArray(FieldPath.parseFrom(fieldPath), values);
}
 
Example #20
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document setArray(String fieldPath, String[] values) {
  return setArray(FieldPath.parseFrom(fieldPath), values);
}
 
Example #21
Source File: DocumentBase.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document setArray(String fieldPath, short[] values) {
  return setArray(FieldPath.parseFrom(fieldPath), values);
}
 
Example #22
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, BigDecimal value) {
  throw readOnly();
}
 
Example #23
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocument set(String field, Document value) {
  return setCommon(FieldPath.parseFrom(field), JsonValueBuilder.initFrom(value));
}
 
Example #24
Source File: JsonDriver.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document newDocument(String documentJson) throws DecodingException {
  return Json.newDocument(documentJson);
}
 
Example #25
Source File: DocumentBase.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document delete(String fieldPath) {
  return delete(FieldPath.parseFrom(fieldPath));
}
 
Example #26
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, byte[] value, int off, int len) {
  throw readOnly();
}
 
Example #27
Source File: JsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(String fieldPath, int value) {
  return set(FieldPath.parseFrom(fieldPath), value);
}
 
Example #28
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document setArray(String fieldPath, long[] values) {
  throw readOnly();
}
 
Example #29
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document empty() {
  throw readOnly();
}
 
Example #30
Source File: DocumentBase.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(String fieldPath, ODate value) {
  return set(FieldPath.parseFrom(fieldPath), value);
}