use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class ConsumerProtocol method deserializeSubscription.
public static PartitionAssignor.Subscription deserializeSubscription(ByteBuffer buffer) {
Struct header = CONSUMER_PROTOCOL_HEADER_SCHEMA.read(buffer);
Short version = header.getShort(VERSION_KEY_NAME);
checkVersionCompatibility(version);
Struct struct = SUBSCRIPTION_V0.read(buffer);
ByteBuffer userData = struct.getBytes(USER_DATA_KEY_NAME);
List<String> topics = new ArrayList<>();
for (Object topicObj : struct.getArray(TOPICS_KEY_NAME)) topics.add((String) topicObj);
return new PartitionAssignor.Subscription(topics, userData);
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class ConsumerProtocol method deserializeAssignment.
public static PartitionAssignor.Assignment deserializeAssignment(ByteBuffer buffer) {
Struct header = CONSUMER_PROTOCOL_HEADER_SCHEMA.read(buffer);
Short version = header.getShort(VERSION_KEY_NAME);
checkVersionCompatibility(version);
Struct struct = ASSIGNMENT_V0.read(buffer);
ByteBuffer userData = struct.getBytes(USER_DATA_KEY_NAME);
List<TopicPartition> partitions = new ArrayList<>();
for (Object structObj : struct.getArray(TOPIC_PARTITIONS_KEY_NAME)) {
Struct assignment = (Struct) structObj;
String topic = assignment.getString(TOPIC_KEY_NAME);
for (Object partitionObj : assignment.getArray(PARTITIONS_KEY_NAME)) {
Integer partition = (Integer) partitionObj;
partitions.add(new TopicPartition(topic, partition));
}
}
return new PartitionAssignor.Assignment(partitions, userData);
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class ConsumerProtocol method serializeAssignment.
public static ByteBuffer serializeAssignment(PartitionAssignor.Assignment assignment) {
Struct struct = new Struct(ASSIGNMENT_V0);
struct.set(USER_DATA_KEY_NAME, assignment.userData());
List<Struct> topicAssignments = new ArrayList<>();
for (Map.Entry<String, List<Integer>> topicEntry : asMap(assignment.partitions()).entrySet()) {
Struct topicAssignment = new Struct(TOPIC_ASSIGNMENT_V0);
topicAssignment.set(TOPIC_KEY_NAME, topicEntry.getKey());
topicAssignment.set(PARTITIONS_KEY_NAME, topicEntry.getValue().toArray());
topicAssignments.add(topicAssignment);
}
struct.set(TOPIC_PARTITIONS_KEY_NAME, topicAssignments.toArray());
ByteBuffer buffer = ByteBuffer.allocate(CONSUMER_PROTOCOL_HEADER_V0.sizeOf() + ASSIGNMENT_V0.sizeOf(struct));
CONSUMER_PROTOCOL_HEADER_V0.writeTo(buffer);
ASSIGNMENT_V0.write(buffer, struct);
buffer.flip();
return buffer;
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class NetworkClient method parseResponse.
public static AbstractResponse parseResponse(ByteBuffer responseBuffer, RequestHeader requestHeader) {
ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer);
// Always expect the response version id to be the same as the request version id
ApiKeys apiKey = ApiKeys.forId(requestHeader.apiKey());
Struct responseBody = apiKey.responseSchema(requestHeader.apiVersion()).read(responseBuffer);
correlate(requestHeader, responseHeader);
return AbstractResponse.getResponse(apiKey, responseBody);
}
use of org.apache.kafka.common.protocol.types.Struct in project kafka by apache.
the class RequestResponseTest method verifyFetchResponseFullWrite.
@Test
public void verifyFetchResponseFullWrite() throws Exception {
FetchResponse fetchResponse = createFetchResponse();
RequestHeader header = new RequestHeader(ApiKeys.FETCH.id, ApiKeys.FETCH.latestVersion(), "client", 15);
Send send = fetchResponse.toSend("1", header);
ByteBufferChannel channel = new ByteBufferChannel(send.size());
send.writeTo(channel);
channel.close();
ByteBuffer buf = channel.buf;
// read the size
int size = buf.getInt();
assertTrue(size > 0);
// read the header
ResponseHeader responseHeader = ResponseHeader.parse(channel.buf);
assertEquals(header.correlationId(), responseHeader.correlationId());
// read the body
Struct responseBody = ApiKeys.FETCH.responseSchema(header.apiVersion()).read(buf);
assertEquals(fetchResponse.toStruct(header.apiVersion()), responseBody);
assertEquals(size, responseHeader.sizeOf() + responseBody.sizeOf());
}
Aggregations