更新时间:2024-10-21 GMT+08:00

Kafka实例开始分区重平衡任务

功能介绍

该接口用于向Kafka实例提交分区重平衡任务或计算重平衡预估时间。

调用方法

请参见如何调用API

URI

POST /v2/kafka/{project_id}/instances/{instance_id}/reassign

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID,获取方式请参见获取项目ID

instance_id

String

实例ID。

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

reassignments

Array of PartitionReassignEntity objects

重平衡分配方案。

throttle

Integer

重平衡门限值。

is_schedule

Boolean

是否作为定时任务执行。若非定时执行,则is_schedule和execute_at字段可为空;若为定时执行,is_schedule为true,execute_at字段非空。

execute_at

Long

定时时间,格式为Unix时间戳,单位为毫秒

time_estimate

Boolean

设为true表示执行时间预估任务,false为执行重平衡任务。

表3 PartitionReassignEntity

参数

是否必选

参数类型

描述

topic

String

topic名称

brokers

Array of integers

分区重平衡到的broker列表,自动生成分配方案时需指定该参数。

replication_factor

Integer

副本因子,自动生成分配方案时可指定。

assignment

Array of TopicAssignment objects

手动指定的分配方案。brokers参数与该参数不能同时为空。

表4 TopicAssignment

参数

是否必选

参数类型

描述

partition

Integer

手动指定分配方案时的分区号。

partition_brokers

Array of integers

手动指定某个分区将要分配的broker列表

响应参数

状态码: 200

表5 响应Body参数

参数

参数类型

描述

job_id

String

任务ID(当执行重平衡任务时仅返回job_id)。

reassignment_time

Integer

预估时间,单位为秒(当执行预估时间任务时仅返回reassignment_time)。

请求示例

POST https://{endpoint}/v2/kafka/{project_id}/instances/{instance_id}/reassign

{
  "reassignments" : [ {
    "topic" : "topic-1513476102",
    "brokers" : [ 0, 1, 2 ],
    "replication_factor" : 3,
    "assignment" : [ {
      "partition" : 0,
      "partition_brokers" : [ 0, 1, 2 ]
    }, {
      "partition" : 1,
      "partition_brokers" : [ 1, 2, 0 ]
    }, {
      "partition" : 2,
      "partition_brokers" : [ 2, 0, 1 ]
    } ]
  }, {
    "topic" : "topic-1513558717",
    "brokers" : [ 0, 1, 4 ],
    "replication_factor" : 3,
    "assignment" : [ {
      "partition" : 0,
      "partition_brokers" : [ 0, 1, 2 ]
    }, {
      "partition" : 1,
      "partition_brokers" : [ 1, 2, 0 ]
    }, {
      "partition" : 2,
      "partition_brokers" : [ 2, 0, 1 ]
    } ]
  } ],
  "throttle" : 10000000,
  "time_estimate" : false
}

响应示例

状态码: 200

提交重平衡任务成功(若为预估时间任务则返回预估时间)。

{
  "job_id" : "8a2c259182ab0e9d0182ab1882560009",
  "reassignment_time" : 10
}

SDK代码示例

SDK代码示例如下。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.kafka.v2.region.KafkaRegion;
import com.huaweicloud.sdk.kafka.v2.*;
import com.huaweicloud.sdk.kafka.v2.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateReassignmentTaskSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        KafkaClient client = KafkaClient.newBuilder()
                .withCredential(auth)
                .withRegion(KafkaRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateReassignmentTaskRequest request = new CreateReassignmentTaskRequest();
        request.withInstanceId("{instance_id}");
        PartitionReassignRequest body = new PartitionReassignRequest();
        List<Integer> listAssignmentPartitionBrokers = new ArrayList<>();
        listAssignmentPartitionBrokers.add(2);
        listAssignmentPartitionBrokers.add(0);
        listAssignmentPartitionBrokers.add(1);
        List<Integer> listAssignmentPartitionBrokers1 = new ArrayList<>();
        listAssignmentPartitionBrokers1.add(1);
        listAssignmentPartitionBrokers1.add(2);
        listAssignmentPartitionBrokers1.add(0);
        List<Integer> listAssignmentPartitionBrokers2 = new ArrayList<>();
        listAssignmentPartitionBrokers2.add(0);
        listAssignmentPartitionBrokers2.add(1);
        listAssignmentPartitionBrokers2.add(2);
        List<TopicAssignment> listReassignmentsAssignment = new ArrayList<>();
        listReassignmentsAssignment.add(
            new TopicAssignment()
                .withPartition(0)
                .withPartitionBrokers(listAssignmentPartitionBrokers2)
        );
        listReassignmentsAssignment.add(
            new TopicAssignment()
                .withPartition(1)
                .withPartitionBrokers(listAssignmentPartitionBrokers1)
        );
        listReassignmentsAssignment.add(
            new TopicAssignment()
                .withPartition(2)
                .withPartitionBrokers(listAssignmentPartitionBrokers)
        );
        List<Integer> listReassignmentsBrokers = new ArrayList<>();
        listReassignmentsBrokers.add(0);
        listReassignmentsBrokers.add(1);
        listReassignmentsBrokers.add(4);
        List<Integer> listAssignmentPartitionBrokers3 = new ArrayList<>();
        listAssignmentPartitionBrokers3.add(2);
        listAssignmentPartitionBrokers3.add(0);
        listAssignmentPartitionBrokers3.add(1);
        List<Integer> listAssignmentPartitionBrokers4 = new ArrayList<>();
        listAssignmentPartitionBrokers4.add(1);
        listAssignmentPartitionBrokers4.add(2);
        listAssignmentPartitionBrokers4.add(0);
        List<Integer> listAssignmentPartitionBrokers5 = new ArrayList<>();
        listAssignmentPartitionBrokers5.add(0);
        listAssignmentPartitionBrokers5.add(1);
        listAssignmentPartitionBrokers5.add(2);
        List<TopicAssignment> listReassignmentsAssignment1 = new ArrayList<>();
        listReassignmentsAssignment1.add(
            new TopicAssignment()
                .withPartition(0)
                .withPartitionBrokers(listAssignmentPartitionBrokers5)
        );
        listReassignmentsAssignment1.add(
            new TopicAssignment()
                .withPartition(1)
                .withPartitionBrokers(listAssignmentPartitionBrokers4)
        );
        listReassignmentsAssignment1.add(
            new TopicAssignment()
                .withPartition(2)
                .withPartitionBrokers(listAssignmentPartitionBrokers3)
        );
        List<Integer> listReassignmentsBrokers1 = new ArrayList<>();
        listReassignmentsBrokers1.add(0);
        listReassignmentsBrokers1.add(1);
        listReassignmentsBrokers1.add(2);
        List<PartitionReassignEntity> listbodyReassignments = new ArrayList<>();
        listbodyReassignments.add(
            new PartitionReassignEntity()
                .withTopic("topic-1513476102")
                .withBrokers(listReassignmentsBrokers1)
                .withReplicationFactor(3)
                .withAssignment(listReassignmentsAssignment1)
        );
        listbodyReassignments.add(
            new PartitionReassignEntity()
                .withTopic("topic-1513558717")
                .withBrokers(listReassignmentsBrokers)
                .withReplicationFactor(3)
                .withAssignment(listReassignmentsAssignment)
        );
        body.withTimeEstimate(false);
        body.withThrottle(10000000);
        body.withReassignments(listbodyReassignments);
        request.withBody(body);
        try {
            CreateReassignmentTaskResponse response = client.createReassignmentTask(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkkafka.v2.region.kafka_region import KafkaRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkkafka.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = KafkaClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(KafkaRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateReassignmentTaskRequest()
        request.instance_id = "{instance_id}"
        listPartitionBrokersAssignment = [
            2,
            0,
            1
        ]
        listPartitionBrokersAssignment1 = [
            1,
            2,
            0
        ]
        listPartitionBrokersAssignment2 = [
            0,
            1,
            2
        ]
        listAssignmentReassignments = [
            TopicAssignment(
                partition=0,
                partition_brokers=listPartitionBrokersAssignment2
            ),
            TopicAssignment(
                partition=1,
                partition_brokers=listPartitionBrokersAssignment1
            ),
            TopicAssignment(
                partition=2,
                partition_brokers=listPartitionBrokersAssignment
            )
        ]
        listBrokersReassignments = [
            0,
            1,
            4
        ]
        listPartitionBrokersAssignment3 = [
            2,
            0,
            1
        ]
        listPartitionBrokersAssignment4 = [
            1,
            2,
            0
        ]
        listPartitionBrokersAssignment5 = [
            0,
            1,
            2
        ]
        listAssignmentReassignments1 = [
            TopicAssignment(
                partition=0,
                partition_brokers=listPartitionBrokersAssignment5
            ),
            TopicAssignment(
                partition=1,
                partition_brokers=listPartitionBrokersAssignment4
            ),
            TopicAssignment(
                partition=2,
                partition_brokers=listPartitionBrokersAssignment3
            )
        ]
        listBrokersReassignments1 = [
            0,
            1,
            2
        ]
        listReassignmentsbody = [
            PartitionReassignEntity(
                topic="topic-1513476102",
                brokers=listBrokersReassignments1,
                replication_factor=3,
                assignment=listAssignmentReassignments1
            ),
            PartitionReassignEntity(
                topic="topic-1513558717",
                brokers=listBrokersReassignments,
                replication_factor=3,
                assignment=listAssignmentReassignments
            )
        ]
        request.body = PartitionReassignRequest(
            time_estimate=False,
            throttle=10000000,
            reassignments=listReassignmentsbody
        )
        response = client.create_reassignment_task(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    kafka "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/kafka/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/kafka/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/kafka/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := kafka.NewKafkaClient(
        kafka.KafkaClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateReassignmentTaskRequest{}
	request.InstanceId = "{instance_id}"
	var listPartitionBrokersAssignment = []int32{
        int32(2),
	    int32(0),
	    int32(1),
    }
	var listPartitionBrokersAssignment1 = []int32{
        int32(1),
	    int32(2),
	    int32(0),
    }
	var listPartitionBrokersAssignment2 = []int32{
        int32(0),
	    int32(1),
	    int32(2),
    }
	partitionAssignment:= int32(0)
	partitionAssignment1:= int32(1)
	partitionAssignment2:= int32(2)
	var listAssignmentReassignments = []model.TopicAssignment{
        {
            Partition: &partitionAssignment,
            PartitionBrokers: &listPartitionBrokersAssignment2,
        },
        {
            Partition: &partitionAssignment1,
            PartitionBrokers: &listPartitionBrokersAssignment1,
        },
        {
            Partition: &partitionAssignment2,
            PartitionBrokers: &listPartitionBrokersAssignment,
        },
    }
	var listBrokersReassignments = []int32{
        int32(0),
	    int32(1),
	    int32(4),
    }
	var listPartitionBrokersAssignment3 = []int32{
        int32(2),
	    int32(0),
	    int32(1),
    }
	var listPartitionBrokersAssignment4 = []int32{
        int32(1),
	    int32(2),
	    int32(0),
    }
	var listPartitionBrokersAssignment5 = []int32{
        int32(0),
	    int32(1),
	    int32(2),
    }
	partitionAssignment3:= int32(0)
	partitionAssignment4:= int32(1)
	partitionAssignment5:= int32(2)
	var listAssignmentReassignments1 = []model.TopicAssignment{
        {
            Partition: &partitionAssignment3,
            PartitionBrokers: &listPartitionBrokersAssignment5,
        },
        {
            Partition: &partitionAssignment4,
            PartitionBrokers: &listPartitionBrokersAssignment4,
        },
        {
            Partition: &partitionAssignment5,
            PartitionBrokers: &listPartitionBrokersAssignment3,
        },
    }
	var listBrokersReassignments1 = []int32{
        int32(0),
	    int32(1),
	    int32(2),
    }
	replicationFactorReassignments:= int32(3)
	replicationFactorReassignments1:= int32(3)
	var listReassignmentsbody = []model.PartitionReassignEntity{
        {
            Topic: "topic-1513476102",
            Brokers: &listBrokersReassignments1,
            ReplicationFactor: &replicationFactorReassignments,
            Assignment: &listAssignmentReassignments1,
        },
        {
            Topic: "topic-1513558717",
            Brokers: &listBrokersReassignments,
            ReplicationFactor: &replicationFactorReassignments1,
            Assignment: &listAssignmentReassignments,
        },
    }
	timeEstimatePartitionReassignRequest:= false
	throttlePartitionReassignRequest:= int32(10000000)
	request.Body = &model.PartitionReassignRequest{
		TimeEstimate: &timeEstimatePartitionReassignRequest,
		Throttle: &throttlePartitionReassignRequest,
		Reassignments: listReassignmentsbody,
	}
	response, err := client.CreateReassignmentTask(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

提交重平衡任务成功(若为预估时间任务则返回预估时间)。

错误码

请参见错误码