要解决“Apache Ignite生成了集群的键,但没有键类”的问题,你可以按照以下步骤进行操作:
Serializable
接口,并且具有适当的hashCode()
和equals()
方法。下面是一个示例键类的代码:
import java.io.Serializable;
public class MyKey implements Serializable {
private int id;
public MyKey(int id) {
this.id = id;
}
// Getters and setters
@Override
public int hashCode() {
return id;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MyKey other = (MyKey) obj;
return id == other.id;
}
}
IgniteCache#withKeepBinary()
方法打开二进制模式,然后使用BinaryObjectBuilder
类来创建二进制键对象。下面是一个示例代码:
IgniteCache cache = ignite.getOrCreateCache("myCache");
BinaryObjectBuilder keyBuilder = ignite.binary().builder("MyKeyTypeName");
keyBuilder.setField("id", 123);
BinaryObject binaryKey = keyBuilder.build();
cache.withKeepBinary().put(binaryKey, "value");
在上面的示例中,我们使用MyKey
类的实例binaryKey
作为键,并将字符串"value"作为值存储在缓存中。
这样,你就可以解决“Apache Ignite生成了集群的键,但没有键类”的问题。