Java Code Examples for org.ojai.Document#asReader()

The following examples show how to use org.ojai.Document#asReader() . 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: TestJsonDocument.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsReaderPartial() {
  Document document = Json.newDocument();
  document.set(FIELD_MAP_BYTE, (byte)127);
  document.set("map.num", 12345);
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("first","John");
  map.put("last", "Doe");
  map.put("id", (long)123456789);
  document.set("map.name", map);
  List<Object> mylist = new ArrayList<Object>();
  mylist.add(true);
  mylist.add("string");
  mylist.add(123.456);
  document.set(FIELD_MAP_ARRAY, mylist);
  DocumentReader r = document.asReader("map.name");
  EventType event;
  while ((event = r.next()) != null) {
    if (event == EventType.LONG) {
      assertEquals("id", r.getFieldName());
      assertEquals(123456789, r.getLong());
    }
  }
}
 
Example 2
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 3
Source File: JSONFileOutputRecordWriter.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public void write(LongWritable arg0, Document document) throws IOException,
InterruptedException {

  writer = (JsonDocumentBuilder) Json.newDocumentBuilder();
  DocumentReader reader = document.asReader();
  Json.writeReaderToBuilder(reader, writer);
  byte[] bytes = writer.getOutputStream();
  out.write(bytes);
}
 
Example 4
Source File: TestJsonDocumentStream.java    From ojai with Apache License 2.0 5 votes vote down vote up
private void testDocumentElements(Document rec) {
  DocumentReader r = rec.asReader();
  EventType et;
  String id = null;
  boolean isArray = false;
  while ((et = r.next()) != null) {
    if (et == EventType.START_ARRAY) {
      isArray = true;
    } else if (et == EventType.END_ARRAY) {
      isArray = false;
    } else if (et == EventType.STRING && r.inMap()) {

      if (r.getFieldName().equals("business_id")) {
        id = r.getString();
      }
      if ((r.getFieldName().equals("street")) && (id.equals("id3"))) {
        assertEquals("Lint St", r.getString());
      }

      if ((isArray) && (r.getFieldName().equals("first"))) {
        assertEquals("Jerry", r.getString());
      }

    } else {
      if ((et == EventType.LONG) && (id.equals("id2"))) {
        assertEquals(45, r.getLong());
      }
      if ((et == EventType.BOOLEAN) && id.equals("id1")) {
        assertEquals(true, r.getBoolean());
      }

    }
  }
}
 
Example 5
Source File: TestDocumentReaderWithProjection.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testProjectEntireArrayOnSubdocument() {
  Document doc = Json.newDocument()
                     .set("a.b", "ab")
                     .set("a.c.d", "acd")
                     .set("a.x", "ax")
                     .setArray("a.d", new Object[] {"ad1","ad2","ad3"});

  DocumentReader docReader = doc.asReader();
  String[] paths = {"a[].b","a.c[]","a.d[2]","a[].x[]"};
  FieldProjector fieldProjector = new FieldProjector(paths);

  DocumentReaderWithProjection r = new DocumentReaderWithProjection(docReader, fieldProjector);

  assertMapEvent(r, EventType.START_MAP, null);
    assertMapEvent(r, EventType.START_MAP, "a");
      assertMapEvent(r, EventType.STRING, "b");

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

      assertMapEvent(r, EventType.STRING, "x");

      assertMapEvent(r, EventType.START_ARRAY, "d");
        assertArrayEvent(r, EventType.STRING, 2);
      assertMapEvent(r, EventType.END_ARRAY, "d");

    assertMapEvent(r, EventType.END_MAP, "a");
  assertMapEvent(r, EventType.END_MAP, null);
  assertNull(r.next());
}
 
Example 6
Source File: TestDocumentReaderWithProjection.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testProjecEntireArrayOfScalar() {
  Document doc = Json.newDocument()
                   .set("a.b", "ab1")
                   .setArray("a.c.e", new Object[] { "ace1", "ace2", "ace3"})
                   .setArray("a.c.d",
                              new Object[] {
                             new Object[] { "acd1", "acd2", "acd3" },
                             new Object[] { "acd4", "acd5", "acd6" }
                           }
                   )
                   .set("a.d", "ad");

  DocumentReader docReader = doc.asReader();
  String[] paths = {"a.b[]","a.c.d[0][]","a.d[][]"};
  FieldProjector fieldProjector = new FieldProjector(paths);

  DocumentReaderWithProjection r = new DocumentReaderWithProjection(docReader, fieldProjector);

  assertMapEvent(r, EventType.START_MAP, null);
  assertMapEvent(r, EventType.START_MAP, "a");

    assertMapEvent(r, EventType.STRING, "b");

    assertMapEvent(r, EventType.START_MAP, "c");
      assertMapEvent(r, EventType.START_ARRAY, "d");
        assertArrayEvent(r, EventType.START_ARRAY, 0);
          assertArrayEvent(r, EventType.STRING, 0);
          assertArrayEvent(r, EventType.STRING, 1);
          assertArrayEvent(r, EventType.STRING, 2);
        assertArrayEvent(r, EventType.END_ARRAY, 0);
      assertMapEvent(r, EventType.END_ARRAY, "d");
    assertMapEvent(r, EventType.END_MAP, "c");

  assertMapEvent(r, EventType.END_MAP, "a");
  assertMapEvent(r, EventType.END_MAP, null);
}
 
Example 7
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test
public void testAsReaderFull() {
  Document document = Json.newDocument();
  document.set(FIELD_MAP_BYTE, (byte)127);
  document.set(FIELD_MAP_LONG, 123456789L);
  Map<String, Object> map = new LinkedHashMap<String, Object>();
  map.put("first","John");
  map.put("last", "Doe");
  document.set("map.name", map);
  List<Object> mylist = new ArrayList<Object>();
  mylist.add(true);
  mylist.add("string");
  mylist.add(123.456);
  document.set(FIELD_MAP_ARRAY, mylist);

  DocumentReader r = document.asReader();
  EventType et = null;
  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.START_MAP, et);

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.START_MAP, et);
  assertEquals("map", r.getFieldName());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.BYTE, et);
  assertEquals("byte", r.getFieldName());
  assertEquals(127, r.getByte());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.LONG, et);
  assertEquals("long", r.getFieldName());
  assertEquals(123456789, r.getLong());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.START_MAP, et);
  assertEquals("name", r.getFieldName());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.STRING, et);
  assertEquals("first", r.getFieldName());
  assertEquals("John", r.getString());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.STRING, et);
  assertEquals("last", r.getFieldName());
  assertEquals("Doe", r.getString());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.END_MAP, et);
  assertEquals("name", r.getFieldName());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.START_ARRAY, et);
  assertEquals("array", r.getFieldName());

  assertTrue((et = r.next()) != null);
  assertTrue(!r.inMap());
  assertEquals(EventType.BOOLEAN, et);
  assertEquals(0, r.getArrayIndex());
  assertEquals(true, r.getBoolean());

  assertTrue((et = r.next()) != null);
  assertTrue(!r.inMap());
  assertEquals(EventType.STRING, et);
  assertEquals(1, r.getArrayIndex());
  assertEquals("string", r.getString());

  assertTrue((et = r.next()) != null);
  assertTrue(!r.inMap());
  assertEquals(EventType.DOUBLE, et);
  assertEquals(2, r.getArrayIndex());
  assertEquals(123.456, r.getDouble(), 0.000001);

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.END_ARRAY, et);
  assertEquals("array", r.getFieldName());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.END_MAP, et);
  assertEquals("map", r.getFieldName());

  assertTrue((et = r.next()) != null);
  assertTrue(r.inMap());
  assertEquals(EventType.END_MAP, et);
  assertNull(r.getFieldName());
}
 
Example 8
Source File: TestDocumentReaderWithProjection.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test
public void testProjectEntireArrayOnArray() {
  Document subDoc1 = Json.newDocument()
                         .set("d", "cd1");
  Document subDoc2 = Json.newDocument()
                         .set("d1", "cd2");
  Document subDoc3 = Json.newDocument()
                         .set("d", "cd3");

  Document doc = Json.newDocument()
                     .setArray("a.b", new Object[] { "ab1", "ab2", "ab3"})
                     .setArray("a.c", new Object[] { subDoc1, subDoc2, subDoc3 })
                     .setArray("a.d", new Object[] {
                                      new Object [] { (long) 0xad1, (long) 0xad2 },
                                      new Object [] { (long) 0xad3, (long) 0xad4 }
                                      }
                      )
                     .set("a.e", "ae");

  DocumentReader docReader = doc.asReader();
  String[] paths = {"a[].b[]","a.c[].d","a.d[]"};
  FieldProjector fieldProjector = new FieldProjector(paths);

  DocumentReaderWithProjection r = new DocumentReaderWithProjection(docReader, fieldProjector);

  assertMapEvent(r, EventType.START_MAP, null);
    assertMapEvent(r, EventType.START_MAP, "a");

      assertMapEvent(r, EventType.START_ARRAY, "b");
        assertArrayEvent(r, EventType.STRING, 0);
        assertArrayEvent(r, EventType.STRING, 1);
        assertArrayEvent(r, EventType.STRING, 2);
      assertMapEvent(r, EventType.END_ARRAY, "b");

      assertMapEvent(r, EventType.START_ARRAY, "c");
        assertArrayEvent(r, EventType.START_MAP, 0);
          assertMapEvent(r, EventType.STRING, "d");
        assertArrayEvent(r, EventType.END_MAP, 0);

        assertArrayEvent(r, EventType.START_MAP, 1);
        assertArrayEvent(r, EventType.END_MAP, 1);

        assertArrayEvent(r, EventType.START_MAP, 2);
          assertMapEvent(r, EventType.STRING, "d");
        assertArrayEvent(r, EventType.END_MAP, 2);
      assertMapEvent(r, EventType.END_ARRAY, "c");

      assertMapEvent(r, EventType.START_ARRAY, "d");
        assertArrayEvent(r, EventType.START_ARRAY, 0);
          assertArrayEvent(r, EventType.LONG, 0);
          assertArrayEvent(r, EventType.LONG, 1);
        assertArrayEvent(r, EventType.END_ARRAY, 0);

        assertArrayEvent(r, EventType.START_ARRAY, 1);
          assertArrayEvent(r, EventType.LONG, 0);
          assertArrayEvent(r, EventType.LONG, 1);
        assertArrayEvent(r, EventType.END_ARRAY, 1);
      assertMapEvent(r, EventType.END_ARRAY, "d");

    assertMapEvent(r, EventType.END_MAP, "a");
  assertMapEvent(r, EventType.END_MAP, null);
}
 
Example 9
Source File: TestDocumentReaderWithProjection.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test
public void testProjectionEntireArrayMixed() {
  Document doc = Json.newDocument()
                     .set("a.scalar1", "ab1")
                     .setArray("a.array", new Object[] { "ac1", "ac2", "ac3", "ac4"})
                     .setArray("a.arrayOfArray", new Object[] {
                             new Object[] { "aoa1", "aoa2" , "aoa3" },
                             new Object[] { "aoa4", "aoa5", "aoa6" }
                      })
                     .setArray("a.arrayOfMaps", new Object[] {
                             Json.newDocument().set("map1", "aom1")
                                               .set("map2", "aom2")
                                               .set("map3", "aom3"),
                             Json.newDocument().set("map4", "aom4")
                                               .set("map5", "aom5")
                                               .set("map6", "aom6")
                      })
                     .set("a.scalar2", (long) 0x12345)
                     .set("b.c", "bc1");

  DocumentReader docReader = doc.asReader();
  String[] paths = {"a.scalar1[]",
                    "a[].array[2]",
                    "a.arrayOfArray[][1]",
                    "a.arrayOfMaps[].map2",
                    "a[].scalar2",
                    "b[]"};

  FieldProjector fieldProjector = new FieldProjector(paths);

  DocumentReaderWithProjection r = new DocumentReaderWithProjection(docReader, fieldProjector);

  assertMapEvent(r, EventType.START_MAP, null);
    assertMapEvent(r, EventType.START_MAP, "a");

      assertMapEvent(r, EventType.STRING, "scalar1");

      assertMapEvent(r, EventType.START_ARRAY, "array");
        assertArrayEvent(r, EventType.STRING, 2);
      assertMapEvent(r, EventType.END_ARRAY, "array");

      assertMapEvent(r, EventType.START_ARRAY, "arrayOfArray");
        assertArrayEvent(r, EventType.START_ARRAY, 0);
          assertArrayEvent(r, EventType.STRING, 1);
        assertArrayEvent(r, EventType.END_ARRAY, 0);

        assertArrayEvent(r, EventType.START_ARRAY, 1);
          assertArrayEvent(r, EventType.STRING, 1);
        assertArrayEvent(r, EventType.END_ARRAY, 1);
      assertMapEvent(r, EventType.END_ARRAY, "arrayOfArray");

      assertMapEvent(r, EventType.START_ARRAY, "arrayOfMaps");
        assertArrayEvent(r, EventType.START_MAP, 0);
          assertMapEvent(r, EventType.STRING, "map2");
        assertArrayEvent(r, EventType.END_MAP, 0);

        assertArrayEvent(r, EventType.START_MAP, 1);
        assertArrayEvent(r, EventType.END_MAP, 1);
      assertMapEvent(r, EventType.END_ARRAY, "arrayOfMaps");

      assertMapEvent(r, EventType.LONG, "scalar2");
    assertMapEvent(r, EventType.END_MAP, "a");

    assertMapEvent(r, EventType.START_MAP, "b");
      assertMapEvent(r, EventType.STRING, "c");
    assertMapEvent(r, EventType.END_MAP, "b");
  assertMapEvent(r, EventType.END_MAP, null);
}