Search in sources :

Example 81 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka-streams-examples by confluentinc.

the class WordCountInteractiveQueriesExample method createStreams.

static KafkaStreams createStreams(final Properties streamsConfiguration) {
    final Serde<String> stringSerde = Serdes.String();
    StreamsBuilder builder = new StreamsBuilder();
    KStream<String, String> textLines = builder.stream(TEXT_LINES_TOPIC, Consumed.with(Serdes.String(), Serdes.String()));
    final KGroupedStream<String, String> groupedByWord = textLines.flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+"))).groupBy((key, word) -> word, Serialized.with(stringSerde, stringSerde));
    // Create a State Store for with the all time word count
    groupedByWord.count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("word-count").withValueSerde(Serdes.Long()));
    // Create a Windowed State Store that contains the word count for every
    // 1 minute
    groupedByWord.windowedBy(TimeWindows.of(60000)).count(Materialized.<String, Long, WindowStore<Bytes, byte[]>>as("windowed-word-count").withValueSerde(Serdes.Long()));
    return new KafkaStreams(builder.build(), streamsConfiguration);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Arrays(java.util.Arrays) Properties(java.util.Properties) KGroupedStream(org.apache.kafka.streams.kstream.KGroupedStream) Files(java.nio.file.Files) Serialized(org.apache.kafka.streams.kstream.Serialized) HostInfo(org.apache.kafka.streams.state.HostInfo) KStream(org.apache.kafka.streams.kstream.KStream) WindowStore(org.apache.kafka.streams.state.WindowStore) File(java.io.File) Bytes(org.apache.kafka.common.utils.Bytes) Consumed(org.apache.kafka.streams.Consumed) Serde(org.apache.kafka.common.serialization.Serde) TimeWindows(org.apache.kafka.streams.kstream.TimeWindows) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Materialized(org.apache.kafka.streams.kstream.Materialized) Serdes(org.apache.kafka.common.serialization.Serdes) KafkaStreams(org.apache.kafka.streams.KafkaStreams) WindowStore(org.apache.kafka.streams.state.WindowStore) KafkaStreams(org.apache.kafka.streams.KafkaStreams) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore)

Example 82 with Bytes

use of org.apache.kafka.common.utils.Bytes in project flink by apache.

the class KafkaContainerClient method readMessages.

public <T> List<T> readMessages(int expectedNumMessages, String groupId, String topic, Deserializer<T> valueDeserializer) throws Exception {
    Properties props = new Properties();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, container.getBootstrapServers());
    props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    final List<T> messages = Collections.synchronizedList(new ArrayList<>(expectedNumMessages));
    try (Consumer<Bytes, T> consumer = new KafkaConsumer<>(props, new BytesDeserializer(), valueDeserializer)) {
        waitUntilTopicAvailableThenAssign(topic, consumer, Duration.ofSeconds(60));
        // Keep polling until getting expected number of messages
        final Deadline deadline = Deadline.fromNow(Duration.ofSeconds(120));
        while (deadline.hasTimeLeft() && messages.size() < expectedNumMessages) {
            LOG.info("Waiting for messages. Received {}/{}.", messages.size(), expectedNumMessages);
            ConsumerRecords<Bytes, T> records = consumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord<Bytes, T> record : records) {
                messages.add(record.value());
            }
        }
        if (messages.size() != expectedNumMessages) {
            throw new IOException("Could not read expected number of messages.");
        }
        return messages;
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) BytesDeserializer(org.apache.kafka.common.serialization.BytesDeserializer) Deadline(org.apache.flink.api.common.time.Deadline) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) IOException(java.io.IOException) Properties(java.util.Properties)

Example 83 with Bytes

use of org.apache.kafka.common.utils.Bytes in project flink by apache.

the class KafkaContainerClient method sendMessages.

public <T> void sendMessages(String topic, Serializer<T> valueSerializer, T... messages) {
    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, container.getBootstrapServers());
    props.put(ProducerConfig.ACKS_CONFIG, "all");
    try (Producer<Bytes, T> producer = new KafkaProducer<>(props, new BytesSerializer(), valueSerializer)) {
        for (T message : messages) {
            producer.send(new ProducerRecord<>(topic, message));
        }
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Bytes(org.apache.kafka.common.utils.Bytes) BytesSerializer(org.apache.kafka.common.serialization.BytesSerializer) Properties(java.util.Properties)

Example 84 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class InMemoryTimeOrderedKeyValueBuffer method flush.

@Override
public void flush() {
    if (loggingEnabled) {
        // counting on this getting called before the record collector's flush
        for (final Bytes key : dirtyKeys) {
            final BufferKey bufferKey = index.get(key);
            if (bufferKey == null) {
                // The record was evicted from the buffer. Send a tombstone.
                logTombstone(key);
            } else {
                final BufferValue value = sortedMap.get(bufferKey);
                logValue(key, bufferKey, value);
            }
        }
        dirtyKeys.clear();
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes)

Example 85 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class InMemoryTimeOrderedKeyValueBuffer method restoreBatch.

private void restoreBatch(final Collection<ConsumerRecord<byte[], byte[]>> batch) {
    for (final ConsumerRecord<byte[], byte[]> record : batch) {
        if (record.partition() != partition) {
            throw new IllegalStateException(String.format("record partition [%d] is being restored by the wrong suppress partition [%d]", record.partition(), partition));
        }
        final Bytes key = Bytes.wrap(record.key());
        if (record.value() == null) {
            // This was a tombstone. Delete the record.
            final BufferKey bufferKey = index.remove(key);
            if (bufferKey != null) {
                final BufferValue removed = sortedMap.remove(bufferKey);
                if (removed != null) {
                    memBufferSize -= computeRecordSize(bufferKey.key(), removed);
                }
                if (bufferKey.time() == minTimestamp) {
                    minTimestamp = sortedMap.isEmpty() ? Long.MAX_VALUE : sortedMap.firstKey().time();
                }
            }
        } else {
            final Header versionHeader = record.headers().lastHeader("v");
            if (versionHeader == null) {
                // Version 0:
                // value:
                // - buffer time
                // - old value
                // - new value
                final byte[] previousBufferedValue = index.containsKey(key) ? internalPriorValueForBuffered(key) : null;
                final DeserializationResult deserializationResult = deserializeV0(record, key, previousBufferedValue);
                cleanPut(deserializationResult.time(), deserializationResult.key(), deserializationResult.bufferValue());
            } else if (Arrays.equals(versionHeader.value(), V_3_CHANGELOG_HEADER_VALUE)) {
                // Version 3:
                // value:
                // - record context
                // - prior value
                // - old value
                // - new value
                // - buffer time
                final DeserializationResult deserializationResult = deserializeV3(record, key);
                cleanPut(deserializationResult.time(), deserializationResult.key(), deserializationResult.bufferValue());
            } else if (Arrays.equals(versionHeader.value(), V_2_CHANGELOG_HEADER_VALUE)) {
                // Version 2:
                // value:
                // - record context
                // - old value
                // - new value
                // - prior value
                // - buffer time
                // NOTE: 2.4.0, 2.4.1, and 2.5.0 actually encode Version 3 formatted data,
                // but still set the Version 2 flag, so to deserialize, we have to duck type.
                final DeserializationResult deserializationResult = duckTypeV2(record, key);
                cleanPut(deserializationResult.time(), deserializationResult.key(), deserializationResult.bufferValue());
            } else if (Arrays.equals(versionHeader.value(), V_1_CHANGELOG_HEADER_VALUE)) {
                // Version 1:
                // value:
                // - buffer time
                // - record context
                // - old value
                // - new value
                final byte[] previousBufferedValue = index.containsKey(key) ? internalPriorValueForBuffered(key) : null;
                final DeserializationResult deserializationResult = deserializeV1(record, key, previousBufferedValue);
                cleanPut(deserializationResult.time(), deserializationResult.key(), deserializationResult.bufferValue());
            } else {
                throw new IllegalArgumentException("Restoring apparently invalid changelog record: " + record);
            }
        }
    }
    updateBufferMetrics();
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) DeserializationResult(org.apache.kafka.streams.state.internals.TimeOrderedKeyValueBufferChangelogDeserializationHelper.DeserializationResult)

Aggregations

Bytes (org.apache.kafka.common.utils.Bytes)398 Test (org.junit.Test)309 Windowed (org.apache.kafka.streams.kstream.Windowed)84 KeyValue (org.apache.kafka.streams.KeyValue)68 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)53 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)50 Properties (java.util.Properties)49 ArrayList (java.util.ArrayList)42 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)38 StreamsConfig (org.apache.kafka.streams.StreamsConfig)35 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)35 Materialized (org.apache.kafka.streams.kstream.Materialized)33 Serdes (org.apache.kafka.common.serialization.Serdes)32 Metrics (org.apache.kafka.common.metrics.Metrics)29 KafkaStreams (org.apache.kafka.streams.KafkaStreams)28 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)27 Consumed (org.apache.kafka.streams.kstream.Consumed)25 KTable (org.apache.kafka.streams.kstream.KTable)23 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)22 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)21