/** * 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); }
@Override public byte[] getSerializedEvent() { try { toBytes(); return serializedBytes; } catch (IOException e) { return null; } }
public static Event extractEvent(final String type, final DateTime eventDateTime, final String payload) throws TException { final List<ThriftField> list = new ArrayList<ThriftField>(); list.add(ThriftField.createThriftField(payload, (short) 1)); final ThriftEnvelope envelope = new ThriftEnvelope(type, list); return new ThriftEnvelopeEvent(eventDateTime, envelope); } }
@Override public String getOutputDir(final String prefix) { final GranularityPathMapper pathMapper = new GranularityPathMapper(String.format("%s/%s", prefix, thriftEnvelope.getTypeName()), granularity); return pathMapper.getPathForDateTime(getEventDateTime()); }
public static <T extends Serializable> T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent) final ThriftEnvelope envelope = (ThriftEnvelope) envelopeEvent.getData();
public ThriftEnvelopeEvent getNextEvent() throws IOException { try { if (hasNextEvent() && (byte) in.read() == '\n') { return new ThriftEnvelopeEvent(in, deserializer); } else { throw new IOException("Couldn't find any more events in the stream"); } } catch (IOException e) { hasFailed = true; deserializer.close(); throw e; } } }
/** * Given a generic thrift object (class generated by the thrift compiler), create a ThriftEnvelopeEvent. * * @param eventName Thrift schema name * @param eventDateTime the event timestamp * @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 DateTime eventDateTime, final T thriftObject) { final List<ThriftField> thriftFieldList = new ArrayList<ThriftField>(); final Field[] fields = thriftObject.getClass().getFields(); for (int i = 0; i < fields.length; i++) { try { // Thrift fields start at 1, not 0 final ThriftField field = ThriftField.createThriftField(fields[i].getType(), fields[i].get(thriftObject), (short) (i + 1)); // null for the Thrift metaData map and potential other non-supported attributes added by the caller in the thriftObject if (field != null) { thriftFieldList.add(field); } } catch (IllegalAccessException ignored) { } } final ThriftEnvelope envelope = new ThriftEnvelope(eventName, thriftFieldList); return new ThriftEnvelopeEvent(eventDateTime, envelope); }
@Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { final int numBytes = in.readInt(); final byte[] fullPayload = new byte[numBytes]; in.readFully(fullPayload); final ByteArrayInputStream inputBuffer = new ByteArrayInputStream(fullPayload, 0, fullPayload.length); deserializer.open(inputBuffer); deserializeFromStream(inputBuffer); deserializer.close(); }
@Override public void writeExternal(final ObjectOutput out) throws IOException { toBytes(); out.writeInt(serializedBytes.length); out.write(serializedBytes); }
/** * Given a serialized Thrift, generate a ThrifTEnvelopeEvent * * @param type Thrift schema name * @param eventDateTime the event timestamp * @param payload serialized Thrift * @return ThriftEnvelopeEvent representing the Thrift * @throws TException if the payload is not a valid Thrift */ public static ThriftEnvelopeEvent extractEvent(final String type, final DateTime eventDateTime, final byte[] payload) throws TException { final List<ThriftField> list = new ThriftFieldListDeserializer().readPayload(payload); final ThriftEnvelope envelope = new ThriftEnvelope(type, list); return new ThriftEnvelopeEvent(eventDateTime, envelope); } }