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

The following examples show how to use org.ojai.Document#getBinary() . 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: MapRJsonOriginSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private String getDocumentKey(Document document) {

    String ans = "";

    if (jsonDataTypes.get(MAPR_ID) == Value.Type.BINARY) {
      ByteBuffer bb = document.getBinary(MAPR_ID);
      ans = new String(b64.encode(bb.array()));

    } else if (jsonDataTypes.get(MAPR_ID) == Value.Type.STRING) {
      ans = document.getString(MAPR_ID);

    }
    return ans;
  }
 
Example 2
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllTypes() {
  Document rec = Json.newDocument();
  rec.set("map.field1", (byte) 100);
  rec.set("map.field2", (short) 10000);
  rec.set("map.longfield2verylongverylong", 12.345678);
  rec.set("FIELD2", "VERY LONG STRING IS THIS YOU KNOW");

  rec.set("map2.field1", (byte) 100);
  rec.set("map2.field2", (short) 10000);
  rec.set("map2.longfield2verylongverylong", 12.345678);
  rec.set("FIELD3", "VERY LONG STRING IS THIS YOU KNOW");
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("Name", "Anurag");
  map.put("Age", 20);
  rec.set("newmap.map", map);

  rec.set(FIELD_MAP_BOOLEAN, false);
  rec.set(FIELD_MAP_STRING, "string");
  rec.set(FIELD_MAP_BYTE, (byte) 100);
  rec.set(FIELD_MAP_SHORT, (short) 10000);
  rec.set(FIELD_MAP_INT, 50000);
  rec.set(FIELD_MAP_LONG, 12345678999L);
  rec.set(FIELD_MAP_FLOAT, 10.1234f);
  rec.set(FIELD_MAP_DOUBLE, 10.12345678910d);
  // rec.set("map.interval", new Interval(1000, 2000));
  rec.set(FIELD_MAP_DECIMAL, new BigDecimal("1000000000.11111111111111111111"));
  byte[] bytes = new byte[5];
  for (int i = 0; i < bytes.length; ++i) {
    bytes[i] = (byte) i;
  }
  rec.set("map.binary1", bytes);
  rec.set("map.binary2", bytes, 1, 3);
  ByteBuffer bbuf = ByteBuffer.allocate(100);
  for (int i = 0; i < bbuf.capacity(); ++i) {
    bbuf.put((byte) i);
  }
  rec.set("map.binary3", bbuf);

  map = new HashMap<String, Object>();
  map.put("Name", "Anurag");
  map.put("Age", 20);
  List<Integer> scores = new ArrayList<Integer>();
  scores.add(100);
  scores.add(200);
  scores.add(300);
  scores.add(400);
  // map.put("Score", scores);
  rec.set("map.map", map);

  List<Object> values = new ArrayList<Object>();
  values.add("Field1");
  values.add(new Integer(500));
  values.add(new Double(5555.5555));
  rec.set("map.list", values);

  assertEquals(rec.getValue("map").getType(), Type.MAP);
  assertEquals(rec.getBoolean(FIELD_MAP_BOOLEAN), false);
  assertEquals(rec.getString(FIELD_MAP_STRING), "string");
  assertEquals(rec.getByte(FIELD_MAP_BYTE), (byte) 100);
  assertEquals(rec.getShort(FIELD_MAP_SHORT), (short) 10000);
  assertEquals(rec.getInt(FIELD_MAP_INT), 50000);
  assertEquals(rec.getLong(FIELD_MAP_LONG), 12345678999L);
  assertEquals(rec.getFloat(FIELD_MAP_FLOAT), (float) 10.1234, 0.0);
  assertEquals(rec.getDouble(FIELD_MAP_DOUBLE), 10.12345678910d, 0.0);
  // assertEquals(rec.getInterval("map.interval"), new Interval(1000, 2000));
  assertEquals(rec.getDecimal(FIELD_MAP_DECIMAL), new BigDecimal(
      "1000000000.11111111111111111111"));

  java.nio.ByteBuffer readBuf;
  readBuf = rec.getBinary("map.binary1");
  for (int i = 0; i < bytes.length; ++i) {
    assertEquals(readBuf.get(i), bytes[i]);
  }

  readBuf = rec.getBinary("map.binary2");
  for (int i = 0; i < 3; ++i) {
    assertEquals(readBuf.get(), bytes[1 + i]);
  }
  readBuf = rec.getBinary("map.binary3");
  assertEquals(readBuf, bbuf);

  try {
    List<Object> l = rec.getValue("map.list").getList();
    assertEquals(values, l);
  } catch (Exception e) {
    logger.error("Exception from list test " + e.getMessage());
  }
}
 
Example 3
Source File: Documents.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the value at the specified fieldPath as a {@link ByteBuffer} object or
 * the specified {@code defaultValue} if the specified {@code FieldPath} does not
 * exist in the document.
 *
 * @throws TypeException if the value at the fieldPath is not of
 *         <code>BINARY</code> type
 */
public static ByteBuffer getBinary(@NonNullable Document document,
    @NonNullable FieldPath fieldPath, @Nullable ByteBuffer defaultValue) {
  ByteBuffer docValue = document.getBinary(fieldPath);
  return docValue != null ? docValue : defaultValue;
}