你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

EventHubProducerClient class

EventHubProducerClient 类用于将事件发送到事件中心。

有多种方法可以创建 EventHubProducerClient

  • 使用为事件中心实例创建的 SAS 策略中的连接字符串。
  • 使用为事件中心命名空间创建的 SAS 策略中的连接字符串,以及事件中心实例的名称
  • 使用完整命名空间(如 <yournamespace>.servicebus.windows.net)和凭据对象。

(可选)还可以传递选项包来配置重试策略或代理设置。

构造函数

EventHubProducerClient(string, EventHubClientOptions)

EventHubProducerClient 类用于将事件发送到事件中心。 使用 options 参数配置重试策略或代理设置。

EventHubProducerClient(string, string, EventHubClientOptions)

EventHubProducerClient 类用于将事件发送到事件中心。 使用 options 参数配置重试策略或代理设置。

EventHubProducerClient(string, string, TokenCredential | NamedKeyCredential | SASCredential, EventHubClientOptions)

EventHubProducerClient 类用于将事件发送到事件中心。 使用 options 参数配置重试策略或代理设置。

属性

eventHubName

创建此客户端的事件中心实例的名称。

fullyQualifiedNamespace

创建此客户端的事件中心实例的完全限定命名空间。 这很可能类似于 .servicebus.windows.net。

identifier

用于标识此 EventHubProducerClient 的名称。 如果未指定或为空,将生成随机唯一值。

方法

close()

关闭与事件中心实例的 AMQP 连接,并返回在断开连接完成后将解析的承诺。

createBatch(CreateBatchOptions)

创建一个 EventDataBatch 实例,可以在达到支持的最大大小之前添加事件。 可以将批处理传递给要发送到 Azure 事件中心的 EventHubProducerClientsendBatch 方法。

partitionKey 或 partitionId 具有不同值的事件需要放入不同的批处理中。 若要跨分区简化此类批处理管理,或者让客户端自动批处理事件并将其以特定间隔发送,请改用 EventHubBufferedProducerClient

下面的示例假定你手头有一组要安全批处理的事件。 如果事件逐个传入,建议改为 EventHubBufferedProducerClient,以便高效管理批处理。

示例用法:

const client = new EventHubProducerClient(connectionString);
let batch = await client.createBatch();
for (let i = 0; i < messages.length; i++) {
 if (!batch.tryAdd(messages[i])) {
   await client.sendBatch(batch);
   batch = await client.createBatch();
   if (!batch.tryAdd(messages[i])) {
     throw new Error("Message too big to fit")
   }
   if (i === messages.length - 1) {
     await client.sendBatch(batch);
   }
  }
}
getEventHubProperties(GetEventHubPropertiesOptions)

提供事件中心运行时信息。

getPartitionIds(GetPartitionIdsOptions)

提供与事件中心关联的每个分区的 ID。

getPartitionProperties(string, GetPartitionPropertiesOptions)

提供有关指定分区的状态的信息。

sendBatch(EventDataBatch, OperationOptions)

将使用 EventHubProducerClient.createBatch() 创建的一批事件发送到关联的事件中心。

partitionKey 或 partitionId 具有不同值的事件需要放入不同的批处理中。 若要跨分区简化此类批处理管理,或者让客户端自动批处理事件并将其以特定间隔发送,请改用 EventHubBufferedProducerClient

下面的示例假定你手头有一组要安全批处理的事件。 如果事件逐个传入,建议改为 EventHubBufferedProducerClient,以便高效管理批处理。

示例用法:

const client = new EventHubProducerClient(connectionString);
let batch = await client.createBatch();
for (let i = 0; i < messages.length; i++) {
 if (!batch.tryAdd(messages[i])) {
   await client.sendBatch(batch);
   batch = await client.createBatch();
   if (!batch.tryAdd(messages[i])) {
     throw new Error("Message too big to fit")
   }
   if (i === messages.length - 1) {
     await client.sendBatch(batch);
   }
  }
}
sendBatch(EventData[] | AmqpAnnotatedMessage[], SendBatchOptions)

将事件数组作为批处理发送到关联的事件中心。

Azure 事件中心对可以发送的批大小有限制,如果超出,则会导致代码 MessageTooLargeError错误。 若要安全地在批大小限制内发送,请改用 EventHubProducerClient.createBatch()EventHubBufferedProducerClient

示例用法:

const client = new EventHubProducerClient(connectionString);
await client.sendBatch(messages);

构造函数详细信息

EventHubProducerClient(string, EventHubClientOptions)

EventHubProducerClient 类用于将事件发送到事件中心。 使用 options 参数配置重试策略或代理设置。

new EventHubProducerClient(connectionString: string, options?: EventHubClientOptions)

参数

connectionString

string

用于连接到事件中心实例的连接字符串。 预计共享密钥属性和事件中心路径将包含在此连接字符串中。 例如,'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;EntityPath=my-event-hub-name'。

options
EventHubClientOptions

配置客户端时要应用的一组选项。

  • retryOptions:为客户端上的所有作配置重试策略。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:通过 Web 套接字配置 AMQP 连接的通道。
  • userAgent:要追加到传递给服务的内置用户代理字符串的字符串。

EventHubProducerClient(string, string, EventHubClientOptions)

EventHubProducerClient 类用于将事件发送到事件中心。 使用 options 参数配置重试策略或代理设置。

new EventHubProducerClient(connectionString: string, eventHubName: string, options?: EventHubClientOptions)

参数

connectionString

string

用于连接到事件中心命名空间的连接字符串。 预计共享密钥属性包含在此连接字符串中,但不包含事件中心路径,例如“Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;'。

eventHubName

string

要连接到客户端的特定事件中心的名称。

options
EventHubClientOptions

配置客户端时要应用的一组选项。

  • retryOptions:为客户端上的所有作配置重试策略。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:通过 Web 套接字配置 AMQP 连接的通道。
  • userAgent:要追加到传递给服务的内置用户代理字符串的字符串。

EventHubProducerClient(string, string, TokenCredential | NamedKeyCredential | SASCredential, EventHubClientOptions)

EventHubProducerClient 类用于将事件发送到事件中心。 使用 options 参数配置重试策略或代理设置。

new EventHubProducerClient(fullyQualifiedNamespace: string, eventHubName: string, credential: TokenCredential | NamedKeyCredential | SASCredential, options?: EventHubClientOptions)

参数

fullyQualifiedNamespace

string

可能类似于 .servicebus.windows.net 的完整命名空间

eventHubName

string

要连接到客户端的特定事件中心的名称。

credential

TokenCredential | NamedKeyCredential | SASCredential

客户端用于获取令牌的凭据对象,用于对与 Azure 事件中心服务的连接进行身份验证。 请参阅@azure/标识,了解如何创建支持 AAD 身份验证的凭据。如果要传入 SharedAccessKeyNameSharedAccessKey 而不使用连接字符串,请使用 @azure/core-auth 中的 AzureNamedKeyCredential。 这些字段分别映射到 AzureNamedKeyCredential中的 namekey 字段。 如果要传入 SharedAccessSignature 而不使用连接字符串,请使用 @azure/core-auth 中的 AzureSASCredential。 此字段映射到 AzureSASCredential中的 signature

options
EventHubClientOptions

配置客户端时要应用的一组选项。

  • retryOptions:为客户端上的所有作配置重试策略。 例如,{ "maxRetries": 4 }{ "maxRetries": 4, "retryDelayInMs": 30000 }
  • webSocketOptions:通过 Web 套接字配置 AMQP 连接的通道。
  • userAgent:要追加到传递给服务的内置用户代理字符串的字符串。

属性详细信息

eventHubName

创建此客户端的事件中心实例的名称。

string eventHubName

属性值

string

fullyQualifiedNamespace

创建此客户端的事件中心实例的完全限定命名空间。 这很可能类似于 .servicebus.windows.net。

string fullyQualifiedNamespace

属性值

string

identifier

用于标识此 EventHubProducerClient 的名称。 如果未指定或为空,将生成随机唯一值。

identifier: string

属性值

string

方法详细信息

close()

关闭与事件中心实例的 AMQP 连接,并返回在断开连接完成后将解析的承诺。

function close(): Promise<void>

返回

Promise<void>

承诺

createBatch(CreateBatchOptions)

创建一个 EventDataBatch 实例,可以在达到支持的最大大小之前添加事件。 可以将批处理传递给要发送到 Azure 事件中心的 EventHubProducerClientsendBatch 方法。

partitionKey 或 partitionId 具有不同值的事件需要放入不同的批处理中。 若要跨分区简化此类批处理管理,或者让客户端自动批处理事件并将其以特定间隔发送,请改用 EventHubBufferedProducerClient

下面的示例假定你手头有一组要安全批处理的事件。 如果事件逐个传入,建议改为 EventHubBufferedProducerClient,以便高效管理批处理。

示例用法:

const client = new EventHubProducerClient(connectionString);
let batch = await client.createBatch();
for (let i = 0; i < messages.length; i++) {
 if (!batch.tryAdd(messages[i])) {
   await client.sendBatch(batch);
   batch = await client.createBatch();
   if (!batch.tryAdd(messages[i])) {
     throw new Error("Message too big to fit")
   }
   if (i === messages.length - 1) {
     await client.sendBatch(batch);
   }
  }
}
function createBatch(options?: CreateBatchOptions): Promise<EventDataBatch>

参数

options
CreateBatchOptions

配置批处理的行为。

  • partitionKey:由 Azure 事件中心服务哈希处理和使用的值,用于确定事件需要发送到的分区。
  • partitionId:需要向其发送事件批处理的分区的 ID。
  • maxSizeInBytes:批大小的上限。 达到此限制后,tryAdd 函数将返回 false
  • abortSignal:向请求发出取消作的信号。

返回

Promise<EventDataBatch>

承诺

getEventHubProperties(GetEventHubPropertiesOptions)

提供事件中心运行时信息。

function getEventHubProperties(options?: GetEventHubPropertiesOptions): Promise<EventHubProperties>

参数

options
GetEventHubPropertiesOptions

要应用于作调用的选项集。

返回

一个承诺,其中包含有关事件中心实例的信息。

getPartitionIds(GetPartitionIdsOptions)

提供与事件中心关联的每个分区的 ID。

function getPartitionIds(options?: GetPartitionIdsOptions): Promise<string[]>

参数

options
GetPartitionIdsOptions

要应用于作调用的选项集。

返回

Promise<string[]>

一个承诺,它使用表示与事件中心关联的每个分区的 ID 的字符串数组进行解析。

getPartitionProperties(string, GetPartitionPropertiesOptions)

提供有关指定分区的状态的信息。

function getPartitionProperties(partitionId: string, options?: GetPartitionPropertiesOptions): Promise<PartitionProperties>

参数

partitionId

string

需要信息的分区的 ID。

options
GetPartitionPropertiesOptions

要应用于作调用的选项集。

返回

一个承诺,其中包含有关分区状态的信息。

sendBatch(EventDataBatch, OperationOptions)

将使用 EventHubProducerClient.createBatch() 创建的一批事件发送到关联的事件中心。

partitionKey 或 partitionId 具有不同值的事件需要放入不同的批处理中。 若要跨分区简化此类批处理管理,或者让客户端自动批处理事件并将其以特定间隔发送,请改用 EventHubBufferedProducerClient

下面的示例假定你手头有一组要安全批处理的事件。 如果事件逐个传入,建议改为 EventHubBufferedProducerClient,以便高效管理批处理。

示例用法:

const client = new EventHubProducerClient(connectionString);
let batch = await client.createBatch();
for (let i = 0; i < messages.length; i++) {
 if (!batch.tryAdd(messages[i])) {
   await client.sendBatch(batch);
   batch = await client.createBatch();
   if (!batch.tryAdd(messages[i])) {
     throw new Error("Message too big to fit")
   }
   if (i === messages.length - 1) {
     await client.sendBatch(batch);
   }
  }
}
function sendBatch(batch: EventDataBatch, options?: OperationOptions): Promise<void>

参数

batch
EventDataBatch

可以使用 createBatch 方法创建的一批事件。

options
OperationOptions

可以指定一组选项,以影响事件发送到关联事件中心的方式。

  • abortSignal:发出取消发送作的请求的信号。

返回

Promise<void>

承诺

sendBatch(EventData[] | AmqpAnnotatedMessage[], SendBatchOptions)

将事件数组作为批处理发送到关联的事件中心。

Azure 事件中心对可以发送的批大小有限制,如果超出,则会导致代码 MessageTooLargeError错误。 若要安全地在批大小限制内发送,请改用 EventHubProducerClient.createBatch()EventHubBufferedProducerClient

示例用法:

const client = new EventHubProducerClient(connectionString);
await client.sendBatch(messages);
function sendBatch(batch: EventData[] | AmqpAnnotatedMessage[], options?: SendBatchOptions): Promise<void>

参数

batch

EventData[] | AmqpAnnotatedMessage[]

EventDataAmqpAnnotatedMessage数组。

options
SendBatchOptions

可以指定一组选项,以影响事件发送到关联事件中心的方式。

  • abortSignal:发出取消发送作的请求的信号。
  • partitionId:此批处理将发送到的分区。 如果已设置,则无法设置 partitionKey
  • partitionKey:用于生成分区分配的哈希值。 如果已设置,则无法设置 partitionId

返回

Promise<void>

承诺