Java Code Examples for org.apache.samza.system.OutgoingMessageEnvelope#getMessage()

The following examples show how to use org.apache.samza.system.OutgoingMessageEnvelope#getMessage() . 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: EventHubSystemProducer.java    From samza with Apache License 2.0 6 votes vote down vote up
protected EventData createEventData(String streamId, OutgoingMessageEnvelope envelope) {
  Optional<Interceptor> interceptor = Optional.ofNullable(interceptors.getOrDefault(streamId, null));
  byte[] eventValue = (byte[]) envelope.getMessage();
  if (interceptor.isPresent()) {
    eventValue = interceptor.get().intercept(eventValue);
  }

  EventData eventData = new EventDataImpl(eventValue);

  eventData.getProperties().put(PRODUCE_TIMESTAMP, Long.toString(System.currentTimeMillis()));

  if (config.getSendKeyInEventProperties(systemName)) {
    String keyValue = "";
    if (envelope.getKey() != null) {
      keyValue = (envelope.getKey() instanceof byte[]) ? new String((byte[]) envelope.getKey())
          : envelope.getKey().toString();
    }
    eventData.getProperties().put(KEY, keyValue);
  }
  return eventData;
}
 
Example 2
Source File: InMemorySystemProducer.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a specified message envelope from a specified Samza source.

 * @param source String representing the source of the message.
 * @param envelope Aggregate object representing the serialized message to send from the source.
 */
@Override
public void send(String source, OutgoingMessageEnvelope envelope) {
  Object key = envelope.getKey();
  Object message = envelope.getMessage();

  Object partitionKey;
  // We use the partition key from message if available, if not fallback to message key or use message as partition
  // key as the final resort.
  if (envelope.getPartitionKey() != null) {
    partitionKey = envelope.getPartitionKey();
  } else if (key != null) {
    partitionKey = key;
  } else {
    partitionKey = message;
  }

  Preconditions.checkNotNull(partitionKey, "Failed to compute partition key for the message: " + envelope);

  int partition =
      Math.abs(hashCode(partitionKey)) % memoryManager.getPartitionCountForSystemStream(envelope.getSystemStream());

  SystemStreamPartition ssp = new SystemStreamPartition(envelope.getSystemStream(), new Partition(partition));
  memoryManager.put(ssp, key, message);
}
 
Example 3
Source File: ConsoleLoggingSystemFactory.java    From samza with Apache License 2.0 5 votes vote down vote up
private String getFormattedValue(OutgoingMessageEnvelope envelope) {
  String value = envelope.getMessage() != null ? new String((byte[]) envelope.getMessage()) : null;
  String formattedValue;

  try {
    Object json = mapper.readValue(value, Object.class);
    formattedValue = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
  } catch (IOException e) {
    formattedValue = value;
    LOG.error("Error while formatting json", e);
  }

  return formattedValue;
}
 
Example 4
Source File: AzureBlobAvroWriter.java    From samza with Apache License 2.0 5 votes vote down vote up
/**
 * This method expects the {@link org.apache.samza.system.OutgoingMessageEnvelope}
 * to contain a message which is a {@link org.apache.avro.generic.IndexedRecord} or an encoded record aka byte[].
 * If the record is already encoded, it will directly write the byte[] to the output stream without checking if it conforms to schema.
 * Else, it encodes the record and writes to output stream.
 * However, the first envelope should always be a record and not a byte[].
 * If the blocksize threshold crosses, it will upload the output stream contents as a block.
 * If the number of records in current blob or size of current blob exceed limits then a new blob is created.
 * Multi-threading and thread-safety:
 *  The underlying {@link org.apache.avro.file.DataFileWriter} is not thread-safe.
 *  For this reason, it is essential to wrap accesses to this object in a synchronized block.
 *  Method write(OutgoingMessageEnvelope) allows multiple threads to encode records as that operation is stateless but
 *  restricts access to the shared objects through the synchronized block.
 *  Concurrent access to shared objects is controlled through a common lock and synchronized block and hence ensures
 *  thread safety.
 * @param ome - OutgoingMessageEnvelope that contains the IndexedRecord (GenericRecord or SpecificRecord) or an encoded record as byte[]
 * @throws IOException when
 *       - OutgoingMessageEnvelope's message is not an IndexedRecord or
 *       - underlying dataFileWriter.append fails
 * @throws IllegalStateException when the first OutgoingMessageEnvelope's message is not a record.
 */
@Override
public void write(OutgoingMessageEnvelope ome) throws IOException {
  Optional<IndexedRecord> optionalIndexedRecord;
  byte[] encodedRecord;
  if (ome.getMessage() instanceof IndexedRecord) {
    optionalIndexedRecord = Optional.of((IndexedRecord) ome.getMessage());
    encodedRecord = encodeRecord((IndexedRecord) ome.getMessage());
  } else if (ome.getMessage() instanceof byte[]) {
    optionalIndexedRecord = Optional.empty();
    encodedRecord = (byte[]) ome.getMessage();
  } else {
    throw new IllegalArgumentException("AzureBlobAvroWriter only supports IndexedRecord and byte[].");
  }

  synchronized (currentDataFileWriterLock) {
    // if currentBlobWriterComponents is null, then it is the first blob of this AzureBlobAvroWriter object
    if (currentBlobWriterComponents == null || willCurrentBlobExceedSize(encodedRecord) || willCurrentBlobExceedRecordLimit()) {
      startNextBlob(optionalIndexedRecord);
    }
    currentBlobWriterComponents.dataFileWriter.appendEncoded(ByteBuffer.wrap(encodedRecord));
    recordsInCurrentBlob++;
    // incrementNumberOfRecordsInBlob should always be invoked every time appendEncoded above is invoked.
    // this is to count the number records in a blob and then use that count as a metadata of the blob.
    currentBlobWriterComponents.azureBlobOutputStream.incrementNumberOfRecordsInBlob();
  }
}
 
Example 5
Source File: SamzaExecutor.java    From samza with Apache License 2.0 5 votes vote down vote up
private String getPrettyFormat(OutgoingMessageEnvelope envelope) {
  String value = new String((byte[]) envelope.getMessage());
  ObjectMapper mapper = new ObjectMapper();
  String formattedValue;
  try {
    Object json = mapper.readValue(value, Object.class);
    formattedValue = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
  } catch (IOException ex) {
    formattedValue = value;
    LOG.error("getPrettyFormat failed with exception while formatting json ", ex);
  }
  return formattedValue;
}
 
Example 6
Source File: DefaultIndexRequestFactory.java    From samza with Apache License 2.0 5 votes vote down vote up
protected void setSource(OutgoingMessageEnvelope envelope, IndexRequest indexRequest) {
  Object message = envelope.getMessage();
  if (message instanceof byte[]) {
    indexRequest.source((byte[]) message);
  } else if (message instanceof Map) {
    indexRequest.source((Map) message);
  } else {
    throw new SamzaException("Unsupported message type: " + message.getClass().getCanonicalName());
  }
}
 
Example 7
Source File: SamzaExecutor.java    From samza with Apache License 2.0 4 votes vote down vote up
private String getCompressedFormat(OutgoingMessageEnvelope envelope) {
  return new String((byte[]) envelope.getMessage());
}