在 JsonSerializer Hazelcast-client 上需要帮助
Need Help on JsonSerializer Hazelcast-client
我正在尝试使用谓词通过 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;
});
}
当我尝试使用谓词查询地图中的条目时出现异常,下面是我使用谓词查询地图的代码
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 服务器上配置类似的序列化程序,下面是例外情况
<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
中进行配置
-130 是为 Hazelcast Node.js 客户端保留的序列化 ID。 Hazelcast 服务器目前不支持本机 JSON 序列化,但计划在下一个版本中支持:https://www.infoq.com/news/2018/08/hazelcast-new-ceo
We are also planning on introducing new data structures, enhancing query with SQL Select and JDBC, and adding native JSON support for document-oriented systems. So more than ever to do and busy as ever.
作为解决方法,您可以对复杂对象使用 Portable
序列化。您需要在客户端和服务器端编写自己的序列化程序。然后,服务器应该能够查询您的对象。请记住,您需要为序列化程序使用正序列化 ID。负 ID 保留给内部序列化器,不能被覆盖。
www.hazelcast.org 有一个很好的实现可移植序列化的示例。只需在 Java Member
和 Node.js
选项卡下查找 Portable Serializer
示例。
我正在尝试使用谓词通过 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; }); }
当我尝试使用谓词查询地图中的条目时出现异常,下面是我使用谓词查询地图的代码
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 服务器上配置类似的序列化程序,下面是例外情况
<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
中进行配置-130 是为 Hazelcast Node.js 客户端保留的序列化 ID。 Hazelcast 服务器目前不支持本机 JSON 序列化,但计划在下一个版本中支持:https://www.infoq.com/news/2018/08/hazelcast-new-ceo
We are also planning on introducing new data structures, enhancing query with SQL Select and JDBC, and adding native JSON support for document-oriented systems. So more than ever to do and busy as ever.
作为解决方法,您可以对复杂对象使用 Portable
序列化。您需要在客户端和服务器端编写自己的序列化程序。然后,服务器应该能够查询您的对象。请记住,您需要为序列化程序使用正序列化 ID。负 ID 保留给内部序列化器,不能被覆盖。
www.hazelcast.org 有一个很好的实现可移植序列化的示例。只需在 Java Member
和 Node.js
选项卡下查找 Portable Serializer
示例。