处理空值的Hazelcast投影

huangapple go评论82阅读模式
英文:

Handling null values Hazelcast Projections

问题

处理Hazelcast投影中的空值

HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<Integer, HazelcastJsonValue> map = hz.getMap("myMap");

map.set(0, new HazelcastJsonValue("{\"id\":\"01\",\"name\":\"abc\",\"age\":null}"));
map.set(1, new HazelcastJsonValue("{\"id\":\"02\",\"name\":\"data\",\"age\":37}"));
map.set(2, new HazelcastJsonValue("{\"id\":\"03\",\"name\":\"abc\",\"age\":39}"));

Collection<Object[]> projection = map.project(Projections.multiAttribute("id", "name", "age"));

在上面的代码中,您可以观察到第一个条目的age字段为null,因此Projections会抛出异常。如何在抛出异常之前返回null值...

英文:

How to handle null values in the hazelcast projections

HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap&lt;Integer, HazelcastJsonValue&gt; map = hz.getMap(&quot;myMap&quot;);

map.set(0, new HazelcastJsonValue(&quot;{\&quot;id\&quot;:\&quot;01\&quot;,\&quot;name\&quot;:\&quot;abc\&quot;,\&quot;age\&quot;:null}&quot;));
map.set(1, new HazelcastJsonValue(&quot;{\&quot;id\&quot;:\&quot;02\&quot;,\&quot;name\&quot;:\&quot; data\&quot;,\&quot;age\&quot;:37} &quot;));
map.set(2, new HazelcastJsonValue(&quot;{\&quot;id\&quot;:\&quot;03\&quot;,\&quot;name\&quot;:\&quot;abc\&quot;,\&quot;age\&quot;:39}&quot;));

Collection&lt;Object[]&gt; projection = map.project(Projections.multiAttribute(&quot;id&quot;, &quot;name&quot;,&quot;age&quot;));

In the above code you can observe that age field is null for the first entry so Projections is throwing exception.
How to just return the null value inside of throwing exception...

答案1

得分: 0

你使用的是哪个 Hazelcast 版本?

我尝试过 4.0.2 版本。我已经添加了以下代码片段来打印这些元素:

projection.stream()
          .flatMap(Stream::of)
          .forEach(System.out::println);

它输出如下:

03
abc
39
01
abc
null
02
 data
37
英文:

Which Hazelcast version are you using?

I've tried with 4.0.2. I've added the following snippet to print the elements:

projection.stream()
          .flatMap(Stream::of)
          .forEach(System.out::println);

It outputs the following:

03
abc
39
01
abc
null
02
 data
37

huangapple
  • 本文由 发表于 2020年9月9日 04:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63801356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定