Hazelcast:面向IllegalArgumentException的SqlPredicate

时间:2016-02-02 12:51:30

标签: java hazelcast

我正在尝试使用haelcast来使用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;
    }

}

当我运行这个时,我得到以下的执行:

  

引起:java.lang.IllegalArgumentException:类'class java.lang.String'上没有合适的'name'访问器       在com.hazelcast.query.impl.ReflectionHelper.createGetter(ReflectionHelper.java:150)       ......还有14个

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

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

我试图使用SqlPredicate执行相同的活动。 任何人都可以指出我在SqlPredicate上做错了什么?

谢谢, 拉胡

1 个答案:

答案 0 :(得分:0)

您应该将谓词应用于地图的值,因此请尝试使用带有字符串键和员工值的IMap