Package com.azure.messaging.eventhubs
Class EventData
java.lang.Object
com.azure.core.models.MessageContent
com.azure.messaging.eventhubs.EventData
public class EventData
extends com.azure.core.models.MessageContent
The data structure encapsulating the event being sent-to and received-from Event Hubs. Each Event Hub partition
can be visualized as a stream of EventData
. This class is not thread-safe.
-
Constructor Summary
ConstructorDescriptionCreates an event with an empty body.EventData
(byte[] body) Creates an event containing thebody
.EventData
(com.azure.core.util.BinaryData body) Creates an event with the providedBinaryData
as payload.Creates an event by encoding thebody
using UTF-8 charset.EventData
(ByteBuffer body) Creates an event containing thebody
. -
Method Summary
Modifier and TypeMethodDescriptionaddContext
(String key, Object value) Adds a new key value pair to the existing context on Event Data.boolean
byte[]
getBody()
Gets the actual payload/data wrapped by EventData.com.azure.core.util.BinaryData
Returns theBinaryData
payload associated with this event.Returns event data as UTF-8 decoded string.Gets the MIME type describing the data contained in thegetBody()
, intended to allow consumers to make informed decisions for inspecting and processing the event.Gets an application-defined value that represents the context to use for correlation across one or more operations.Gets the instant, in UTC, of when the event was enqueued in the Event Hub partition.Gets an application-defined value that uniquely identifies the event.Gets the offset of the event when it was received from the associated Event Hub partition.Gets the partition hashing key if it was set when originally publishing the event.Gets the set of free-form event properties which may be used for passing metadata associated with the event with the event body during Event Hubs operations.com.azure.core.amqp.models.AmqpAnnotatedMessage
Gets the underlying AMQP message.Gets the sequence number assigned to the event when it was enqueued in the associated Event Hub partition.Properties that are populated by Event Hubs service.int
hashCode()
Gets a hash of the binary contents ingetBody()
.setBodyAsBinaryData
(com.azure.core.util.BinaryData binaryData) Sets a new binary body and correspondingAmqpAnnotatedMessage
on the event.setContentType
(String contentType) Sets the MIME type describing the data contained in thegetBody()
, intended to allow consumers to make informed decisions for inspecting and processing the event.setCorrelationId
(String correlationId) Sets an application-defined value that represents the context to use for correlation across one or more operations.setMessageId
(String messageId) Sets an application-defined value that uniquely identifies the event.
-
Constructor Details
-
EventData
public EventData()Creates an event with an empty body. -
EventData
public EventData(byte[] body) Creates an event containing thebody
.- Parameters:
body
- The data to set for this event.- Throws:
NullPointerException
- ifbody
isnull
.
-
EventData
Creates an event containing thebody
.- Parameters:
body
- The data to set for this event.- Throws:
NullPointerException
- ifbody
isnull
.
-
EventData
Creates an event by encoding thebody
using UTF-8 charset.- Parameters:
body
- The string that will be UTF-8 encoded to create an event.- Throws:
NullPointerException
- ifbody
isnull
.
-
EventData
public EventData(com.azure.core.util.BinaryData body) Creates an event with the providedBinaryData
as payload.- Parameters:
body
- TheBinaryData
payload for this event.
-
-
Method Details
-
getProperties
Gets the set of free-form event properties which may be used for passing metadata associated with the event with the event body during Event Hubs operations. A common use-case forproperties()
is to associate serialization hints for thegetBody()
as an aid to consumers who wish to deserialize the binary data.Adding serialization hint using
getProperties()
In the sample, the type of telemetry is indicated by adding an application property with key "eventType".
TelemetryEvent telemetry = new TelemetryEvent("temperature", "37"); byte[] serializedTelemetryData = telemetry.toString().getBytes(UTF_8); EventData eventData = new EventData(serializedTelemetryData); eventData.getProperties().put("eventType", TelemetryEvent.class.getName());
The following types are supported:
-
getSystemProperties
Properties that are populated by Event Hubs service. As these are populated by the Event Hubs service, they are only present on a receivedEventData
. Provides an abstraction on top of properties exposed bygetRawAmqpMessage()
. These properties are read-only and can be modified viagetRawAmqpMessage()
. -
getBody
public byte[] getBody()Gets the actual payload/data wrapped by EventData.If the means for deserializing the raw data is not apparent to consumers, a common technique is to make use of
getProperties()
when creating the event, to associate serialization hints as an aid to consumers who wish to deserialize the binary data.- Returns:
- A byte array representing the data.
-
getBodyAsString
Returns event data as UTF-8 decoded string.- Returns:
- UTF-8 decoded string representation of the event data.
-
getBodyAsBinaryData
public com.azure.core.util.BinaryData getBodyAsBinaryData()Returns theBinaryData
payload associated with this event.- Overrides:
getBodyAsBinaryData
in classcom.azure.core.models.MessageContent
- Returns:
- the
BinaryData
payload associated with this event.
-
setBodyAsBinaryData
Sets a new binary body and correspondingAmqpAnnotatedMessage
on the event. Contents fromgetRawAmqpMessage()
are shallow copied to the new underlying message.- Overrides:
setBodyAsBinaryData
in classcom.azure.core.models.MessageContent
-
getOffset
Gets the offset of the event when it was received from the associated Event Hub partition. This is only present on a receivedEventData
.- Returns:
- The offset within the Event Hub partition of the received event.
null
if theEventData
was not received from Event Hubs service.
-
getPartitionKey
Gets the partition hashing key if it was set when originally publishing the event. If it exists, this value was used to compute a hash to select a partition to send the message to. This is only present on a receivedEventData
.- Returns:
- A partition key for this Event Data.
null
if theEventData
was not received from Event Hubs service or there was no partition key set when the event was sent to the Event Hub.
-
getEnqueuedTime
Gets the instant, in UTC, of when the event was enqueued in the Event Hub partition. This is only present on a receivedEventData
.- Returns:
- The instant, in UTC, this was enqueued in the Event Hub partition.
null
if theEventData
was not received from Event Hubs service.
-
getSequenceNumber
Gets the sequence number assigned to the event when it was enqueued in the associated Event Hub partition. This is unique for every message received in the Event Hub partition. This is only present on a receivedEventData
.- Returns:
- The sequence number for this event.
null
if theEventData
was not received from Event Hubs service.
-
getRawAmqpMessage
public com.azure.core.amqp.models.AmqpAnnotatedMessage getRawAmqpMessage()Gets the underlying AMQP message.- Returns:
- The underlying AMQP message.
-
getContentType
Gets the MIME type describing the data contained in thegetBody()
, intended to allow consumers to make informed decisions for inspecting and processing the event.- Overrides:
getContentType
in classcom.azure.core.models.MessageContent
- Returns:
- The content type.
-
setContentType
Sets the MIME type describing the data contained in thegetBody()
, intended to allow consumers to make informed decisions for inspecting and processing the event.- Overrides:
setContentType
in classcom.azure.core.models.MessageContent
- Parameters:
contentType
- The content type.- Returns:
- The updated
EventData
.
-
getCorrelationId
Gets an application-defined value that represents the context to use for correlation across one or more operations. The identifier is a free-form value and may reflect a unique identity or a shared data element with significance to the application.- Returns:
- The correlation id.
null
if there is none set.
-
setCorrelationId
Sets an application-defined value that represents the context to use for correlation across one or more operations. The identifier is a free-form value and may reflect a unique identity or a shared data element with significance to the application.- Parameters:
correlationId
- The correlation id.- Returns:
- The updated
EventData
.
-
getMessageId
Gets an application-defined value that uniquely identifies the event. The identifier is a free-form value and can reflect a GUID or an identifier derived from the application context.- Returns:
- The message id.
null
if there is none set.
-
setMessageId
Sets an application-defined value that uniquely identifies the event. The identifier is a free-form value and can reflect a GUID or an identifier derived from the application context.- Parameters:
messageId
- The message id.- Returns:
- The updated
EventData
.
-
equals
-
hashCode
public int hashCode()Gets a hash of the binary contents ingetBody()
. -
addContext
Adds a new key value pair to the existing context on Event Data.- Parameters:
key
- The key for this context objectvalue
- The value for this context object.- Returns:
- The updated
EventData
. - Throws:
NullPointerException
- ifkey
orvalue
is null.
-