public void store(String jsonRaw) throws IOException { if (jsonEventDeserializer == null) { throw new RakamException("Event store is not supported.", BAD_REQUEST); } JsonParser jp = JsonHelper.getMapper().getFactory().createParser(jsonRaw); JsonToken t = jp.nextToken(); if (t != JsonToken.START_ARRAY) { throw new RakamException("The script didn't return an array", BAD_REQUEST); } t = jp.nextToken(); List<Event> list = new ArrayList<>(); for (; t == START_OBJECT; t = jp.nextToken()) { list.add(jsonEventDeserializer.deserializeWithProject(jp, project, Event.EventContext.empty(), true)); } EventCollectionHttpService.mapEvent(eventMapperSet, eventMapper -> eventMapper.mapAsync(new EventList(Event.EventContext.empty(), list), EMPTY_PARAMS, localhost, HttpHeaders.EMPTY_HEADERS)); int[] ints = eventStore.storeBatch(list); if (ints.length > 0) { logger.error(format("Failed to save events: %s", Arrays.stream(ints).boxed().map(i -> i + "").collect(Collectors.joining(", ")))); } } }
public EventList deserialize(String project, String collection, SliceInput slice) throws IOException { String json = slice.readSlice(slice.readInt()).toStringUtf8(); Schema schema = new Schema.Parser().parse(json); int records = slice.readInt(); BinaryDecoder binaryDecoder = DecoderFactory.get().directBinaryDecoder(slice, null); List<SchemaField> fields = metastore.getCollection(project, collection); Schema avroSchema = AvroUtil.convertAvroSchema(fields); GenericDatumReader<GenericRecord> reader = new GenericDatumReader(schema, avroSchema); List<Event> list = new ArrayList<>(records); for (int i = 0; i < records; i++) { GenericRecord record = reader.read(null, binaryDecoder); list.add(new Event(project, collection, null, fields, record)); } return new EventList(Event.EventContext.empty(), project, list); } }
public Event createEvent(String collection, Map<String, Object> properties) { List<SchemaField> cache = fieldCache.get(collection); List<SchemaField> fields; List<SchemaField> generatedSchema = generateSchema(properties); if (cache == null || !generatedSchema.stream().allMatch(f -> cache.contains(f))) { fields = metastore.getOrCreateCollectionFields(project, collection, ImmutableSet.copyOf(generatedSchema)); fieldCache.put(collection, fields); } else { fields = cache; } try { GenericData.Record record = new GenericData.Record(AvroUtil.convertAvroSchema(fields)); properties.forEach((key, value) -> record.put(key, cast(value, record.getSchema().getField(key).schema().getTypes().get(1).getType()))); return new Event(project, collection, Event.EventContext.empty(), fields, record); } catch (Exception e) { throw Throwables.propagate(e); } }