Search in sources :

Example 1 with Struct

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);
}
Also used : ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 2 with Struct

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);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 3 with Struct

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;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 4 with Struct

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);
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ResponseHeader(org.apache.kafka.common.requests.ResponseHeader) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 5 with Struct

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());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Send(org.apache.kafka.common.network.Send) Struct(org.apache.kafka.common.protocol.types.Struct) Test(org.junit.Test)

Aggregations

Struct (org.apache.kafka.common.protocol.types.Struct)215 ArrayList (java.util.ArrayList)94 Map (java.util.Map)69 HashMap (java.util.HashMap)64 ByteBuffer (java.nio.ByteBuffer)62 TopicPartition (org.apache.kafka.common.TopicPartition)29 List (java.util.List)16 Test (org.junit.Test)15 Errors (org.apache.kafka.common.protocol.Errors)13 Schema (org.apache.kafka.common.protocol.types.Schema)11 ByteBufferSend (org.apache.kafka.common.network.ByteBufferSend)8 LinkedHashMap (java.util.LinkedHashMap)7 Field (org.apache.kafka.common.protocol.types.Field)7 ArrayOf (org.apache.kafka.common.protocol.types.ArrayOf)6 Send (org.apache.kafka.common.network.Send)5 Node (org.apache.kafka.common.Node)4 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)3 ResponseHeader (org.apache.kafka.common.requests.ResponseHeader)3 KafkaPrincipal (org.apache.kafka.common.security.auth.KafkaPrincipal)3 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)3