Ignite 如何使用键作为两个缓存/表的多列进行AffinityKey连接?

Ignite 如何使用键作为两个缓存/表的多列进行AffinityKey连接?,ignite,Ignite,我计划加入两个缓存,其中多个列作为键。但我发现编写连接查询很困难。 这是完整的代码。请帮助使用两个缓存/表编写联接查询 个人价值对象: private static class Person implements Serializable { private PersonKey personKey; private OrgKey orgKey; @QuerySqlField private String firstName;

我计划加入两个缓存,其中多个列作为键。但我发现编写连接查询很困难。 这是完整的代码。请帮助使用两个缓存/表编写联接查询

个人价值对象:

private static class Person implements Serializable {
        private PersonKey personKey;
        private OrgKey orgKey;
        @QuerySqlField
        private String firstName;
        @QuerySqlField
        private String lastName;
        @QueryTextField
        private String resume;
        /** Salary (indexed). */
        @QuerySqlField(index = true)
        private double salary;
        /**
         * Custom cache key to guarantee that person is always collocated with
         * its organization.
         */
        private transient AffinityKey<PersonKey> key;


        Person(Organization org, PersonKey key, String firstName, String lastName, double salary, String resume) {
            // Generate unique ID for this person.
            this.personKey = key;
            this.orgKey = org.key;
            this.firstName = firstName;
            this.lastName = lastName;
            this.resume = resume;
            this.salary = salary;
        }
private static class Organization implements Serializable {
        /** Organization ID (indexed). */
        private OrgKey key;
        /** Organization name (indexed). */
        @QuerySqlField(index = true)
        private String name;

        public Organization(OrgKey key, String name) {
            this.key = key;
            this.name = name;
        }
组织价值对象:

private static class Person implements Serializable {
        private PersonKey personKey;
        private OrgKey orgKey;
        @QuerySqlField
        private String firstName;
        @QuerySqlField
        private String lastName;
        @QueryTextField
        private String resume;
        /** Salary (indexed). */
        @QuerySqlField(index = true)
        private double salary;
        /**
         * Custom cache key to guarantee that person is always collocated with
         * its organization.
         */
        private transient AffinityKey<PersonKey> key;


        Person(Organization org, PersonKey key, String firstName, String lastName, double salary, String resume) {
            // Generate unique ID for this person.
            this.personKey = key;
            this.orgKey = org.key;
            this.firstName = firstName;
            this.lastName = lastName;
            this.resume = resume;
            this.salary = salary;
        }
private static class Organization implements Serializable {
        /** Organization ID (indexed). */
        private OrgKey key;
        /** Organization name (indexed). */
        @QuerySqlField(index = true)
        private String name;

        public Organization(OrgKey key, String name) {
            this.key = key;
            this.name = name;
        }
组织密钥类:

private static class OrgKey implements Serializable {

    @QuerySqlField(index = true)
    private int id;
    @QuerySqlField(index = true)
    private String location;
    private UUID orgId;

    public OrgKey(int id, String location, UUID orgId) {
        this.id = id;
        this.location = location;
        this.orgId = orgId;
    }
}
加入查询:

IgniteCache<AffinityKey<PersonKey>, Person> cache = Ignition.ignite().cache(PERSON_CACHE);

        String sql = "select * " + "from Persons, Organizations as org " + "where Persons.id = org.orgId";

        QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery(sql));
IgniteCache cache=Ignition.ignite().cache(个人缓存);
String sql=“从个人、组织中选择*”+“作为组织”+“其中Persons.id=org.orgId”;

QueryCursor请检查这个简单的示例,我确实通过键中的两个字段连接:

package sql;

import java.util.Arrays;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheAtomicityMode;
import org.apache.ignite.cache.query.FieldsQueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.cache.query.annotations.QuerySqlField;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.jetbrains.annotations.NotNull;

public class DoubleJoin {
    public static final String DEFAULT_CACHE_NAME = "DEFAULT";

    public static void main(String[] args) throws InterruptedException {
        IgniteConfiguration ignCfg = new IgniteConfiguration();

        TcpDiscoverySpi spi = new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder finder = new TcpDiscoveryVmIpFinder();
        finder.setAddresses(Arrays.asList("127.0.0.1"));
        spi.setIpFinder(finder);

        ignCfg.setDiscoverySpi(spi);

        Ignite ignite = Ignition.start(ignCfg);

        CacheConfiguration cacheCfg = getCacheConfiguration();

        final IgniteCache cache = ignite.getOrCreateCache(cacheCfg);

        cache.put(new A(1, 2), new AVal(100));
        cache.put(new B(1, 2), new BVal(100));

        FieldsQueryCursor cursor = cache.query(new SqlFieldsQuery("SELECT * " +
            "FROM AVal " +
            "INNER JOIN BVal ON AVal.a1=BVal.b1 AND AVal.a2=BVal.b2;"));

        for (Object o : cursor) {
            System.out.println(o);
        }
    }

    static class A {
        @AffinityKeyMapped
        @QuerySqlField
        int a1;
        @QuerySqlField
        int a2;

        public A(int a1, int a2) {
            this.a1 = a1;
            this.a2 = a2;
        }
    }

    static class AVal {
        @QuerySqlField
        int aV;

        public AVal(int aV) {
            this.aV = aV;
        }
    }

    static class B {
        @AffinityKeyMapped
        @QuerySqlField
        int b1;
        @QuerySqlField
        int b2;

        public B(int b1, int b2) {
            this.b1 = b1;
            this.b2 = b2;
        }
    }

    static class BVal {
        @QuerySqlField
        int bV;

        public BVal(int bV) {
            this.bV = bV;
        }
    }

    @NotNull private static CacheConfiguration<Integer, Integer> getCacheConfiguration() {
        CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<>();

        cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cfg.setName(DEFAULT_CACHE_NAME);

        cfg.setIndexedTypes(A.class, AVal.class, B.class, BVal.class);

        return cfg;
    }
}
包sql;
导入java.util.array;
导入org.apache.ignite.ignite;
导入org.apache.ignite.IgniteCache;
导入org.apache.ignite.Ignition;
导入org.apache.ignite.cache.CacheAtomicityMode;
导入org.apache.ignite.cache.query.FieldsQueryCursor;
导入org.apache.ignite.cache.query.SqlFieldsQuery;
导入org.apache.ignite.cache.query.annotations.QuerySqlField;
导入org.apache.ignite.configuration.CacheConfiguration;
导入org.apache.ignite.configuration.ignite配置;
导入org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
导入org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
导入org.jetbrains.annotations.NotNull;
公共类双连接{
公共静态最终字符串DEFAULT\u CACHE\u NAME=“DEFAULT”;
公共静态void main(字符串[]args)引发InterruptedException{
IgniteConfiguration ignCfg=新IgniteConfiguration();
TcpDiscoverySpi spi=新的TcpDiscoverySpi();
TcpDiscoveryVmIpFinder=新的TcpDiscoveryVmIpFinder();
finder.setAddresses(Arrays.asList(“127.0.0.1”);
spi.setIpFinder(查找器);
ignCfg.setDiscoverySpi(spi);
点火=点火启动(ignCfg);
CacheConfiguration cacheCfg=getCacheConfiguration();
final IgniteCache cache=ignite.getOrCreateCache(cacheCfg);
cache.put(新的A(1,2),新的AVal(100));
cache.put(新的B(1,2),新的BVal(100));
FieldsQueryCursor cursor=cache.query(新建SqlFieldsQuery(“选择”)+
“来自阿瓦尔”+
“AVal.a1=BVal.b1和AVal.a2=BVal.b2上的内部连接BVal;”);
用于(对象o:光标){
系统输出打印ln(o);
}
}
静态A类{
@亲缘关系
@QuerySqlField
int a1;
@QuerySqlField
int a2;
公共A(内部a1、内部a2){
这1.a1=a1;
这1.a2=a2;
}
}
静态类AVal{
@QuerySqlField
int aV;
公共AVA(国际AVA){
this.aV=aV;
}
}
静态B类{
@亲缘关系
@QuerySqlField
int b1;
@QuerySqlField
int b2;
公共B区(b1区、b2区){
这1.b1=b1;
这1.b2=b2;
}
}
静态类BVal{
@QuerySqlField
int bV;
公共BVal(内部bV){
this.bV=bV;
}
}
@NotNull私有静态缓存配置getCacheConfiguration(){
CacheConfiguration cfg=新的CacheConfiguration();
setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
setName(默认的\u缓存\u名称);
setIndexedTypes(A.class、AVal.class、B.class、BVal.class);
返回cfg;
}
}

我对a类做了一个小修改,如果我错了,请纠正我。我已经按照下面的链接使用了关联密钥映射器。

package.com.cache;
导入java.util.array;
导入org.apache.ignite.ignite;
导入org.apache.ignite.IgniteCache;
导入org.apache.ignite.Ignition;
导入org.apache.ignite.cache.CacheAtomicityMode;
导入org.apache.ignite.cache.affinity.AffinityKeyMapped;
导入org.apache.ignite.cache.query.FieldsQueryCursor;
导入org.apache.ignite.cache.query.SqlFieldsQuery;
导入org.apache.ignite.cache.query.annotations.QuerySqlField;
导入org.apache.ignite.configuration.CacheConfiguration;
导入org.apache.ignite.configuration.ignite配置;
导入org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
导入org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
导入org.jetbrains.annotations.NotNull;
公共类双连接{
公共静态最终字符串DEFAULT\u CACHE\u NAME=“DEFAULT”;
公共静态void main(字符串[]args)引发InterruptedException{
IgniteConfiguration ignCfg=新IgniteConfiguration();
TcpDiscoverySpi spi=新的TcpDiscoverySpi();
TcpDiscoveryVmIpFinder=新的TcpDiscoveryVmIpFinder();
finder.setAddresses(Arrays.asList(“127.0.0.1”);
spi.setIpFinder(查找器);
ignCfg.setDiscoverySpi(spi);
点火=点火启动(ignCfg);
CacheConfiguration cacheCfg=getCacheConfiguration();
final IgniteCache cache=ignite.getOrCreateCache(cacheCfg);
cache.put(新的A(1,2),新的AVal(100,新的A(1,2));
cache.put(新的A(2,3),新的AVal(200,新的A(2,3));
cache.put(新的A(3,4),新的AVal(300,新的A(3,4));
cache.put(新的B(1,2),新的BVal(200));
FieldsQueryCursor cursor=cache.query(新的SqlFieldsQuery(“从AVL中选择BVall.b1、BVall.b2、BVall.bV”+”)
+“内部联接(从BVal中选择*,其中BVal.bV=?)AVal.a1=BVall.b1和AVal.a2=BVall.b2;”).setArgs(200));
用于(对象o:光标){
系统输出打印ln(o);
}
}
静态A类{
@QuerySqlField
int a1;
@QuerySqlField
int a2;
@亲缘关系
int b1;
@亲缘关系
int b2;
公共A(内部a1、内部a2){
这1.a1=a1;
这1.a2=a2;
这1.b1=a1;
这1.b2=a2;
}
}
静态类AVal{
@QuerySqlField
int aV;
A A;
公共AVA(国际AVA,A){
this.aV=aV;
 package com.cache;

import java.util.Arrays;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheAtomicityMode;
import org.apache.ignite.cache.affinity.AffinityKeyMapped;
import org.apache.ignite.cache.query.FieldsQueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.cache.query.annotations.QuerySqlField;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.jetbrains.annotations.NotNull;

public class DoubleJoin {
    public static final String DEFAULT_CACHE_NAME = "DEFAULT";

    public static void main(String[] args) throws InterruptedException {
        IgniteConfiguration ignCfg = new IgniteConfiguration();

        TcpDiscoverySpi spi = new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder finder = new TcpDiscoveryVmIpFinder();
        finder.setAddresses(Arrays.asList("127.0.0.1"));
        spi.setIpFinder(finder);

        ignCfg.setDiscoverySpi(spi);

        Ignite ignite = Ignition.start(ignCfg);

        CacheConfiguration cacheCfg = getCacheConfiguration();

        final IgniteCache cache = ignite.getOrCreateCache(cacheCfg);

        cache.put(new A(1, 2), new AVal(100, new A(1, 2)));
        cache.put(new A(2, 3), new AVal(200, new A(2, 3)));
        cache.put(new A(3, 4), new AVal(300, new A(3, 4)));
        cache.put(new B(1, 2), new BVal(200));

        FieldsQueryCursor cursor = cache.query(new SqlFieldsQuery("SELECT BVall.b1, BVall.b2, BVall.bV " + "FROM AVal "
                + "INNER JOIN (select *  from BVal where BVal.bV = ?) BVall ON AVal.a1=BVall.b1 AND AVal.a2=BVall.b2  ;").setArgs(200));

        for (Object o : cursor) {
            System.out.println(o);
        }
    }

    static class A {
        @QuerySqlField
        int a1;
        @QuerySqlField
        int a2;
        @AffinityKeyMapped
        int b1;
        @AffinityKeyMapped
        int b2;

        public A(int a1, int a2) {
            this.a1 = a1;
            this.a2 = a2;
            this.b1 = a1;
            this.b2 = a2;
        }
    }

    static class AVal {
        @QuerySqlField
        int aV;

        A a;

        public AVal(int aV,  A a) {
            this.aV = aV;
            this.a = a;
        }
    }

    static class B {
        @QuerySqlField
        int b1;
        @QuerySqlField
        int b2;

        public B(int b1, int b2) {
            this.b1 = b1;
            this.b2 = b2;
        }
    }

    static class BVal {
        @QuerySqlField
        int bV;

        public BVal(int bV) {
            this.bV = bV;
        }
    }

    @NotNull
    private static CacheConfiguration<Integer, Integer> getCacheConfiguration() {
        CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<>();

        cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cfg.setName(DEFAULT_CACHE_NAME);

        cfg.setIndexedTypes(A.class, AVal.class, B.class, BVal.class);

        return cfg;
    }
}