一、JDBC方式连接Ignite集群
通过JDBC方式连接Ignite集群,执行SQL进行见表,merge into插入数据,并执行结果查询示例如下:
/**
* 测试 ignite jdbc连接,简单创建按一张表,插入一条数据,然后查询出来,并验证字段是否正确
* @throws ClassNotFoundException
* @throws SQLException
*/
@Test
public void testIgniteJdbcConnection() throws ClassNotFoundException, SQLException, IOException, InvocationTargetException, IllegalAccessException {
EnvConfig envConfig = ConfigUtils.getEnvConfig();
Class.forName("org.apache.ignite.IgniteJdbcThinDriver");
Connection conn = DriverManager.getConnection(envConfig.getJdbcUrl());
Statement statement = conn.createStatement();
statement.execute("CREATE TABLE IF NOT EXISTS City (\n" +
" ID INT(11),\n" +
" Name CHAR(35),\n" +
" CountryCode CHAR(3),\n" +
" District CHAR(20),\n" +
" Population INT(11),\n" +
" PRIMARY KEY (ID, CountryCode)\n" +
") WITH \"backups=1, CACHE_NAME=City\";\n");
statement.execute("MERGE INTO public.City (ID,Name,CountryCode) VALUES (1,'testName','95')");
ResultSet resultSet = statement.executeQuery("select Name from PUBLIC.City");
String name = null;
while (resultSet.next()){
name = resultSet.getString("Name");
break;
}
Assert.assertEquals(name,"testName");
}
二、java client方式连接Ignite集群
* 1、缓存表的创建,索引创建(显式+注解) * 2、数据put * 3、数据get * 4、查询数据 * 5、然后基于此cache表执行task任务(统计数据条数并返回)
import com.hs.bigdata.ignite.testcase.env.EnvConfig;
import com.hs.bigdata.ignite.testcase.model.Person;
import com.hs.bigdata.ignite.testcase.utils.ConfigUtils;
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.CacheMode;
import org.apache.ignite.cache.CacheWriteSynchronizationMode;
import org.apache.ignite.cache.query.FieldsQueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.deployment.uri.UriDeploymentSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
public class IgniteTest2_JavaClientTestCase {
/**
* 通过 java client方式连接Ignite,
* 然后测试
* 1、缓存表的创建,索引创建(显式+注解)
* 2、数据put
* 3、数据get
* 4、查询数据
* 5、然后基于此cache表执行task任务(统计数据条数并返回)
*/
@Test
public void testIgniteJdbcConnection() throws IOException, InvocationTargetException, IllegalAccessException {
EnvConfig envConfig = ConfigUtils.getEnvConfig();
List<String> igniteAddress1 = envConfig.getIgniteAddress1();
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setClientMode(true);
cfg.setPeerClassLoadingEnabled(true);
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(igniteAddress1);
cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
UriDeploymentSpi uriDeploymentSpi = new UriDeploymentSpi();
cfg.setDeploymentSpi(uriDeploymentSpi);
Ignite ignite = Ignition.start(cfg);
//1、定义缓存配置
String cacheName = Person.class.getSimpleName();
CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<>();
ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT);
//1.1 注册索引类型
ccfg.setIndexedTypes(Long.class, Person.class);
ccfg.setName(cacheName);
ccfg.setCacheMode(CacheMode.REPLICATED);
ccfg.setBackups(0);
ccfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
ccfg.setSqlSchema("PUBLIC");
//缓存已经存在则删除
ignite.destroyCache(cacheName);
//1.2、注册缓存
IgniteCache<Long, Person> cache = ignite.getOrCreateCache(ccfg);
//2、Put api写入数据到缓存,也可以使用insert方式
long id = 1L;
String name = "xiaohua";
int age = 12;
float salary = 123.01f;
Person person = new Person(id,name,age,salary);
cache.put(id,person);
//3、get数据
Person personTemp = cache.get(id);
Assert.assertEquals(id,personTemp.getId());
Assert.assertEquals(name,personTemp.getName());
Assert.assertEquals(age,personTemp.getAge());
Assert.assertEquals(salary,personTemp.getSalary(),0l);
//4、查询缓存表
SqlFieldsQuery qry = new SqlFieldsQuery("SELECT * from public.Person where name=" + "'" + name + "'");
FieldsQueryCursor<List<?>> query = cache.query(qry);
Iterator<List<?>> iterator = query.iterator();
while (iterator.hasNext()){
List<?> next = iterator.next();
Assert.assertEquals(next.get(0),personTemp.getId());
Assert.assertEquals(next.get(1),personTemp.getName());
Assert.assertEquals(next.get(2),personTemp.getSalary());
}
ignite.close();
}
}
三、maven依赖
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.12</junit.version>
<ignite.version>2.14.0</ignite.version>
<slf4j.version>1.7.15</slf4j.version>
<slf4j.simple.version>1.7.25</slf4j.simple.version>
<common.lang3.version>3.11</common.lang3.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>model</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.simple.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${common.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-urideploy</artifactId>
<version>${ignite.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
标签:Ignite,ignite,jdbc,示例,cache,ccfg,import,apache,org
From: https://blog.csdn.net/qq_43462685/article/details/142095899