gpt4 book ai didi

java - HazelCast:SqlPredicate 问题

转载 作者:行者123 更新时间:2023-11-30 07:37:32 25 4
gpt4 key购买 nike

我刚开始使用 HazelCast。我有一个键和值的映射,值是对象列表。我试图在 map 上使用 SqlPredicate 来过滤值。代码片段:

private void testHazelCast() {
final Employee e1 = new Employee("A", 20, 30);
final Employee e2 = new Employee("C", 25, 45);
final Employee e3 = new Employee("B", 30, 35);
final Employee e4 = new Employee("F", 35, 30);
final Employee e5 = new Employee("E", 40, 40);
final Employee e6 = new Employee(null, 40, 20);
final Employee e7 = new Employee("F", 60, 55);

List<Employee> employeeList_1 = new ArrayList<Employee>() {{add(e1);add(e2);}};
List<Employee> employeeList_2 = new ArrayList<Employee>() {{add(e3);add(e4);add(e7);}};
List<Employee> employeeList_3 = new ArrayList<Employee>() {{add(e5);}};
List<Employee> employeeList_4 = new ArrayList<Employee>() {{add(e6);}};
IMap<Integer, List<Employee>> map = hazelcast.getMap("employee");
map.put(1, employeeList_1);
map.put(2, employeeList_2);
map.put(3, employeeList_3);
map.put(4, employeeList_4);

// EntryObject e = new PredicateBuilder().getEntryObject();
// Predicate predicate_1 = e.get("name").equal("A");
Predicate predicate = new SqlPredicate("name = A");
Set<List<Employee>> employeeSet = (Set<List<Employee>>) map.values(predicate_1);
}

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 String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Name: " + name + ", ");
sb.append("Age: " + age + ", ");
sb.append("Weight: " + weight);
return sb.toString();
}

@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.util.ArrayList'

你能告诉我我到底做错了什么吗?我知道它试图在 List 类中找到“name”元素。无论如何,我可以使用 SqlPredicate 来过滤值吗?

谢谢,拉胡尔

最佳答案

对映射的每个值执行谓词。在您的示例中,您的 map 具有 Employee 集合作为值,因此谓词将在此集合上执行。

您的谓词评估为“名称 = A 的集合”:这是无效的,因为集合没有名称。

在 Hazelcast 3.6 中,您有“自定义属性”和 ValueExtractor 的概念。您可以:

  1. 创建一个类,该类拥有员工集合
  2. 将此类链接到 ValueExtractor,该 ValueExtractor 收集自定义属性“name”中每个员工的姓名

参见:http://docs.hazelcast.org/docs/3.6/manual/html-single/index.html#custom-attributes

此外,拥有集合 map 并不是很有效。

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

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