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

}


运行此命令时,将得到以下执行:


  原因: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我到底在哪里做错吗?

谢谢,
拉胡尔

最佳答案

您应该将谓词应用于地图的值,因此请尝试将IMap与字符串键和Employee值一起使用

10-08 11:13
查看更多