HazelCast:Sql谓词问题



我是使用HazelCast的新手。我有一个键和值的映射,值是对象列表。我正在尝试在地图上使用 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;
    }
}    

在执行上述代码时,我收到异常:

原因:java.lang.IllegalArgumentException:在类"java.util.ArrayList"上没有合适的"name"访问器

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

谢谢拉胡尔

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

谓词的计算结果为"名称 = A 的集合":这是无效的,因为集合没有名称。

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

  1. 创建一个类,该类拥有一组员工
  2. 将此类链接到 ValueExtractor,该提取器在自定义属性"name"中收集每个雇员的姓名

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

此外,拥有收藏地图的效率不是很高。

最新更新