当前位置: 首页>>代码示例>>Java>>正文


Java KStream.map方法代码示例

本文整理汇总了Java中org.apache.kafka.streams.kstream.KStream.map方法的典型用法代码示例。如果您正苦于以下问题:Java KStream.map方法的具体用法?Java KStream.map怎么用?Java KStream.map使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.kafka.streams.kstream.KStream的用法示例。


在下文中一共展示了KStream.map方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createInput

import org.apache.kafka.streams.kstream.KStream; //导入方法依赖的package包/类
@Override
public KStream createInput(String name) {
	KStream<Object, Object> stream = kStreamBuilder.stream(bindingServiceProperties.getBindingDestination(name));
	stream = stream.map((key, value) -> {
		KeyValue<Object, Object> keyValue;
		BindingProperties bindingProperties = bindingServiceProperties.getBindingProperties(name);
		String contentType = bindingProperties.getContentType();
		if (!StringUtils.isEmpty(contentType)) {
			Message<Object> message = MessageBuilder.withPayload(value)
					.setHeader(MessageHeaders.CONTENT_TYPE, contentType).build();
			keyValue = new KeyValue<>(key, message);
		}
		else {
			keyValue = new KeyValue<>(key, value);
		}
		return keyValue;
	});
	return stream;
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-stream-binder-kafka,代码行数:20,代码来源:KStreamBoundElementFactory.java

示例2: wrap

import org.apache.kafka.streams.kstream.KStream; //导入方法依赖的package包/类
public void wrap(KStream<Object, Object> delegate) {
	Assert.notNull(delegate, "delegate cannot be null");
	Assert.isNull(this.delegate, "delegate already set to " + this.delegate);
	ProducerProperties producer = bindingServiceProperties.getBindingProperties(name).getProducer();

	if (messageConverter != null && !producer.isUseNativeEncoding()) {
		KeyValueMapper<Object, Object, KeyValue<Object, Object>> keyValueMapper = (k, v) -> {
			Message<?> message = (Message<?>) v;
			BindingProperties bindingProperties = bindingServiceProperties.getBindingProperties(name);
			String contentType = bindingProperties.getContentType();
			Map<String, Object> headers = new HashMap<>(((Message<?>) v).getHeaders());
			if (!StringUtils.isEmpty(contentType)) {
				headers.put(MessageHeaders.CONTENT_TYPE, contentType);
			}
			MessageHeaders messageHeaders = new MessageHeaders(headers);
			return new KeyValue<>(k,
					messageConverter.toMessage(message.getPayload(),
							messageHeaders));
		};
		delegate = delegate.map(keyValueMapper);
	}
	this.delegate = delegate;
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-stream-binder-kafka,代码行数:24,代码来源:KStreamBoundElementFactory.java

示例3: adapt

import org.apache.kafka.streams.kstream.KStream; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public KStream adapt(KStream<?, ?> bindingTarget, MethodParameter parameter) {
	ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
	final Class<?> valueClass = (resolvableType.getGeneric(1).getRawClass() != null)
			? (resolvableType.getGeneric(1).getRawClass()) : Object.class;

	return bindingTarget.map((KeyValueMapper) (o, o2) -> {
		KeyValue<Object, Object> keyValue;
		if (valueClass.isAssignableFrom(o2.getClass())) {
			keyValue =  new KeyValue<>(o, o2);
		}
		else if (o2 instanceof Message) {
			if (valueClass.isAssignableFrom(((Message) o2).getPayload().getClass())) {
				keyValue = new KeyValue<>(o, ((Message) o2).getPayload());
			}
			else {
				keyValue = new KeyValue<>(o, messageConverter.fromMessage((Message) o2, valueClass));
			}
		}
		else if(o2 instanceof String || o2 instanceof byte[]) {
			Message<Object> message = MessageBuilder.withPayload(o2).build();
			keyValue =  new KeyValue<>(o, messageConverter.fromMessage(message, valueClass));
		}
		else {
			keyValue =  new KeyValue<>(o, o2);
		}
		return keyValue;
	});
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-stream-binder-kafka,代码行数:31,代码来源:KStreamListenerParameterAdapter.java

示例4: start

import org.apache.kafka.streams.kstream.KStream; //导入方法依赖的package包/类
public void start() {
    KStreamBuilder builder = new KStreamBuilder();

    Serde<UUID> keySerde = new FressianSerde();
    Serde<Map> valSerde = new FressianSerde();

    KStream<UUID, Map> commands = builder.stream(keySerde, valSerde, commandsTopic);
    KStream<UUID, Map> customerEvents = commands
            .filter((id, command) -> command.get(new Keyword("action")).equals(new Keyword("create-customer")))
            .map((id, command) -> {
                logger.debug("Command received");
                Map userEvent = new HashMap(command);
                userEvent.put(new Keyword("action"), new Keyword("customer-created"));
                userEvent.put(new Keyword("parent"), id);
                Map userValue = (Map) userEvent.get(new Keyword("data"));
                userValue.put(new Keyword("id"), UUID.randomUUID());
                return new KeyValue<>(UUID.randomUUID(), userEvent);
    }).through(keySerde, valSerde, eventsTopic);

    KStream<UUID, Map> customers = customerEvents
            .map((id, event) -> {
                Map customer = (Map) event.get(new Keyword("data"));
                UUID customerId = (UUID) customer.get(new Keyword("id"));
                return new KeyValue<UUID, Map>(customerId, customer);
            });

    customers.through(keySerde, valSerde, customersTopic);

    StateStoreSupplier store = Stores.create("Customers")
            .withKeys(keySerde)
            .withValues(valSerde)
            .persistent()
            .build();
    builder.addStateStore(store);

    customers.process(customerStore, "Customers");

    this.kafkaStreams = new KafkaStreams(builder, kafkaStreamsConfig);
    this.kafkaStreams.start();
}
 
开发者ID:capitalone,项目名称:cqrs-manager-for-distributed-reactive-services,代码行数:41,代码来源:CommandProcessor.java

示例5: doBindProducer

import org.apache.kafka.streams.kstream.KStream; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected Binding<KStream<Object, Object>> doBindProducer(String name, KStream<Object, Object> outboundBindTarget,
														ExtendedProducerProperties<KStreamProducerProperties> properties) {
	ExtendedProducerProperties<KafkaProducerProperties> extendedProducerProperties = new ExtendedProducerProperties<KafkaProducerProperties>(
			new KafkaProducerProperties());
	this.kafkaTopicProvisioner.provisionProducerDestination(name, extendedProducerProperties);
	outboundBindTarget = outboundBindTarget
			.map((k, v) -> KeyValue.pair(k, ((Message<Object>) v).getPayload()));

	Serde<?> keySerde = Serdes.ByteArray();
	Serde<?> valueSerde = Serdes.ByteArray();
	if (properties.isUseNativeEncoding()) {
		outboundBindTarget.to(name, Produced.with((Serde<Object>) keySerde, (Serde<Object>) valueSerde));
	}
	else {
		try {
			if (StringUtils.hasText(properties.getExtension().getKeySerde())) {
				keySerde = Utils.newInstance(properties.getExtension().getKeySerde(), Serde.class);
				if (keySerde instanceof Configurable) {
					((Configurable) keySerde).configure(streamsConfig.originals());
				}
			}
			else {
				keySerde = this.binderConfigurationProperties.getConfiguration().containsKey("key.serde") ?
						Utils.newInstance(this.binderConfigurationProperties.getConfiguration().get("key.serde"), Serde.class) : Serdes.ByteArray();
			}

			if (StringUtils.hasText(properties.getExtension().getValueSerde())) {
				valueSerde = Utils.newInstance(properties.getExtension().getValueSerde(), Serde.class);
				if (valueSerde instanceof Configurable) {
					((Configurable) valueSerde).configure(streamsConfig.originals());
				}
			}
			outboundBindTarget.to(name, Produced.with((Serde<Object>) keySerde, (Serde<Object>) valueSerde));
		} catch (ClassNotFoundException e) {
			throw new IllegalStateException("Serde class not found: ", e);
		}
	}
	return new DefaultBinding<>(name, null, outboundBindTarget, null);
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-stream-binder-kafka,代码行数:42,代码来源:KStreamBinder.java


注:本文中的org.apache.kafka.streams.kstream.KStream.map方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。