gpt4 book ai didi

java - Hazelcast:SqlPredicate 面临 IllegalArgumentException

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:30 28 4
gpt4 key购买 nike

我正在尝试使用 Hazelcast 来使用 SqlPredicate。代码片段:

private void testHazelCast() {
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
Employee e1 = new Employee("A", 20, 30);
Employee e2 = new Employee("C", 25, 45);
Employee e3 = new Employee("B", 30, 35);
Employee e4 = new Employee("F", 35, 30);
Employee e5 = new Employee("E", 40, 40);
Employee e6 = new Employee(null, 40, 20);
IMap<Employee, String> map = hazelcast.getMap("employee");
map.put(e1, "e1");
map.put(e2, "e2");
map.put(e3, "e3");
map.put(e4, "e4");
map.put(e5, "e5");
map.put(e6, "e6");

EntryObject e = new PredicateBuilder().getEntryObject();
Predicate predicate = new SqlPredicate(String.format("name = A"));
Set<Employee> employeeSet = map.keySet(predicate);
System.out.println(employeeSet);
}

class Employee implements Serializable{
String name;
int age;
int weight;

public Employee(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Employee employee = (Employee) o;

if (age != employee.age) return false;
if (weight != employee.weight) return false;
if (!name.equals(employee.name)) return false;

return true;
}

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + age;
result = 31 * result + weight;
return result;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

}

当我运行此程序时,我收到以下执行:

Caused by: java.lang.IllegalArgumentException: There is no suitable accessor for 'name' on class 'class java.lang.String' at com.hazelcast.query.impl.ReflectionHelper.createGetter(ReflectionHelper.java:150) ... 14 more

我需要使用 SqlPredicate 过滤键,然后使用键检索值。我已经使用 PredicateBuilder 实现了这一点。

Predicate predicate = e.key().get("name").equal("A");

我尝试使用 SqlPredicate 执行相同的 Activity 。谁能指出我在 SqlPredicate 上到底做错了什么?

谢谢,拉胡尔

最佳答案

您应该将谓词应用于映射的值,因此请尝试将 IMap 与 String 键和 Employee 值一起使用

关于java - Hazelcast:SqlPredicate 面临 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35154051/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com