有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hazelcast:面向IllegalArgumentException的SqlPredicate

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

}

当我运行此操作时,我得到以下执行选项:

Caused by: java.lang.IllegalArgumentException: There is no suitable accessor for 'name' on class 'class java.lang.String' at com.hazelcast.query.impl.ReflectionHelper.createGetter(ReflectionHelper.java:150) ... 14 more

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

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

我试图使用SqlPredicate执行相同的活动。 有人能指出我在SqlPredicate上到底哪里做错了吗

谢谢, 拉胡尔


共 (1) 个答案

  1. # 1 楼答案

    您应该对映射的值应用谓词,因此尝试使用带有字符串键和雇员值的IMap