分页
更新时间:2026-09-09
Marker 分页
ListClusters、GetClusterNodes、ListClusterConfigs、ListJobs 的请求嵌入 ListRequest,使用 Marker / MaxKeys 分页;响应嵌入 ListResponse,包含 Marker、NextMarker、IsTruncated、MaxKeys。
MaxKeys 是值类型 int,未设置时为零值 0。只有取值在 1 到 1000 之间时才会作为请求参数发送,超出范围时由服务端使用默认页大小。
Go
1package kafkademo
2
3import (
4 "fmt"
5 "log"
6
7 "github.com/baidubce/bce-sdk-go/services/kafka"
8)
9
10func listAllClusters(client *kafka.Client) {
11 marker := ""
12 for {
13 request := &kafka.ListClustersRequest{
14 ListRequest: kafka.ListRequest{
15 MaxKeys: 100,
16 Marker: marker,
17 },
18 }
19
20 response, err := client.ListClusters(request)
21 if err != nil {
22 log.Fatalf("failed to list clusters: %v", err)
23 }
24
25 for _, cluster := range response.Clusters {
26 fmt.Println(cluster.ClusterID, cluster.Name)
27 }
28
29 if !response.IsTruncated || response.NextMarker == "" {
30 break
31 }
32 marker = response.NextMarker
33 }
34}
IsTruncated 与 NextMarker 都是值类型,可直接判断。翻页终止条件应同时检查两者:IsTruncated 为 false 或 NextMarker 为空时停止。
页码分页
ListTopicPartitions 的请求嵌入 PageListRequest,使用 PageNo / PageSize 分页;响应嵌入 PageListResponse,包含 PageNo、PageSize、Total。
PageNo 与 PageSize 都是 *int:为 nil 时方法内部按 1 和 10 取默认值,PageNo 始终发送,PageSize 仅在大于 0 时发送。
Go
1package kafkademo
2
3import (
4 "fmt"
5 "log"
6
7 "github.com/baidubce/bce-sdk-go/services/kafka"
8)
9
10func listTopicPartitions(client *kafka.Client) {
11 pageNo := 2
12 pageSize := 20
13
14 response, err := client.ListTopicPartitions(&kafka.ListTopicPartitionsRequest{
15 ClusterID: "{{集群 ID}}",
16 TopicName: "{{主题名称}}",
17 PageListRequest: kafka.PageListRequest{
18 PageNo: &pageNo,
19 PageSize: &pageSize,
20 },
21 })
22 if err != nil {
23 log.Fatalf("failed to list topic partitions: %v", err)
24 }
25
26 fmt.Printf("pageNo=%d pageSize=%d total=%d
27",
28 response.PageNo, response.PageSize, response.Total)
29 for _, partition := range response.Partitions {
30 fmt.Printf("partitionId=%d leaderId=%d messageNum=%d
31",
32 partition.PartitionID, partition.LeaderID, partition.MessageNum)
33 }
34}
过滤参数
部分列表接口支持服务端过滤,空字符串或 nil 的过滤条件不会发送:
| 接口 | 可选过滤参数 |
|---|---|
ListClusters |
ClusterName、State、Mode、KafkaVersion、Payment、TagKey + TagValue |
GetClusterNodes |
State |
ListClusterConfigs |
ConfigName、State |
ListClusterConfigRevisions |
State |
ListTopic |
TopicName(前缀匹配) |
ListConsumerGroup |
GroupName |
ListJobs |
Name |
ListQuotas |
EntityType |
ListAcls |
Username、PatternType、ResourceType、ResourceName |
ListClusters 的标签过滤要求 TagKey 与 TagValue 成对出现:只设置 TagKey 返回 request tagValue should not be nil,只设置 TagValue 返回 request tagKey should not be null or empty。注意 TagValue 是 *string,允许按空标签值过滤。
评价此篇文章
