如何为从多个表连接的对象定义 Ignite 缓存

How to define Ignite Caches for objects Joined from Multiple Tables

我正在将 Postgres 设置为 Ignite 的外部持久存储,想知道有哪些方法可以为数据分布在多个表中的对象定义缓存。

为了使用这个 Person 和 Car class 及其下面的表格,我提供了我自己的 CacheStore 实现。这种方法似乎非常冗长,但是因为我需要自己手动分配字段值。我可以使用任何其他方法来执行此操作吗?

人Class:

public class PersonMO{
        private int id;
        private String name;
        private String address;
        private Set<Car> cars

        public PersonMO() {};
        public PersonMO(int id, String name, String address) {
                this.id = id;
                this.name = name;
                this.address = address;
        }
       
        public String getName() {
                return name;
        }
       
        public void setName(String name) {
                this.name = name;
        }

        public int getId() {
                return id;
        }
       
        public void setId(int id) {
                this.id = id;
        }
       
        public String getAddress() {
                return address;
        }
       
        public void setAddress(String address) {
                this.address = address;
        }
       
        public String toString() {
                return "ID: "+id +", Name: "+name+" AD: " +address;
        }

        public void setCars(Set<Car> cars) {
                this.cars = cars;
        }
       
        public Set<Car> getCars() {
                return cars;
        }
}

汽车Class

public class Car {
        int id;
        private String name;
       
        public Car(int id, String name) {
                this.id = id;
                this.name = name;
        }
       
        public int getId() {
                return id;
        }
       
        public void setId(int id) {
                this.id = id;
        }
       
        public String getName() {
                return name;
        }
       
        public void setName(String name) {
                this.name = name;
        }
       
}

CacheStore 实现

public class PersonMOCacheStore implements CacheStore<Integer, PersonMO>{

        @SpringResource(resourceName = "pgDataSource")
        private DriverManagerDataSource pgDataSource;
       
        @LoggerResource
    private IgniteLogger log;

        //public void loadCache(IgniteBiInClosure<Integer, PersonMO> clo, @Nullable Object... args)
        @Override
        public void loadCache(IgniteBiInClosure<Integer, PersonMO> clo, Object... args)
                        throws CacheLoaderException {
                log.info(">> Loading cache from store...");
               
                try(Connection conn = pgDataSource.getConnection()){
                        try(PreparedStatement st = conn.prepareStatement("select * from PERSON")){
                                try(ResultSet rs = st.executeQuery()){
                                        while(rs.next()) {
                                                PersonMO person = new PersonMO(rs.getInt(1),rs.getString(2), rs.getString(3));
                                                person.setCars(getCarSet(conn, person.getId() ) );
                                                clo.apply(person.getId(), person);
                                        }
                                        log.info(">> Finished Loading cache from store...");
                                }
                        }
                }catch(SQLException e) {
                         throw new CacheLoaderException("Failed to load values from cache store.",e);
                }
               
        }
       
        //implementation for IgniteCache.get
        @Override
        public PersonMO load(Integer key) throws CacheLoaderException {
                log.info(">> Loading person from store...");
               
                try (Connection conn = pgDataSource.getConnection()) {
                        try(PreparedStatement st = conn.prepareStatement("select * from PERSON where id = ?")){
                                st.setString(1, key.toString());
                                ResultSet rs = st.executeQuery();
                                if(rs.next()) {
                                        PersonMO p= new PersonMO(rs.getInt(1),rs.getString(2), rs.getString(3));
                                        //p.setCars( getCarSet(conn, p.getId() ) );
                                        return p;
                                }else {
                                        return null;
                                }
                               
                               
                        }
                }catch(SQLException e) {
                         throw new CacheLoaderException("Failed to load values from cache store.",e);
                }
        }


        private Set<Car> getCarSet(Connection conn, int personId) throws SQLException{
                Set<Car> carSet = new HashSet<Car>();
                PreparedStatement st = conn.prepareStatement("select * from CAR where id = "+ personId);
                ResultSet rs = st.executeQuery();
               
                while(rs.next()) {
                        carSet.add(new Car(rs.getInt(1),rs.getString(2) ));
                }
                return carSet;
        }
       //other methods needed left out for sake of simplicity 
}

您可以使用 CacheJdbcPojoStore 来填充缓存。然后可以通过 SQL 或 key/value API 访问数据。

然而,它并没有那么冗长:)

https://www.gridgain.com/docs/latest/developers-guide/persistence/external-storage#cachejdbcpojostore