use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class CreateTopicsRequest method toStruct.
/**
* Visible for testing.
*/
@Override
public Struct toStruct() {
short version = version();
Struct struct = new Struct(ApiKeys.CREATE_TOPICS.requestSchema(version));
List<Struct> createTopicRequestStructs = new ArrayList<>(topics.size());
for (Map.Entry<String, TopicDetails> entry : topics.entrySet()) {
Struct singleRequestStruct = struct.instance(REQUESTS_KEY_NAME);
String topic = entry.getKey();
TopicDetails args = entry.getValue();
singleRequestStruct.set(TOPIC_KEY_NAME, topic);
singleRequestStruct.set(NUM_PARTITIONS_KEY_NAME, args.numPartitions);
singleRequestStruct.set(REPLICATION_FACTOR_KEY_NAME, args.replicationFactor);
// replica assignment
List<Struct> replicaAssignmentsStructs = new ArrayList<>(args.replicasAssignments.size());
for (Map.Entry<Integer, List<Integer>> partitionReplicaAssignment : args.replicasAssignments.entrySet()) {
Struct replicaAssignmentStruct = singleRequestStruct.instance(REPLICA_ASSIGNMENT_KEY_NAME);
replicaAssignmentStruct.set(REPLICA_ASSIGNMENT_PARTITION_ID_KEY_NAME, partitionReplicaAssignment.getKey());
replicaAssignmentStruct.set(REPLICA_ASSIGNMENT_REPLICAS_KEY_NAME, partitionReplicaAssignment.getValue().toArray());
replicaAssignmentsStructs.add(replicaAssignmentStruct);
}
singleRequestStruct.set(REPLICA_ASSIGNMENT_KEY_NAME, replicaAssignmentsStructs.toArray());
// configs
List<Struct> configsStructs = new ArrayList<>(args.configs.size());
for (Map.Entry<String, String> configEntry : args.configs.entrySet()) {
Struct configStruct = singleRequestStruct.instance(CONFIGS_KEY_NAME);
configStruct.set(CONFIG_KEY_KEY_NAME, configEntry.getKey());
configStruct.set(CONFIG_VALUE_KEY_NAME, configEntry.getValue());
configsStructs.add(configStruct);
}
singleRequestStruct.set(CONFIGS_KEY_NAME, configsStructs.toArray());
createTopicRequestStructs.add(singleRequestStruct);
}
struct.set(REQUESTS_KEY_NAME, createTopicRequestStructs.toArray());
struct.set(TIMEOUT_KEY_NAME, timeout);
if (version >= 1)
struct.set(VALIDATE_ONLY_KEY_NAME, validateOnly);
return struct;
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class ListOffsetResponse method toStruct.
@Override
protected Struct toStruct(short version) {
Struct struct = new Struct(ApiKeys.LIST_OFFSETS.responseSchema(version));
Map<String, Map<Integer, PartitionData>> topicsData = CollectionUtils.groupDataByTopic(responseData);
List<Struct> topicArray = new ArrayList<>();
for (Map.Entry<String, Map<Integer, PartitionData>> topicEntry : topicsData.entrySet()) {
Struct topicData = struct.instance(RESPONSES_KEY_NAME);
topicData.set(TOPIC_KEY_NAME, topicEntry.getKey());
List<Struct> partitionArray = new ArrayList<>();
for (Map.Entry<Integer, PartitionData> partitionEntry : topicEntry.getValue().entrySet()) {
PartitionData offsetPartitionData = partitionEntry.getValue();
Struct partitionData = topicData.instance(PARTITIONS_KEY_NAME);
partitionData.set(PARTITION_KEY_NAME, partitionEntry.getKey());
partitionData.set(ERROR_CODE_KEY_NAME, offsetPartitionData.error.code());
if (version == 0)
partitionData.set(OFFSETS_KEY_NAME, offsetPartitionData.offsets.toArray());
else {
partitionData.set(TIMESTAMP_KEY_NAME, offsetPartitionData.timestamp);
partitionData.set(OFFSET_KEY_NAME, offsetPartitionData.offset);
}
partitionArray.add(partitionData);
}
topicData.set(PARTITIONS_KEY_NAME, partitionArray.toArray());
topicArray.add(topicData);
}
struct.set(RESPONSES_KEY_NAME, topicArray.toArray());
return struct;
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class MetadataResponse method toStruct.
@Override
protected Struct toStruct(short version) {
Struct struct = new Struct(ApiKeys.METADATA.responseSchema(version));
List<Struct> brokerArray = new ArrayList<>();
for (Node node : brokers) {
Struct broker = struct.instance(BROKERS_KEY_NAME);
broker.set(NODE_ID_KEY_NAME, node.id());
broker.set(HOST_KEY_NAME, node.host());
broker.set(PORT_KEY_NAME, node.port());
// This field only exists in v1+
if (broker.hasField(RACK_KEY_NAME))
broker.set(RACK_KEY_NAME, node.rack());
brokerArray.add(broker);
}
struct.set(BROKERS_KEY_NAME, brokerArray.toArray());
// This field only exists in v1+
if (struct.hasField(CONTROLLER_ID_KEY_NAME))
struct.set(CONTROLLER_ID_KEY_NAME, controller == null ? NO_CONTROLLER_ID : controller.id());
// This field only exists in v2+
if (struct.hasField(CLUSTER_ID_KEY_NAME))
struct.set(CLUSTER_ID_KEY_NAME, clusterId);
List<Struct> topicMetadataArray = new ArrayList<>(topicMetadata.size());
for (TopicMetadata metadata : topicMetadata) {
Struct topicData = struct.instance(TOPIC_METADATA_KEY_NAME);
topicData.set(TOPIC_KEY_NAME, metadata.topic);
topicData.set(TOPIC_ERROR_CODE_KEY_NAME, metadata.error.code());
// This field only exists in v1+
if (topicData.hasField(IS_INTERNAL_KEY_NAME))
topicData.set(IS_INTERNAL_KEY_NAME, metadata.isInternal());
List<Struct> partitionMetadataArray = new ArrayList<>(metadata.partitionMetadata.size());
for (PartitionMetadata partitionMetadata : metadata.partitionMetadata()) {
Struct partitionData = topicData.instance(PARTITION_METADATA_KEY_NAME);
partitionData.set(PARTITION_ERROR_CODE_KEY_NAME, partitionMetadata.error.code());
partitionData.set(PARTITION_KEY_NAME, partitionMetadata.partition);
partitionData.set(LEADER_KEY_NAME, partitionMetadata.leader.id());
ArrayList<Integer> replicas = new ArrayList<>(partitionMetadata.replicas.size());
for (Node node : partitionMetadata.replicas) replicas.add(node.id());
partitionData.set(REPLICAS_KEY_NAME, replicas.toArray());
ArrayList<Integer> isr = new ArrayList<>(partitionMetadata.isr.size());
for (Node node : partitionMetadata.isr) isr.add(node.id());
partitionData.set(ISR_KEY_NAME, isr.toArray());
partitionMetadataArray.add(partitionData);
}
topicData.set(PARTITION_METADATA_KEY_NAME, partitionMetadataArray.toArray());
topicMetadataArray.add(topicData);
}
struct.set(TOPIC_METADATA_KEY_NAME, topicMetadataArray.toArray());
return struct;
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class OffsetCommitRequest method toStruct.
@Override
public Struct toStruct() {
short version = version();
Struct struct = new Struct(ApiKeys.OFFSET_COMMIT.requestSchema(version));
struct.set(GROUP_ID_KEY_NAME, groupId);
Map<String, Map<Integer, PartitionData>> topicsData = CollectionUtils.groupDataByTopic(offsetData);
List<Struct> topicArray = new ArrayList<>();
for (Map.Entry<String, Map<Integer, PartitionData>> topicEntry : topicsData.entrySet()) {
Struct topicData = struct.instance(TOPICS_KEY_NAME);
topicData.set(TOPIC_KEY_NAME, topicEntry.getKey());
List<Struct> partitionArray = new ArrayList<>();
for (Map.Entry<Integer, PartitionData> partitionEntry : topicEntry.getValue().entrySet()) {
PartitionData fetchPartitionData = partitionEntry.getValue();
Struct partitionData = topicData.instance(PARTITIONS_KEY_NAME);
partitionData.set(PARTITION_KEY_NAME, partitionEntry.getKey());
partitionData.set(COMMIT_OFFSET_KEY_NAME, fetchPartitionData.offset);
// Only for v1
if (partitionData.hasField(TIMESTAMP_KEY_NAME))
partitionData.set(TIMESTAMP_KEY_NAME, fetchPartitionData.timestamp);
partitionData.set(METADATA_KEY_NAME, fetchPartitionData.metadata);
partitionArray.add(partitionData);
}
topicData.set(PARTITIONS_KEY_NAME, partitionArray.toArray());
topicArray.add(topicData);
}
struct.set(TOPICS_KEY_NAME, topicArray.toArray());
if (struct.hasField(GENERATION_ID_KEY_NAME))
struct.set(GENERATION_ID_KEY_NAME, generationId);
if (struct.hasField(MEMBER_ID_KEY_NAME))
struct.set(MEMBER_ID_KEY_NAME, memberId);
if (struct.hasField(RETENTION_TIME_KEY_NAME))
struct.set(RETENTION_TIME_KEY_NAME, retentionTime);
return struct;
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class OffsetFetchRequest method toStruct.
@Override
protected Struct toStruct() {
Struct struct = new Struct(ApiKeys.OFFSET_FETCH.requestSchema(version()));
struct.set(GROUP_ID_KEY_NAME, groupId);
if (partitions != null) {
Map<String, List<Integer>> topicsData = CollectionUtils.groupDataByTopic(partitions);
List<Struct> topicArray = new ArrayList<>();
for (Map.Entry<String, List<Integer>> entries : topicsData.entrySet()) {
Struct topicData = struct.instance(TOPICS_KEY_NAME);
topicData.set(TOPIC_KEY_NAME, entries.getKey());
List<Struct> partitionArray = new ArrayList<>();
for (Integer partitionId : entries.getValue()) {
Struct partitionData = topicData.instance(PARTITIONS_KEY_NAME);
partitionData.set(PARTITION_KEY_NAME, partitionId);
partitionArray.add(partitionData);
}
topicData.set(PARTITIONS_KEY_NAME, partitionArray.toArray());
topicArray.add(topicData);
}
struct.set(TOPICS_KEY_NAME, topicArray.toArray());
} else
struct.set(TOPICS_KEY_NAME, null);
return struct;
}
Aggregations