我正在尝试使用Predicates在Hazelcast-client(Node Js)上实现NoSql解决方案 .
我正在创建一个用户 Map ,如下所示:
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)
但不幸的是,我无法在服务器端找到类似的JSONSerializer来配置hazlecast.xml
1 回答
-130是为Hazelcast Node.js客户端保留的序列化ID . Hazelcast服务器目前不支持本机JSON序列化,但计划在下一版本中使用:https://www.infoq.com/news/2018/08/hazelcast-new-ceo
作为一种解决方法,您可以对复杂对象使用
Portable
序列化 . 您需要在客户端和服务器端编写自己的序列化程序 . 然后,服务器应该能够查询您的对象 . 请记住,您需要为序列化程序使用正序列化ID . 负id为内部序列化程序保留,不能被覆盖 .www.hazelcast.org有一个很好的实现便携式序列化的示例 . 只需在
Java Member
和Node.js
标签下查找Portable Serializer
样本 .