gpt4 book ai didi

java - Spring Boot apache ignite表未找到

转载 作者:行者123 更新时间:2023-12-01 19:51:12 27 4
gpt4 key购买 nike

首先,我对 Spring Boot、REST API,尤其是 apache ignite 还很陌生,所以这可能是一个小问题,但我暂时无法解决。

我有一个 Oracle 12c 数据库,其中包含一些表,问题中的两个表是:

create table la_license_count (
total_count number not null default 100,
active_count number not null default 0,
temporary_count number not null default 0,
id number not null
);

列“id”是主键,除此之外还有检查约束和唯一索引。

create table la_license_mapping (
client_id varchar2(200 byte) not null,
license_count number not null default 0,
license_type chat(1 byte) not null default 'F',
registered_at date not null default sysdate,
keepalive_time number,
id number not null;
);

列“id”是主键,除此之外还有检查约束和唯一约束。

需要基于 Spring Boot 的 REST API 来读取和更新此表。 Apache Ignite 缓存层需要位于 REST API 和数据库之间。这是按如下方式实现的。整个解决方案基于此指南: https://dzone.com/articles/in-memory-data-grid-with-apache-ignite ,并根据其他 StackOverflow 和各种论坛文章(当它不起作用时)进行了一些更改。

数据库 bean(自定义,因为密码以自定义方式在日志中加密)。 DataSource 本身工作正常,通过使用 Autowiring 的 JdbcTemplate 执行 CallableStatement 进行了测试;

@Configuration
public class DatabaseBean {

@Autowired
private ConfigReader configReader;

@Bean(destroyMethod = "")
public DataSource dataSource() {
DatabaseSettings db = configReader.getDatabaseSettings();

return DataSourceBuilder
.create()
.url(db.getConnectionString())
.username(db.getUserName())
.password(db.isPasswordEncrypted() ? Encryptor.decrypt(db.getPassword()) : db.getPassword())
.driverClassName(db.getDriverClassName())
.build();
}

}

实体类:

public class LALicenseCount implements Serializable {

private static final long serialVersionUID = -1L;
private final AtomicLong ID_GEN = new AtomicLong();

private Long id;
private Long totalCount;
private Long activeCount;
private Long temporaryCount;

// getters, setters

public void init() {
this.id = ID_GEN.getAndIncrement();
}

// Various methods for business logic

}

public class LALicenseMapping implements Serializable {

private static final long serialVersionUID = -1L;
private final AtomicLong ID_GEN = new AtomicLong();

private Long id;

@QuerySqlField(index = false)
private String clientID;

private Long licenseCount;

@QuerySqlField(index = false)
private String licenseType;

private Date registeredAt;

private Long keepaliveTime;

// getters, setters

public void init() {
this.id = ID_GEN.getAndIncrement();
}

// Various methods for business logic

}

存储库:

@Repository
@RepositoryConfig(cacheName = "LALicenseCountCache")
public interface LALicenseCountRepository extends IgniteRepository<LALicenseCount, Long> {

}

@Repository
@RepositoryConfig(cacheName = "LALicenseMappingCache")
public interface LALicenseMappingRepository extends IgniteRepository<LALicenseMapping, Long> {

List<LALicenseMapping> findByClientID(String clientID);

List<LALicenseMapping> findByClientIDAndLicenseType(String clientID, String licenseType);

}

点燃 bean :

@Component
public class IgniteConfigurationBean {

@Autowired
private ConfigReader configReader;

@Autowired
private ApplicationContext applicationContext;

@Bean
public Ignite igniteInstance() {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setIgniteInstanceName("ignite-1");
cfg.setPeerClassLoadingEnabled(true);

CacheConfiguration<Long, LALicenseCount> ccfg = new CacheConfiguration<>("LALicenseCountCache");
ccfg.setIndexedTyped(Long.class, LALicenseCount.class);
ccfg.setWriteBehind(true);
ccfg.setReadThrough(true);
ccfg.setWriteThrough(true);
CacheJdbcPojoStoreFactory<Long, LALicenseCount> f = new CacheJdbcPojoStoreFactory<>();
f.setDataSourceBean("dataSource");
f.setDialect(new OracleDialect());
JdbcType t = new JdbcType();
t.setCacheName("LALicenseCountCache");
t.setKeyType(Long.class);
t.setValueType(LALicenseCount.class);
t.setDatabaseTable("la_license_count");
t.setDatabaseSchema(configReader.getDatabaseSettings().getUserName());
t.setKeyFields(new JdbcTypeField(Types.INTEGER, "id", Long.class, "id"));
t.setValueFields(
new JdbcTypeField(Types.INTEGER, "total_count", Long.class, "totalCount"),
new JdbcTypeField(Types.INTEGER, "active_count", Long.class, "activeCount"),
new JdbcTypeField(Types.INTEGER, "temporary_count", Long.class, "temporaryCount"));
f.setTypes(t);
ccfg.setCacheStoreFactory(f);

CacheConfiguration<Long, LALicenseMapping> ccfg2 = new CacheConfiguration<>("LALicenseMappingCache");
ccfg2.setIndexedTyped(Long.class, LALicenseMapping.class);
ccfg2.setWriteBehind(true);
ccfg2.setReadThrough(true);
ccfg2.setWriteThrough(true);
CacheJdbcPojoStoreFactory<Long, LALicenseMapping> f2 = new CacheJdbcPojoStoreFactory<>();
f2.setDataSourceBean("dataSource");
f2.setDialect(new OracleDialect());
JdbcType t2 = new JdbcType();
t2.setCacheName("LALicenseMappingCache");
t2.setKeyType(Long.class);
t2.setValueType(LALicenseCount.class);
t2.setDatabaseTable("la_license_mapping");
t2.setDatabaseSchema(configReader.getDatabaseSettings().getUserName());
t2.setKeyFields(new JdbcTypeField(Types.INTEGER, "id", Long.class, "id"));
t2.setValueFields(
new JdbcTypeField(Types.VARCHAR, "client_id", String.class, "clientID"),
new JdbcTypeField(Types.INTEGER, "license_count", Long.class, "licenseCount"),
new JdbcTypeField(Types.VARCHAR, "license_type", String.class, "licenseType"),
new JdbcTypeField(Types.DATE, "registered_at", java.sql.Date.class, "registeredAt"),
new JdbcTypeField(Types.INTEGER, "keepalive_time", Long.class, "keepaliveTime"));
f2.setTypes(t2);
ccfg2.setCacheStoreFactory(f2);

cfg.setCacheConfiguration(ccfg, ccfg2);

return IgniteSpring.start(cfg, applicationContext);
}
}

主要应用程序类:

@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages = {...})
@EnableIgniteRepositories("...")
public class Swagger2SpringBoot extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Swagger2SpringBoot.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(Swagger2SpringBoot.class, args);
}
}

使用此配置,应用程序至少可以启动。我花了相当多的时间来弄清楚如何设置 bean、注释、依赖项等。现在我使用这些的方式:

@Service
public class LicenseManager {

@Autowired
private LALicenseCountRepository laLicenseCountRepository;

@Autowired
private LALicenseMappingRepository laLicenseMappingRepository;

@Autowired
private Ignite ignite;

private LALicenseCount licenseCount;
private IgniteCache<Long, LALicenseCount> laLicenseCountCache;

@PostConstruct
public void init() {
// There is only one LA_LICENSE_COUNT record always in the DB, and the ID is 1
licenseCount = laLicenseCountRepository.findOne(1L);
laLicenseCountCache = ignite.getOrCreateCache("LALicenseCountCache");
}

public Iterable<LALicenseMapping> getAllAllocatedLicenses() {
return laLicenseMappingRepository.findAll();
}

public void allocateNewLicense(String clientID) {
// prechecks

laLicenseCountCache.query(
new SqlFieldsQuery(
sqlReader.getLicense().getUpdateAllocatedCounts()
).setArgs(
licenseCount.getActiveCount(),
licenseCount.getTemporaryCount(),
1L
)
);

// remaining logic
}

}

请保留代码中关于1L常量的注释,这部分会改变,这段代码只是为了看看我是否可以读/写数据库。到目前为止, getAllAllocationLicenses() 函数似乎正在工作(没有发生异常),我成功地在响应中收到一个空的 JSON 数组。问题是,在我手动将一条记录输入数据库表后,我也收到一个空的 JSON 数组(不确定 Ignite 是否应该找到该记录,但我认为是的)。

更大的问题是写操作失败。应执行的 SQL 来自属性文件,如下所示:

update la_license_count set active_count = ?, temporary_count = ? where id = ?

我收到以下堆栈跟踪(仅在 laLicenseCountCache.query 行之前显示):

2018-07-11 17:40:33.211 [http-nio-9080-exec-8] INFO  h.t.c.e.service.LicenseManager - 123456 - Processing new license allocation for client
2018-07-11 17:40:33.506 [http-nio-9080-exec-8] DEBUG h.t.c.e.service.LicenseManager - 123456 - Checks done, allocating new final license
2018-07-11 17:40:33.515 [http-nio-9080-exec-8] ERROR h.t.c.e.a.LAMiddlewareApiController - Unknown exception
javax.cache.CacheException: Failed to parse query. Table "LA_LICENSE_COUNT" not found; SQL statement:
update la_license_count set active_count = ?, temporary_count = ? where id = ? [42102-196]
at org.apache.ignite.internal.processors.cache.IgniteCacheProxyImpl.query(IgniteCacheProxyImpl.java:676)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxyImpl.query(IgniteCacheProxyImpl.java:615)
at org.apache.ignite.internal.processors.cache.GatewayProtectedCacheProxy.query(GatewayProtectedCacheProxy.java:356)
at h.t.c.e.s.LicenseManager.allocateLicenseForCid(LicenseManager.java:127)
at h.t.c.e.s.a.LAMiddlewareApiController.lAMiddlewareLicensesCidGet(LAMiddlewareApiController.java:147)
...
Caused by: org.apache.ignite.internal.processors.query.IgniteSQLException: Failed to parse query. Table "LA_LICENSE_COUNT" not found; SQL statement:
update la_license_count set active_count = ?, temporary_count = ? where id = ? [42102-196]
...
Caused by: org.h2.jdbc.JdbcSQLException: Table "LA_LICENSE_COUNT" not found; SQL statement:
update la_license_count set active_count = ?, temporary_count = ? where id = ? [42102-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5552)
at org.h2.command.Parser.readTableOrView(Parser.java:5529)
at org.h2.command.Parser.readSimpleTableFilter(Parser.java:796)
at org.h2.command.Parser.parseUpdate(Parser.java:739)
at org.h2.command.Parser.parsePrepared(Parser.java:471)
at org.h2.command.Parser.parse(Parser.java:321)
at org.h2.command.Parser.parse(Parser.java:293)
at org.h2.command.Parser.prepareCommand(Parser.java:258)
at org.h2.engine.Session.prepareLocal(Session.java:578)
at org.h2.engine.Session.prepareCommand(Session.java:519)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:73)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:288)
at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.prepare0(IgniteH2Indexing.java:484)
at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.prepareStatement(IgniteH2Indexing.java:452)
at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.prepareStatement(IgniteH2Indexing.java:419)
at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.prepareStatementAndCaches(IgniteH2Indexing.java:2008)
... 85 common frames omitted

根据最后一个异常,看起来它正在尝试像访问 H2 DB 一样访问 Oracle DB?那可能吗?我为 Ignite 显式设置了 OracleDialect,并且 Oracle JDBC 驱动程序位于 pom.xml 文件中。或者 Ignite 无法连接到数据库? (我没有在日志中找到任何表明这一点的内容。)

我在这里缺少什么?

最佳答案

如果您正在针对 Apache Ignite 执行查询,则可能应该是 update "LALicenseCountCache".LALicenseCount set active_count = ?,temporary_count = ?其中 id = ?

Ignite 表名称和架构 != Oracle 表名称和架构。 Ignite 不实现 SQL 查询的透明代理。您不是通过 Ignite 查询 Oracle,而是查询 Ignite 自己的 SQL。

请注意,您还应该执行 loadCache() 来最初将所有数据从 Oracle 提取到 Apache Ignite 的缓存。否则,您的更新将无法工作,因为 Ignite 的 SQL 仅对缓存中已有的内容进行操作。

关于java - Spring Boot apache ignite表未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51290798/

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