CacheConfig class:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.ignite.organization;

import java.sql.*;
import java.util.*;
import org.apache.ignite.cache.*;
import org.apache.ignite.cache.store.jdbc.*;
import org.apache.ignite.configuration.*;

/**
 * CacheConfig definition.
 *
 * Code generated by Apache Ignite Schema Import utility: 04/11/2016.
 */
public class CacheConfig {
    /**
     * Create JDBC type for organization.
     *
     * @param cacheName Cache name.
     * @return Configured JDBC type.
     */
    private static JdbcType jdbcTypeOrganization(String cacheName) {
        JdbcType jdbcType = new JdbcType();

        jdbcType.setCacheName(cacheName);
        jdbcType.setDatabaseSchema("ORG");
        jdbcType.setDatabaseTable("organization");
       
jdbcType.setKeyType("org.apache.ignite.organization.OrganizationKey");
       
jdbcType.setValueType("org.apache.ignite.organization.Organization");

        // Key fields for organization.
        Collection<JdbcTypeField> keys = new ArrayList<>();
        keys.add(new JdbcTypeField(Types.INTEGER, "orgId", int.class,
"orgid"));
        jdbcType.setKeyFields(keys.toArray(new JdbcTypeField[keys.size()]));

        // Value fields for organization.
        Collection<JdbcTypeField> vals = new ArrayList<>();
        vals.add(new JdbcTypeField(Types.INTEGER, "orgId", int.class,
"orgid"));
        vals.add(new JdbcTypeField(Types.VARCHAR, "orgname", String.class,
"orgname"));
        jdbcType.setValueFields(vals.toArray(new
JdbcTypeField[vals.size()]));

        return jdbcType;
    }

    /**
     * Create SQL Query descriptor for organization.
     *
     * @return Configured query entity.
     */
    private static QueryEntity queryEntityOrganization() {
        QueryEntity qryEntity = new QueryEntity();

       
qryEntity.setKeyType("org.apache.ignite.organization.OrganizationKey");
       
qryEntity.setValueType("org.apache.ignite.organization.Organization");

        // Query fields for organization.
        LinkedHashMap<String, String> fields = new LinkedHashMap<>();

        fields.put("orgid", "int");
        fields.put("orgname", "String");

        qryEntity.setFields(fields);

        // Indexes for organization.
        Collection<QueryIndex> idxs = new ArrayList<>();

        idxs.add(new QueryIndex("orgId", true, "PRIMARY"));

        qryEntity.setIndexes(idxs);

        return qryEntity;
    }

    /**
     * Create JDBC type for person.
     *
     * @param cacheName Cache name.
     * @return Configured JDBC type.
     */
    private static JdbcType jdbcTypePerson(String cacheName) {
        JdbcType jdbcType = new JdbcType();

        jdbcType.setCacheName(cacheName);
        jdbcType.setDatabaseSchema("ORG");
        jdbcType.setDatabaseTable("person");
        jdbcType.setKeyType("org.apache.ignite.organization.PersonKey");
        jdbcType.setValueType("org.apache.ignite.organization.Person");

        // Key fields for person.
        Collection<JdbcTypeField> keys = new ArrayList<>();
        keys.add(new JdbcTypeField(Types.INTEGER, "id", int.class, "id"));
        jdbcType.setKeyFields(keys.toArray(new JdbcTypeField[keys.size()]));

        // Value fields for person.
        Collection<JdbcTypeField> vals = new ArrayList<>();
        vals.add(new JdbcTypeField(Types.INTEGER, "id", int.class, "id"));
        vals.add(new JdbcTypeField(Types.VARCHAR, "first_name",
String.class, "firstName"));
        vals.add(new JdbcTypeField(Types.VARCHAR, "last_name", String.class,
"lastName"));
        vals.add(new JdbcTypeField(Types.INTEGER, "orgId", Integer.class,
"orgid"));
        jdbcType.setValueFields(vals.toArray(new
JdbcTypeField[vals.size()]));

        return jdbcType;
    }

    /**
     * Create SQL Query descriptor for person.
     *
     * @return Configured query entity.
     */
    private static QueryEntity queryEntityPerson() {
        QueryEntity qryEntity = new QueryEntity();

        qryEntity.setKeyType("org.apache.ignite.organization.PersonKey");
        qryEntity.setValueType("org.apache.ignite.organization.Person");

        // Query fields for person.
        LinkedHashMap<String, String> fields = new LinkedHashMap<>();

        fields.put("id", "int");
        fields.put("firstName", "String");
        fields.put("lastName", "String");
        fields.put("orgid", "Integer");

        qryEntity.setFields(fields);

        // Indexes for person.
        Collection<QueryIndex> idxs = new ArrayList<>();

        idxs.add(new QueryIndex("id", true, "PRIMARY"));

        qryEntity.setIndexes(idxs);

        return qryEntity;
    }

    /**
     * Configure cache.
     *
     * @param cacheName Cache name.
     * @param storeFactory Cache store factory.
     * @return Cache configuration.
     */
    public static <K, V> CacheConfiguration<K, V> cache(String cacheName,
CacheJdbcPojoStoreFactory<K, V> storeFactory) {
        if (storeFactory == null)
             throw new IllegalArgumentException("Cache store factory cannot
be null.");

        CacheConfiguration<K, V> ccfg = new CacheConfiguration<>(cacheName);

        ccfg.setCacheStoreFactory(storeFactory);
        ccfg.setReadThrough(true);
        ccfg.setWriteThrough(true);

        // Configure JDBC types. 
        Collection<JdbcType> jdbcTypes = new ArrayList<>();

        jdbcTypes.add(jdbcTypeOrganization(cacheName));
        jdbcTypes.add(jdbcTypePerson(cacheName));

        storeFactory.setTypes(jdbcTypes.toArray(new
JdbcType[jdbcTypes.size()]));

        // Configure query entities. 
        Collection<QueryEntity> qryEntities = new ArrayList<>();

        qryEntities.add(queryEntityOrganization());
        qryEntities.add(queryEntityPerson());

        ccfg.setQueryEntities(qryEntities);

        return ccfg;
    }
}




--
View this message in context: 
http://apache-ignite-users.70518.x6.nabble.com/How-to-load-2-tables-in-a-cache-tp4026p4128.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Reply via email to