我正在尝试使用谓词通过Hazelcast-client(Node Js)实现NoSql解决方案。
我正在创建如下用户地图:
function generateUsers(users) { return users.put('Rod', new User('Rod', 19, true)).then(function () { var usertemp={ username: "Jane", age: 20, active: true }; //return users.put('Jane', new User('Jane', 20, true)); return users.put('Jane', usertemp); }).then(function () { var usertemp={ username: "Freddy", age: 23, active: true }; return users.put('Freddy', usertemp); }).then(function(){ var usertemp={ username: "test", age: 20, active: false }; //var usertemp2= new User('Test',20,true); users.put('test',usertemp); console.log("after put"); console.log("Userdata:"+users); return users; }); }
当我尝试使用谓词查询map中的条目时,它给出了例外,以下是我使用谓词查询Map的代码
var JsonSerializer = /** @class */ (function () { function JsonSerializer() { } JsonSerializer.prototype.getId = function () { return 1; }; JsonSerializer.prototype.read = function (input) { return JSON.parse(input.readUTF()); }; JsonSerializer.prototype.write = function (output, object) { output.writeUTF(JSON.stringify(object)); }; return JsonSerializer; }()); var cfg = new Config.ClientConfig(); cfg.serializationConfig.customSerializers =[new JsonSerializer(),]; cfg.serializationConfig.portableFactories[1] = new PortableFactory(); // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1 Client.newHazelcastClient(cfg).then(function (hz) { // Get a Distributed Map called "users" var users = hz.getMap('users'); // Add some users to the Distributed Map return generateUsers(users).then(function () { // Create a Predicate var criteriaQuery = Predicates.and( Predicates.truePredicate('active', true), Predicates.isBetween('age', 18, 21) ); // Get result collections using the the Predicate console.log("before valuesWithPredicate"); return users.valuesWithPredicate(criteriaQuery); }).then(function (values) { // Print out the results console.log(values.toArray()); // Shutdown this Hazelcast Client hz.shutdown(); }) });
以下是例外:
Unhandled rejection Error: There is no suitable de-serializer for type -130. This exception is likely to be caused by differences in the serialization configuration between members or between clients and members.
> NOTE: I've Tried adding customSerializer JsonSerializer in HazelcastClient config
Edit1:我错过了hazlecast服务器控制台,似乎我们需要在hazlecast服务器上配置类似的Serializer,以下是例外情况
<pre>
SEVERE: [10.8.162.33]:5701 [dev] [3.10.5] There is no suitable de-serializer for type -130. This exception is likely to be caused by differences in the serialization configuration between members or between clients and members.
com.hazelcast.nio.serialization.HazelcastSerializationException: There is no suitable de-serializer for type -130. This exception is likely to be caused by differences in the serialization configuration between members or between clients and members.
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.newHazelcastSerializationException(AbstractSerializationService.java:238)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:182)
at com.hazelcast.query.impl.CachedQueryEntry.getValue(CachedQueryEntry.java:75)
at com.hazelcast.query.impl.CachedQueryEntry.getTargetObject(CachedQueryEntry.java:108)
at com.hazelcast.query.impl.QueryableEntry.extractAttributeValue(QueryableEntry.java:81)
at com.hazelcast.query.impl.QueryableEntry.getAttributeValue(QueryableEntry.java:48)
at com.hazelcast.query.impl.predicates.AbstractPredicate.readAttributeValue(AbstractPredicate.java:132)
at com.hazelcast.query.impl.predicates.AbstractPredicate.apply(AbstractPredicate.java:57)
at com.hazelcast.query.impl.predicates.AndPredicate.apply(AndPredicate.java:136)
at com.hazelcast.map.impl.query.PartitionScanRunner.run(PartitionScanRunner.java:97)
at com.hazelcast.map.impl.query.CallerRunsPartitionScanExecutor.execute(CallerRunsPartitionScanExecutor.java:42)
at com.hazelcast.map.impl.query.QueryRunner.runPartitionScanQueryOnGivenOwnedPartition(QueryRunner.java:172)
at com.hazelcast.map.impl.query.QueryPartitionOperation.run(QueryPartitionOperation.java:45)
at com.hazelcast.spi.Operation.call(Operation.java:148)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.call(OperationRunnerImpl.java:202)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:191)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:120)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.run(OperationThread.java:100)
但是不幸的是,我无法在hazlecast.xml中在服务器端找到类似的JSONSerializer进行配置
答案 0 :(得分:0)
-130是为Hazelcast Node.js客户端保留的序列化ID。 Hazelcast服务器目前不支持本机JSON序列化,但计划在下一个版本中使用:https://www.infoq.com/news/2018/08/hazelcast-new-ceo
我们还计划引入新的数据结构,使用SQL Select和JDBC增强查询,并为面向文档的系统添加本机JSON支持。因此比以往任何时候都更加忙碌。
作为解决方法,可以对复杂对象使用Portable
序列化。您需要在客户端和服务器端都编写自己的序列化器。然后,服务器应该能够查询您的对象。请记住,您需要为序列化程序使用正序列化ID。负ID保留给内部序列化器使用,不能被覆盖。
www.hazelcast.org有一个很好的示例,用于实现可移植序列化。只需在Portable Serializer
和Java Member
标签下查找Node.js
示例。