private void setEventPropertiesFromNode(final JsonNode node) { if (eventDateTime == null) { eventDateTime = getEventDateTimeFromJson(node); } if (granularity == null) { granularity = getGranularityFromJson(node); } }
@Override public byte[] getSerializedEvent() { try { toBytes(); return serializedBytes; } catch (IOException e) { return null; } }
protected SmileEnvelopeEvent(final String eventName, final Granularity granularity, final DateTime eventDateTime, final JsonNode node) { this.eventName = eventName; this.granularity = granularity; this.eventDateTime = eventDateTime; this.root = node; setEventPropertiesFromNode(node); }
@Override public String getOutputDir(final String prefix) { final GranularityPathMapper pathMapper = new GranularityPathMapper(String.format("%s/%s", prefix, eventName), granularity); return pathMapper.getPathForDateTime(getEventDateTime()); }
@Override public boolean equals(final Object obj) { if (!(obj instanceof SmileEnvelopeEvent)) { return false; } final Event other = (Event) obj; return other.getName().equals(eventName) && other.getEventDateTime().equals(eventDateTime) && other.getGranularity().equals(granularity) && other.getData().equals(root); } }
@Override public String getOutputDir(final String prefix) { final GranularityPathMapper pathMapper = new GranularityPathMapper(String.format("%s/%s", prefix, thriftEnvelope.getTypeName()), granularity); return pathMapper.getPathForDateTime(getEventDateTime()); }
@SuppressWarnings("deprecation") public static Granularity getGranularityFromJson(final JsonNode node) { final JsonNode granularityNode = node.get(SMILE_EVENT_GRANULARITY_TOKEN_NAME); if (granularityNode == null) { return Granularity.HOURLY; } try { return Granularity.valueOf(granularityNode.asText()); } catch (IllegalArgumentException e) { // hmmmh. Returning null seems dangerous; but that's what we had... return null; } }
public Collection<String> getPathsForInterval(final ReadableInterval interval) { final Collection<String> paths = new ArrayList<String>(); granularity.stepThroughInterval(interval, new Granularity.Callback<RuntimeException>() { public void step(final ReadableInterval stepInterval) throws RuntimeException { paths.add(getPathForDateTime(stepInterval.getStart())); } }); return paths; }
/** * Given a serialized Thrift, generate a ThrifTEnvelopeEvent * * @param type Thrift schema name * @param payload serialized Thrift * @return ThriftEnvelopeEvent representing the Thrift (the event timestamp defaults to now()) * @throws TException if the payload is not a valid Thrift */ public static ThriftEnvelopeEvent extractEvent(final String type, final byte[] payload) throws TException { return extractEvent(type, new DateTime(), payload); }
public SmileEnvelopeEvent(final String eventName, final byte[] inputBytes, final DateTime eventDateTime, final Granularity granularity) throws IOException { this.eventName = eventName; this.serializedEvent = inputBytes; this.eventDateTime = eventDateTime; this.granularity = granularity; this.root = parseAsTree(inputBytes); }
/** * Deserialize one event from the specified stream. * This expects the stream to be open and won't close it. The specified deserializer should take care of this. * * @param in inputstream to read * @param deserializer deserializer responsible to open/close the stream * @throws IOException generic I/O Exception */ public ThriftEnvelopeEvent(final InputStream in, final ThriftEnvelopeDeserializer deserializer) throws IOException { this.deserializer = deserializer; deserializeFromStream(in); }
public String getPathForDateTime(final ReadableDateTime dateTime) { return String.format("%s/%s", prefix, granularity.getRelativePathFor(dateTime)); } }
public List<ReadableDateTime> reverseSteps(final ReadableInterval interval) { final List<ReadableDateTime> result = forwardSteps(interval); Collections.reverse(result); return result; }
/** * Given a file written by the serialization-writer library, extract all events. * This assumes the file was written using ObjectOutputStream. * * @param file file to deserialize * @return events contained in the file * @throws java.io.IOException generic IOException * @throws ClassNotFoundException if the underlying Event class is not in the classpath */ public static List<Event> fromFile(final File file) throws IOException, ClassNotFoundException { return fromInputStream(new FileInputStream(file)); }
public static Event extractEvent(final String type, final String payload) throws TException { return extractEvent(type, new DateTime(), payload); }
/** * Given a stream written by the serialization-writer library, extract all events. * This assumes the file was written using ObjectOutputStream. * * @param stream stream to deserialize * @return events contained in the file * @throws java.io.IOException generic IOException * @throws ClassNotFoundException if the underlying Event class is not in the classpath */ public static List<Event> fromInputStream(final InputStream stream) throws IOException, ClassNotFoundException { return fromObjectInputStream(new ObjectInputStream(stream)); }
@Override public byte[] getSerializedEvent() { if (serializedEvent == null) { // can we not avoid serializing it if we already have bytes? try { serializedEvent = getObjectMapper().writeValueAsBytes(root); } catch (IOException e) { // would rather this was thrown, but signature won't allow it: return null; } } return serializedEvent; }
@Override public void writeExternal(final ObjectOutput out) throws IOException { toBytes(); out.writeInt(serializedBytes.length); out.write(serializedBytes); }
public List<ReadableDateTime> forwardSteps(final ReadableInterval interval) { final List<ReadableDateTime> result = new ArrayList<ReadableDateTime>(); stepThroughInterval(interval, new Callback<RuntimeException>() { @Override public void step(final ReadableInterval stepInterval) { result.add(stepInterval.getStart()); } }); return result; }
/** * Given a generic thrift object (class generated by the thrift compiler), create a ThriftEnvelopeEvent. * The event timestamp defaults to now(). * * @param eventName Thrift schema name * @param thriftObject Thrift instance * @param <T> any Thrift class generated by the thrift compiler * @return ThriftEnvelopeEvent which wraps all thrift fields as ThriftFields */ public static <T extends Serializable> ThriftEnvelopeEvent extractEvent(final String eventName, final T thriftObject) { return extractEvent(eventName, new DateTime(), thriftObject); }