HazelCast:SqlPredicate问题

时间:2016-02-03 13:36:03

标签: java hazelcast

我是使用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:类'class java.util.ArrayList'上没有合适的'name'访问器

你能不能让我知道我到底做错了什么。我知道它正在尝试在List类中找到“name”元素。 反正我是否可以使用SqlPredicate来过滤值?

谢谢, 拉胡

1 个答案:

答案 0 :(得分:1)

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

您的谓词评估为&#34;名称= A&#34; :这是无效的,因为集合没有名称。

在Hazelcast 3.6中,您有一个自定义属性&#39;和ValueExtractor。你可以:

  1. 创建一个拥有员工
  2. 集合的类
  3. 将此类链接到ValueExtractor,后者会在自定义属性&#39;名称&#39;
  4. 中收集每位员工的姓名

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

    此外,拥有收集地图效率不高。