Search in sources :

Example 11 with KeyValue

use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by yahoo.

the class MessageImpl method getKeyValueBySchemaVersion.

private T getKeyValueBySchemaVersion() {
    KeyValueSchemaImpl kvSchema = getKeyValueSchema();
    byte[] schemaVersion = getSchemaVersion();
    if (kvSchema.getKeyValueEncodingType() == KeyValueEncodingType.SEPARATED) {
        org.apache.pulsar.common.schema.KeyValue keyValue = (org.apache.pulsar.common.schema.KeyValue) kvSchema.decode(getKeyBytes(), getData(), schemaVersion);
        if (schema instanceof AutoConsumeSchema) {
            return (T) AutoConsumeSchema.wrapPrimitiveObject(keyValue, ((AutoConsumeSchema) schema).getSchemaInfo(schemaVersion).getType(), schemaVersion);
        } else {
            return (T) keyValue;
        }
    } else {
        return decode(schemaVersion);
    }
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) KeyValueSchemaImpl(org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl)

Example 12 with KeyValue

use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by yahoo.

the class TopicsImpl method getIndividualMsgsFromBatch.

private List<Message<byte[]>> getIndividualMsgsFromBatch(String topic, String msgId, byte[] data, Map<String, String> properties, MessageMetadata msgMetadataBuilder, BrokerEntryMetadata brokerEntryMetadata) {
    List<Message<byte[]>> ret = new ArrayList<>();
    int batchSize = Integer.parseInt(properties.get(BATCH_HEADER));
    ByteBuf buf = Unpooled.wrappedBuffer(data);
    for (int i = 0; i < batchSize; i++) {
        String batchMsgId = msgId + ":" + i;
        SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
        try {
            ByteBuf singleMessagePayload = Commands.deSerializeSingleMessageInBatch(buf, singleMessageMetadata, i, batchSize);
            if (singleMessageMetadata.getPropertiesCount() > 0) {
                for (KeyValue entry : singleMessageMetadata.getPropertiesList()) {
                    properties.put(entry.getKey(), entry.getValue());
                }
            }
            MessageImpl message = new MessageImpl<>(topic, batchMsgId, properties, singleMessagePayload, Schema.BYTES, msgMetadataBuilder);
            if (brokerEntryMetadata != null) {
                message.setBrokerEntryMetadata(brokerEntryMetadata);
            }
            ret.add(message);
        } catch (Exception ex) {
            log.error("Exception occurred while trying to get BatchMsgId: {}", batchMsgId, ex);
        }
    }
    buf.release();
    return ret;
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) Message(org.apache.pulsar.client.api.Message) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException)

Example 13 with KeyValue

use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by yahoo.

the class EntryFilter2Test method filterEntry.

@Override
public FilterResult filterEntry(Entry entry, FilterContext context) {
    if (context.getMsgMetadata() == null || context.getMsgMetadata().getPropertiesCount() <= 0) {
        return FilterResult.ACCEPT;
    }
    List<KeyValue> list = context.getMsgMetadata().getPropertiesList();
    // filter by subscription properties
    PersistentSubscription subscription = (PersistentSubscription) context.getSubscription();
    if (!MapUtils.isEmpty(subscription.getSubscriptionProperties())) {
        for (KeyValue keyValue : list) {
            if (subscription.getSubscriptionProperties().containsKey(keyValue.getKey())) {
                return FilterResult.ACCEPT;
            }
        }
    }
    return FilterResult.REJECT;
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription)

Example 14 with KeyValue

use of org.apache.pulsar.common.api.proto.KeyValue in project pulsar by yahoo.

the class EntryFilterTest method filterEntry.

@Override
public FilterResult filterEntry(Entry entry, FilterContext context) {
    if (context.getMsgMetadata() == null || context.getMsgMetadata().getPropertiesCount() <= 0) {
        return FilterResult.ACCEPT;
    }
    Consumer consumer = context.getConsumer();
    Map<String, String> metadata = consumer.getMetadata();
    log.info("filterEntry for {}", metadata);
    String matchValueAccept = metadata.getOrDefault("matchValueAccept", "ACCEPT");
    String matchValueReject = metadata.getOrDefault("matchValueReject", "REJECT");
    String matchValueReschedule = metadata.getOrDefault("matchValueReschedule", "RESCHEDULE");
    List<KeyValue> list = context.getMsgMetadata().getPropertiesList();
    // filter by string
    for (KeyValue keyValue : list) {
        if (matchValueAccept.equalsIgnoreCase(keyValue.getKey())) {
            log.info("metadata {} key {} outcome ACCEPT", metadata, keyValue.getKey());
            return FilterResult.ACCEPT;
        } else if (matchValueReject.equalsIgnoreCase(keyValue.getKey())) {
            log.info("metadata {} key {} outcome REJECT", metadata, keyValue.getKey());
            return FilterResult.REJECT;
        } else if (matchValueReschedule.equalsIgnoreCase(keyValue.getKey())) {
            log.info("metadata {} key {} outcome RESCHEDULE", metadata, keyValue.getKey());
            return FilterResult.RESCHEDULE;
        } else {
            log.info("metadata {} key {} outcome ??", metadata, keyValue.getKey());
        }
    }
    return null;
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) Consumer(org.apache.pulsar.broker.service.Consumer)

Example 15 with KeyValue

use of org.apache.pulsar.common.api.proto.KeyValue in project incubator-pulsar by apache.

the class SchemaInfoUtil method newSchemaInfo.

public static SchemaInfo newSchemaInfo(Schema schema) {
    SchemaInfoImpl.SchemaInfoImplBuilder si = SchemaInfoImpl.builder().name(schema.getName()).schema(schema.getSchemaData()).type(Commands.getSchemaType(schema.getType()));
    if (schema.getPropertiesCount() == 0) {
        si.properties(Collections.emptyMap());
    } else {
        Map<String, String> properties = new TreeMap<>();
        for (int i = 0; i < schema.getPropertiesCount(); i++) {
            KeyValue kv = schema.getPropertyAt(i);
            properties.put(kv.getKey(), kv.getValue());
        }
        si.properties(properties);
    }
    return si.build();
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) TreeMap(java.util.TreeMap)

Aggregations

KeyValue (org.apache.pulsar.common.api.proto.KeyValue)44 Set (java.util.Set)18 MessageImpl (org.apache.pulsar.client.impl.MessageImpl)18 ArrayList (java.util.ArrayList)15 CompletableFuture (java.util.concurrent.CompletableFuture)12 TopicMessageImpl (org.apache.pulsar.client.impl.TopicMessageImpl)12 Test (org.testng.annotations.Test)12 HashMap (java.util.HashMap)10 ByteBuf (io.netty.buffer.ByteBuf)9 List (java.util.List)9 Map (java.util.Map)9 TreeMap (java.util.TreeMap)9 TimeUnit (java.util.concurrent.TimeUnit)9 EncryptionKeys (org.apache.pulsar.common.api.proto.EncryptionKeys)9 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)6 Collections (java.util.Collections)6