gpt4 book ai didi

java - 点燃 Spring + 点燃 SQL ClassCastException

转载 作者:行者123 更新时间:2023-11-29 08:25:11 26 4
gpt4 key购买 nike

每次我尝试交替使用 Ignite Spring API 和 Ignite SQL API 时,我都会收到类转换异常或“找不到 xxxx 的 sql 表”

发生这种情况是因为我使用 ignite spring 创建了一个缓存并尝试使用 Ignite SQL API 使用数据,反之亦然:

@Configuration
@EnableIgniteRepositories("local.teste.is.api.repositories")
public class SpringAppCfg {

@Bean
public Ignite igniteInstance() {
IgniteConfiguration cfg = new IgniteConfiguration();

cfg.setIgniteInstanceName("springDataNode");
cfg.setPeerClassLoadingEnabled(true);
cfg.setClientMode(true);

...

// Defining and creating a new cache to be used by Ignite Spring Data
// repository.
CacheConfiguration ccfg = new CacheConfiguration("SQL_PUBLIC_SAMPLETYPE3");

// Setting SQL schema for the cache.
ccfg.setIndexedTypes(Integer.class, SampleType.class);

cfg.setCacheConfiguration(ccfg);

Ignite ignite = Ignition.start(cfg);

IgniteCache cache = ignite.getOrCreateCache(ccfg);

SqlQuery sql = new SqlQuery(SampleType.class, "true");

try (QueryCursor<Entry<Integer, SampleType>> cursor = cache.query(sql)) {
for (Entry<Integer, SampleType> e : cursor)
System.out.println(e.getValue().toString());
}


return ignite;
}

示例类型存储库:

package local.teste.is.api.repositories;

import org.apache.ignite.springdata.repository.IgniteRepository;
import org.apache.ignite.springdata.repository.config.RepositoryConfig;

import local.al40.is.api.entities.SampleType;

@RepositoryConfig(cacheName = "SQL_PUBLIC_SAMPLETYPE")
public interface SampleTypeRepository extends IgniteRepository<SampleType, Integer> {

public SampleType getSampleTypeBySampleTypeId(Integer id);

}

所有使用 ignite Spring 的东西都可以工作,包括保存和读取数据:

public class Application {

private static AnnotationConfigApplicationContext dataCtx;
private static SampleTypeRepository repo;

public static void main(String[] args) throws Exception {

dataCtx = new AnnotationConfigApplicationContext();

// Explicitly registering Spring configuration.
dataCtx.register(SpringAppCfg.class);
dataCtx.refresh();

repo = dataCtx.getBean(SampleTypeRepository.class);

System.out.println(repo);

SampleType s = new SampleType(1, "teste");

repo.save(s.getSampleTypeId(), s);

System.out.println(repo.getSampleTypeBySampleTypeId(1).getSampleTypeName());

如果我通过 DDL 创建一个 Ignite SQL 表并尝试通过 Ignite Spring 使用它,就会出现类似“...ignite.IgniteRepositoryImpl#123456 无法转换为 SampleType.class”的 ClassCastException。这让我相信这是一个与序列化相关的问题。如果我通过 Ignite Spring 创建表并尝试查询它,它还会给我“找不到 xxxx 的 sql 表”。有人试图整合这两种观点吗?我在网上找到的例子假设只有使用 Ignite Spring 创建的缓存,而 Ignite 的文档使这种交换似乎是可能的和透明的。但是,显然不是,除非我忘记了什么。

最好的问候,

卡洛斯·科斯塔

最佳答案

好的,我解决了我的问题。谢谢大家的帮助。为了进一步澄清我相当复杂的疑问,我将编写以下准则,如果人们试图交替使用 Ignite SQL 和 Ignite Spring,则应考虑这些准则:

选项 1) 通过 Ignite Spring 创建缓存:

a) 在存储库类中明确定义缓存名称:

...
@RepositoryConfig(cacheName = "My Cache Name")
public interface SampleTypeRepository extends IgniteRepository<SampleType, Integer> {

public SampleType getSampleTypeBySampleTypeId(Integer id);

}

b) 在 Spring 应用程序中配置 Ignite 实例时定义缓存和索引:

...
CacheConfiguration ccfg = new CacheConfiguration("My Chache Name");

// Setting SQL schema for the cache.
ccfg.setIndexedTypes(Integer.class, SampleType.class);

config.setCacheConfiguration(ccfg);

Ignite ignite = Ignition.start(config);
...

c) 查询它,但请注意,由于某种原因,SQL 表名称是“SampleType”而不是“My Cache Name”。我不知道 ehy,但这似乎是我在 Ignite Spring 文档中没有明确找到的一些默认行为。否则你可能会得到“找不到 SQL 表”:

...
IgniteCache cache = ignite.cache("My Chache Name");

SqlFieldsQuery sql = new SqlFieldsQuery("select * from SampleType");
try (QueryCursor<List<?>> cursor = cache.query(sql)) {
for (List<?> e : cursor)
System.out.println(e.get(1));
}

选项 2) 通过 SQL DDL 创建缓存:

a) 通过明确定义 cache_name、key_type 和 value_type,使用与键和值类型对应的类的完全限定包名称来创建 SQL 表/缓存:

CREATE TABLE IF NOT EXISTS SampleType(SampleTypeID int, SampleTypeName varchar, PRIMARY KEY (SampleTypeID)) WITH "cache_name=mycachename, key_type=java.lang.Integer, value_type=local.teste.is.api.entities.SampleType";

b) 要插入数据,通过 ignite Spring 使用文档中显示的 API 方法就像一个魅力。但是,通过 DDL 或 JDBC 插入时,您需要清楚地识别 _KEY 属性(显然是隐藏属性 :P):

INSERT INTO SampleType(_KEY, SAMPLETYPEID, SAMPLETYPENAME) VALUES(?,?,?)

然后,我认为您可以互换使用这些 API。

最好的问候,卡洛斯·科斯塔

关于java - 点燃 Spring + 点燃 SQL ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53814078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com