Java Code Examples for org.apache.kafka.common.serialization.Serdes#ByteArray

The following examples show how to use org.apache.kafka.common.serialization.Serdes#ByteArray . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KafkaTable.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(Map<String, ?> operand) {
    super.configure(operand);
    if (getRowType() == null) {
        throw new IllegalStateException("Custom tables not yet supported for Kafka");
    }
    Map<String, Object> configs = new HashMap<>(operand);
    String groupId = (String) configs.getOrDefault(KafkaCacheConfig.KAFKACACHE_GROUP_ID_CONFIG, "kareldb-1");
    int epoch = (Integer) configs.get("epoch");
    org.apache.avro.Schema avroSchema = (org.apache.avro.Schema) configs.get("avroSchema");
    Pair<org.apache.avro.Schema, org.apache.avro.Schema> schemas = getKeyValueSchemas(avroSchema);
    String topic = getName() + "_" + epoch;
    configs.put(KafkaCacheConfig.KAFKACACHE_TOPIC_CONFIG, topic);
    configs.put(KafkaCacheConfig.KAFKACACHE_GROUP_ID_CONFIG, groupId);
    configs.put(KafkaCacheConfig.KAFKACACHE_CLIENT_ID_CONFIG, groupId + "-" + topic);
    String enableRocksDbStr = (String) configs.getOrDefault(KarelDbConfig.ROCKS_DB_ENABLE_CONFIG, "true");
    boolean enableRocksDb = Boolean.parseBoolean(enableRocksDbStr);
    String rootDir = (String) configs.getOrDefault(
        KarelDbConfig.ROCKS_DB_ROOT_DIR_CONFIG, KarelDbConfig.ROCKS_DB_ROOT_DIR_DEFAULT);
    Comparator<byte[]> cmp = new AvroKeyComparator(schemas.left);
    Cache<byte[], byte[]> cache = enableRocksDb
        ? new RocksDBCache<>(topic, "rocksdb", rootDir, Serdes.ByteArray(), Serdes.ByteArray(), cmp)
        : new InMemoryCache<>(cmp);
    this.rows = new KafkaCache<>(
        new KafkaCacheConfig(configs), Serdes.ByteArray(), Serdes.ByteArray(), null, cache);
}
 
Example 2
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Provide the {@link Serde} for inbound value.
 * @param consumerProperties {@link ConsumerProperties} on binding
 * @param extendedConsumerProperties binding level extended
 * {@link KafkaStreamsConsumerProperties}
 * @return configurd {@link Serde} for the inbound value.
 */
public Serde<?> getInboundValueSerde(ConsumerProperties consumerProperties,
		KafkaStreamsConsumerProperties extendedConsumerProperties) {
	Serde<?> valueSerde;

	String valueSerdeString = extendedConsumerProperties.getValueSerde();
	try {
		if (consumerProperties != null && consumerProperties.isUseNativeDecoding()) {
			valueSerde = getValueSerde(valueSerdeString);
		}
		else {
			valueSerde = Serdes.ByteArray();
		}
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return valueSerde;
}
 
Example 3
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
public Serde<?> getInboundValueSerde(ConsumerProperties consumerProperties,
									KafkaStreamsConsumerProperties extendedConsumerProperties,
									ResolvableType resolvableType) {
	Serde<?> valueSerde;

	String valueSerdeString = extendedConsumerProperties.getValueSerde();
	try {
		if (consumerProperties != null && consumerProperties.isUseNativeDecoding()) {
			valueSerde = getValueSerde(valueSerdeString, resolvableType);
		}
		else {
			valueSerde = Serdes.ByteArray();
		}
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return valueSerde;
}
 
Example 4
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Provide the {@link Serde} for outbound value.
 * @param producerProperties {@link ProducerProperties} on binding
 * @param kafkaStreamsProducerProperties binding level extended
 * {@link KafkaStreamsProducerProperties}
 * @return configurd {@link Serde} for the outbound value.
 */
public Serde<?> getOutboundValueSerde(ProducerProperties producerProperties,
		KafkaStreamsProducerProperties kafkaStreamsProducerProperties) {
	Serde<?> valueSerde;
	try {
		if (producerProperties.isUseNativeEncoding()) {
			valueSerde = getValueSerde(
					kafkaStreamsProducerProperties.getValueSerde());
		}
		else {
			valueSerde = Serdes.ByteArray();
		}
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return valueSerde;
}
 
Example 5
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
public Serde<?> getOutboundValueSerde(ProducerProperties producerProperties,
									KafkaStreamsProducerProperties kafkaStreamsProducerProperties, ResolvableType resolvableType) {
	Serde<?> valueSerde;
	try {
		if (producerProperties.isUseNativeEncoding()) {
			valueSerde = getValueSerde(
					kafkaStreamsProducerProperties.getValueSerde(), resolvableType);
		}
		else {
			valueSerde = Serdes.ByteArray();
		}
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return valueSerde;
}
 
Example 6
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private Serde<?> getKeySerde(String keySerdeString, ResolvableType resolvableType) {
	Serde<?> keySerde = null;
	try {
		if (StringUtils.hasText(keySerdeString)) {
			keySerde = Utils.newInstance(keySerdeString, Serde.class);
		}
		else {
			if (resolvableType != null &&
					(isResolvalbeKafkaStreamsType(resolvableType) || isResolvableKStreamArrayType(resolvableType))) {
				ResolvableType generic = resolvableType.isArray() ? resolvableType.getComponentType().getGeneric(0) : resolvableType.getGeneric(0);
				Serde<?> fallbackSerde = getFallbackSerde("default.key.serde");
				keySerde = getSerde(generic, fallbackSerde);
			}
			if (keySerde == null) {
				keySerde = Serdes.ByteArray();
			}
		}
		keySerde.configure(this.streamConfigGlobalProperties, true);
	}
	catch (ClassNotFoundException ex) {
		throw new IllegalStateException("Serde class not found: ", ex);
	}
	return keySerde;
}
 
Example 7
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Serde<?> getValueSerde(String valueSerdeString, ResolvableType resolvableType)
		throws ClassNotFoundException {
	Serde<?> valueSerde = null;
	if (StringUtils.hasText(valueSerdeString)) {
		valueSerde = Utils.newInstance(valueSerdeString, Serde.class);
	}
	else {

		if (resolvableType != null && ((isResolvalbeKafkaStreamsType(resolvableType)) ||
				(isResolvableKStreamArrayType(resolvableType)))) {
			Serde<?> fallbackSerde = getFallbackSerde("default.value.serde");
			ResolvableType generic = resolvableType.isArray() ? resolvableType.getComponentType().getGeneric(1) : resolvableType.getGeneric(1);
			valueSerde = getSerde(generic, fallbackSerde);
		}
		if (valueSerde == null) {

			valueSerde = Serdes.ByteArray();
		}
	}
	valueSerde.configure(streamConfigGlobalProperties, false);
	return valueSerde;
}
 
Example 8
Source File: KeyValueSerdeResolver.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private Serde<?> getFallbackSerde(String s) throws ClassNotFoundException {
	return this.binderConfigurationProperties.getConfiguration()
			.containsKey(s)
			? Utils.newInstance(this.binderConfigurationProperties
					.getConfiguration().get(s),
			Serde.class)
			: Serdes.ByteArray();
}
 
Example 9
Source File: KStreamBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Binding<KStream<Object, Object>> doBindProducer(String name,
		KStream<Object, Object> outboundBindTarget,
		ExtendedProducerProperties<KafkaStreamsProducerProperties> properties) {

	ExtendedProducerProperties<KafkaProducerProperties> extendedProducerProperties =
			(ExtendedProducerProperties) properties;

	this.kafkaTopicProvisioner.provisionProducerDestination(name, extendedProducerProperties);
	Serde<?> keySerde = this.keyValueSerdeResolver
			.getOuboundKeySerde(properties.getExtension(), kafkaStreamsBindingInformationCatalogue.getOutboundKStreamResolvable());
	LOG.info("Key Serde used for (outbound) " + name + ": " + keySerde.getClass().getName());

	Serde<?> valueSerde;
	if (properties.isUseNativeEncoding()) {
		valueSerde = this.keyValueSerdeResolver.getOutboundValueSerde(properties,
				properties.getExtension(), kafkaStreamsBindingInformationCatalogue.getOutboundKStreamResolvable());
	}
	else {
		valueSerde = Serdes.ByteArray();
	}
	LOG.info("Value Serde used for (outbound) " + name + ": " + valueSerde.getClass().getName());

	to(properties.isUseNativeEncoding(), name, outboundBindTarget,
			(Serde<Object>) keySerde, (Serde<Object>) valueSerde, properties.getExtension());
	return new DefaultBinding<>(name, null, outboundBindTarget, null);
}
 
Example 10
Source File: AbstractKafkaStreamsBinderProcessor.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
protected Serde<?> getValueSerde(String inboundName, KafkaStreamsConsumerProperties kafkaStreamsConsumerProperties, ResolvableType resolvableType) {
	if (bindingServiceProperties.getConsumerProperties(inboundName).isUseNativeDecoding()) {
		BindingProperties bindingProperties = this.bindingServiceProperties
				.getBindingProperties(inboundName);
		return this.keyValueSerdeResolver.getInboundValueSerde(
				bindingProperties.getConsumer(), kafkaStreamsConsumerProperties, resolvableType);
	}
	else {
		return Serdes.ByteArray();
	}
}
 
Example 11
Source File: RocksDBCacheTest.java    From kcache with Apache License 2.0 4 votes vote down vote up
RocksDBCache<Bytes, byte[]> getRocksDBCache() {
    return new RocksDBCache<>(DB_NAME, dir.getAbsolutePath(), Serdes.Bytes(), Serdes.ByteArray());
}