org.ojai.Value Java Examples

The following examples show how to use org.ojai.Value. 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: JsonDocumentBuilder.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Override
public JsonDocumentBuilder put(String field, float value) {
  try {
    preparePut();
    if (jsonOptions.isWithTags()) {
      putNewMap(field);
      jsonGenerator.writeNumberField(Value.TAG_FLOAT, value);
      endMap();
    } else {
      jsonGenerator.writeNumberField(field, value);
    }
    return this;
  } catch (IOException ie) {
    throw transformIOException(ie);
  }
}
 
Example #2
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Override
public JsonDocumentBuilder put(String field, BigDecimal value) {
  try {
    preparePut();
    if (jsonOptions.isWithTags()) {
      putNewMap(field);
      jsonGenerator.writeStringField(Value.TAG_DECIMAL, value.toString());
      endMap();
    } else {
      jsonGenerator.writeNumberField(field, value);
    }
    return this;
  } catch (IOException ie) {
    throw transformIOException(ie);
  }
}
 
Example #3
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Override
public JsonDocumentBuilder put(String field, byte[] value) {
  try {
    preparePut();
    if (jsonOptions.isWithTags()) {
      putNewMap(field);
      jsonGenerator.writeBinaryField(Value.TAG_BINARY, value);
      endMap();
    } else {
      jsonGenerator.writeBinaryField(field, value);
    }
    return this;
  } catch (IOException ie) {
    throw transformIOException(ie);
  }
}
 
Example #4
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Override
public JsonDocumentBuilder addDecimal(long decimalValue) {
  try {
    prepareAdd();
    if (jsonOptions.isWithTags()) {
      jsonGenerator.writeStartObject();
      jsonGenerator.writeStringField(Value.TAG_DECIMAL, String.valueOf(decimalValue));
      jsonGenerator.writeEndObject();
    } else {
      jsonGenerator.writeNumber(decimalValue);
    }
    return this;
  } catch (IOException ie) {
    throw transformIOException(ie);
  }
}
 
Example #5
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Override
public JsonDocumentBuilder add(BigDecimal value) {
  try {
    prepareAdd();
    if (jsonOptions.isWithTags()) {
      jsonGenerator.writeStartObject();
      jsonGenerator.writeStringField(Value.TAG_DECIMAL, value.toPlainString());
      jsonGenerator.writeEndObject();
    } else {
      jsonGenerator.writeNumber(value);
    }
    return this;
  } catch (IOException ie) {
    throw transformIOException(ie);
  }
}
 
Example #6
Source File: MapRDBCDCSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void setId(Record record, Value id) throws StageException {
  if(id.getType() == Value.Type.STRING) {
    record.set("/_id", Field.create(id.getString()));
  } else if(id.getType() == Value.Type.BINARY) {
    record.set("/_id", Field.create(id.getBinary().array()));
  } else {
    throw new OnRecordErrorException(record, MaprDBCDCErrors.MAPRDB_04, id.getType().name());
  }
}
 
Example #7
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 #8
Source File: TestValues.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyByteBufferSerialization() throws Exception {
  ByteBuffer hbb = ByteBuffer.allocate(0);
  Value hbbValue = JsonValueBuilder.initFrom(hbb);
  assertEquals("{\"$binary\":\"\"}", hbbValue.toString());

  ByteBuffer dbb = ByteBuffer.allocateDirect(0);
  Value dbbValue = JsonValueBuilder.initFrom(dbb);
  assertEquals("{\"$binary\":\"\"}", dbbValue.toString());
}
 
Example #9
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 #10
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder addDate(int days) {
  return addStringWithTag(Value.TAG_DATE, ODate.fromDaysSinceEpoch(days).toDateStr());
}
 
Example #11
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Value getValue(FieldPath fieldPath) {
  return wrapped.getValue(fieldPath);
}
 
Example #12
Source File: Events.java    From ojai with Apache License 2.0 4 votes vote down vote up
public EventDescriptor setValue(@NonNullable Value value) {
  this.value = value;
  return this;
}
 
Example #13
Source File: Events.java    From ojai with Apache License 2.0 4 votes vote down vote up
public Value getValue() {
  return value;
}
 
Example #14
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, Value value) {
  throw readOnly();
}
 
Example #15
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder add(OTimestamp value) {
  return addStringWithTag(Value.TAG_TIMESTAMP, value.toUTCString());
}
 
Example #16
Source File: TestJsonDocumentEquals.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test
public void testFieldsEquals() {
  Document rec = Json.newDocument();
  rec.set("map.field1", (byte) 100);
  rec.set("map.field2", (short) 10000);
  rec.set("map.string", "eureka");
  rec.set("map.boolean", false);
  rec.set("map.date", ODate.parse("2013-02-12"));
  OTime time = OTime.parse("07:42:46");
  rec.set("map.time", time);
  OTimestamp timeStamp = OTimestamp.parse("2012-10-20T07:42:46");
  rec.set("map.timestamp", timeStamp);
  rec.set("map.int", 12345678);
  byte[] byteArray = "abracadabra".getBytes();
  rec.set("map.bytearray", byteArray);
  rec.setNull("map.null");

  Map<String, Object> m = new HashMap<String, Object>();
  m.put("a", 500);
  m.put("b", "abcd");
  List<Object> newlist = new ArrayList<Object>();
  newlist.add("aaaaa");
  newlist.add(1234567.89);
  m.put("c",newlist);
  rec.set("map2", m);

  List<Object> l = new ArrayList<Object>();
  l.add(12345.678901);
  l.add("abracadabra");

  Map<String, Object> m2 = new HashMap<String, Object>();
  m2.put("a1", 111);
  m2.put("b1", false);
  l.add(m2);

  rec.set("list1", l);

  Document rec2 = rec;
  assertEquals(true, rec2.equals(rec));

  Value stringValue = rec.getValue("map.string");
  assertEquals(true, stringValue.equals("eureka"));

  byte b = rec.getByte("map.field1");
  assertEquals(true, rec.getValue("map.field1").equals(b));

  Byte bite = new Byte(b);
  assertEquals(true, bite.equals(rec.getByte("map.field1")));

  short s = rec.getShort("map.field2");
  assertEquals(true, rec.getValue("map.field2").equals(s));

  assertEquals(true, rec.getValue("map.field2").equals(JsonValueBuilder.initFrom(s)));

  assertEquals(true, rec.getValue("map.boolean").equals(false));

  assertEquals(true, rec.getValue("map.date").equals(ODate.parse("2013-02-12")));

  assertEquals(true, rec.getValue("map2").equals(m));

  int x = rec.getInt("map.int");
  assertEquals(true, rec.getValue("map.int").equals(x));

  assertEquals(true, rec.getList("list1").equals(l));

  assertEquals(true, rec.getValue("map.time").equals(time));

  assertEquals(true, rec.getValue("map.timestamp").equals(timeStamp));

  assertEquals(true, rec.getValue("map.bytearray").equals(ByteBuffer.wrap(byteArray)));

  Value myValue;
  myValue = rec.getValue("map.date");
  assertEquals(true, rec.getValue("map.date").equals(myValue));

  myValue = rec.getValue("map.time");
  assertEquals(true, rec.getValue("map.time").equals(myValue));

  myValue = rec.getValue("map.timestamp");
  assertEquals(true, rec.getValue("map.timestamp").equals(myValue));

  myValue = rec.getValue("list1");
  assertEquals(true,rec.getValue("list1").equals(myValue));

  myValue = rec.getValue("map2");
  assertEquals(true,rec.getValue("map2").equals(myValue));

  Value nval = rec.getValue("map.null");
  assertEquals(true, rec.getValue("map.null").equals(nval));

  Document r1 = Json.newDocument();
  Document r2 = Json.newDocument();
  Document r3 = Json.newDocument();
  r1.setNull("a");
  r2.setNull("a");
  r3.setNull("b");
  assertEquals(true, r1.equals(r2));
  assertEquals(false, r1.equals(r3));

}
 
Example #17
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder add(ODate value) {
  return addStringWithTag(Value.TAG_DATE, value.toDateStr());
}
 
Example #18
Source File: DocumentBase.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(String fieldPath, Value value) {
  return set(FieldPath.parseFrom(fieldPath), value);
}
 
Example #19
Source File: TestMapRDBCDCSource.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private ConsumerRecords<byte[], ChangeDataRecord> generateConsumerRecords(int recordCount, int nodeCount, String topic, int partition, ChangeDataRecordType type) {
  List<ConsumerRecord<byte[], ChangeDataRecord>> consumerRecordsList = new ArrayList<>();
  for(int i=0; i<recordCount; i++) {
    long now = Instant.now().toEpochMilli();

    Value idVal = Mockito.mock(Value.class);
    if(i%2 == 0) {
      Mockito.when(idVal.getString()).thenReturn(String.valueOf(i));
      Mockito.when(idVal.getType()).thenReturn(Value.Type.STRING);
    } else {
      ByteBuffer bbuf = ByteBuffer.allocate(8);
      Mockito.when(idVal.getBinary()).thenReturn(bbuf.putInt(i));
      Mockito.when(idVal.getType()).thenReturn(Value.Type.BINARY);
    }

    ChangeDataRecord cdr = Mockito.mock(ChangeDataRecord.class);
    Mockito.when(cdr.getId()).thenReturn(idVal);
    Mockito.when(cdr.getOpTimestamp()).thenReturn(now);
    Mockito.when(cdr.getServerTimestamp()).thenReturn(now);
    Mockito.when(cdr.getType()).thenReturn(type);

    if(type != ChangeDataRecordType.RECORD_DELETE) {
      List<KeyValue<FieldPath, ChangeNode>> nodeList = new ArrayList<>();
      ChangeNode node = Mockito.mock(ChangeNode.class);

      for (int j = 0; j < nodeCount; j++) {
        String fieldPath = "";
        if(type == ChangeDataRecordType.RECORD_UPDATE) {
          fieldPath = String.valueOf(i);
          Mockito.when(node.getType()).thenReturn(valueRing.peek().getType());
          Mockito.when(node.getValue()).thenReturn(valueRing.next());
        } else {
          Mockito.when(node.getType()).thenReturn(Value.Type.MAP);
          Mockito.when(node.getMap()).thenReturn(Collections.singletonMap("datakey" + i, objectRing.next()));
        }

        Mockito.when(node.getOpTimestamp()).thenReturn(now);
        Mockito.when(node.getServerTimestamp()).thenReturn(now);

        nodeList.add(new ChangeDataKeyValue(new FieldPath(new FieldSegment.NameSegment(fieldPath,
            null,
            false
        )), node));
      }

      Mockito.when(cdr.iterator()).thenReturn(nodeList.iterator());
    }

    consumerRecordsList.add(new ConsumerRecord<>(topic, partition, 0, ("key" + i).getBytes(), cdr));
  }

  Map<TopicPartition, List<ConsumerRecord<byte[], ChangeDataRecord>>> recordsMap = new HashMap<>();
  recordsMap.put(new TopicPartition(topic, partition), consumerRecordsList);
  return new ConsumerRecords<>(recordsMap);
}
 
Example #20
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder add(long value) {
  return addLong(Value.TAG_LONG, value);
}
 
Example #21
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder add(OTime value) {
  return addStringWithTag(Value.TAG_TIME, value.toTimeStr());
}
 
Example #22
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder putTimestamp(String field, long timeMillis) {
  return putStringWithTag(field, Value.TAG_TIMESTAMP, new OTimestamp(timeMillis).toUTCString());
}
 
Example #23
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final Value _id, final QueryCondition condition) throws StoreException {
  return store.findById(_id, condition);
}
 
Example #24
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder put(String field, Value value) {
  Value.Type t = value.getType();
  switch (t) {
  case NULL:
    putNull(field);
    break;
  case BOOLEAN:
    put(field, value.getBoolean());
    break;
  case STRING:
    put(field, value.getString());
    break;
  case BYTE:
    put(field, value.getByte());
    break;
  case SHORT:
    put(field, value.getShort());
    break;
  case INT:
    put(field, value.getInt());
    break;
  case LONG:
    put(field, value.getLong());
    break;
  case FLOAT:
    put(field, value.getFloat());
    break;
  case DOUBLE:
    put(field, value.getDouble());
    break;
  case DECIMAL:
    put(field, value.getDecimal());
    break;
  case DATE:
    put(field, value.getDate());
    break;
  case TIME:
    put(field, value.getTime());
    break;
  case TIMESTAMP:
    put(field, value.getTimestamp());
    break;
  case INTERVAL:
    put(field, value.getInterval());
    break;
  case BINARY:
    put(field, value.getBinary());
    break;
  case MAP:
    put(field, (Document)value);
    break;
  case ARRAY:
    putArray(field, value.getList());
    break;
  default:
    break;
  }
  return this;
}
 
Example #25
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder put(String field, OTime value) {
  return putStringWithTag(field, Value.TAG_TIME, value.toTimeStr());
}
 
Example #26
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder putDate(String field, int days) {
  return putStringWithTag(field, Value.TAG_DATE, ODate.fromDaysSinceEpoch(days).toDateStr());
}
 
Example #27
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public void increment(final Value _id, final String field, final float inc) throws StoreException {
  store.increment(_id, field, inc);
}
 
Example #28
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final Value _id, final QueryCondition condition, final FieldPath... fieldPaths)
    throws StoreException {
  return store.findById(_id, condition, fieldPaths);
}
 
Example #29
Source File: JsonDocumentBuilder.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDocumentBuilder put(String field, long value) {
  return putLongWithTag(field, Value.TAG_LONG, value);
}
 
Example #30
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndMutate(final Value _id, final QueryCondition condition, final DocumentMutation mutation)
    throws StoreException {
  return store.checkAndUpdate(_id, condition, mutation);
}