Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka Kafka streams中的映射操作后使用双引号的字符串_Apache Kafka_Apache Kafka Streams - Fatal编程技术网

Apache kafka Kafka streams中的映射操作后使用双引号的字符串

Apache kafka Kafka streams中的映射操作后使用双引号的字符串,apache-kafka,apache-kafka-streams,Apache Kafka,Apache Kafka Streams,我使用Kafka streams DSL和map将KStream转换为KStream 在ValueMapper函数中,我简单地返回newvaluemapper(“key”,“some constant string”),但在将值发送回Kafka useKStream的地方。对于(“some topic”),我得到的结果是添加了双引号 我的代码如下: KStream<String, JsonNode> views = builder.stream("fromTopic"); views

我使用Kafka streams DSL和map将
KStream
转换为
KStream

ValueMapper
函数中,我简单地返回
newvaluemapper(“key”,“some constant string”)
,但在将值发送回Kafka use
KStream的地方。对于(“some topic”)
,我得到的结果是添加了双引号

我的代码如下:

KStream<String, JsonNode> views = builder.stream("fromTopic");
views.map(new ValueKMapper()).to("toTopic");
然后当我使用
tototopic
时,我得到了
“hello”“
,并添加了引号


也许这是Kafka streams的一个bug?

我假设您在问题中提供的方法
apply()

public KeyValue<String, String> apply(String key, JsonNode value) 
{
    return new KeyValue<String,String>("a", "hello");
}
但是,
value.get(key)
将返回一个
TextNode
,而
toString()
方法将返回包含引号的
TextNode
的字符串表示形式。为了正确解析
JsonNode
,您需要使用
textValue()
方法,这样您的方法就可以

public KeyValue<String, String> apply(String key, JsonNode value) 
{
    return new KeyValue<String,String>(key, value.get(key).textValue());
}
将在

json.get("a").textValue();

将返回
hello

您可能使用Json serde作为默认值。写入接收器时尝试添加字符串序列:

.to("toTopic", Produced.with(Serdes.String(), Serdes.String()));

能否将所有代码显示为一个?
ValueMapper
是一个接口。。。您不能在此基础上调用
new
。你能分享代码片段吗?@MatthiasJ.Sax我只返回一个新的键值。不确定。您使用什么
Serde
?也许Serde不正确,把事情搞砸了?尝试使用(Serdes.String(),Serdes.String());
否(“outputOPC”,producted.with);Java不允许这样做。它被称为“类型擦除”--在运行时,泛型类型信息不再可用,因此无法根据泛型类型自动选择Serde。感谢您的回答,但为了避免一些可能的原因,我非常抱歉,我使用常量字符串创建了一个
KeyValue
,这与
JsonNode
I无关我通过将Serde.String()添加到KStream.to(“tototopic”,Serde.String(),Serde.String())解决了这个问题。
json.get("a").toString())
json.get("a").textValue();
.to("toTopic", Produced.with(Serdes.String(), Serdes.String()));