我正在尝试使用SQL Server实现Ignite第三方持久性。我使用Web控制台生成java模型,我能够毫无问题地执行生成的实用程序和配置文件(LoadCaches或ServerNodeCodeStartup,带有来自ServerConfigurationFactory的配置) - 缓存加载调用执行没有任何问题。
public TestPersistentStore() throws Exception {
try (Ignite ignite = Ignition.start(ServerConfigurationFactory.createConfiguration())) {
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, Customer> cache = ignite.cache("CustomerCache")) {
// Make initial cache loading from persistent store. This is a
// distributed operation and will call CacheStore.loadCache(...)
// method on all nodes in topology.
loadCache(cache);
// Start transaction and execute several cache operations with
// read/write-through to persistent store.
executeTransaction(cache);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache("CustomerCache");
}
} catch (Exception e) {
e.printStackTrace();
}
}
executeTransaction的内容:
private static void executeTransaction(IgniteCache<Integer, Customer> cache) {
int id = 1;
try (Transaction tx = Ignition.ignite("acmecorp").transactions().txStart()) {
Customer val = cache.get(id);
System.out.println("Read value: " + val);
val = cache.getAndPut(id, new Customer(id, "Isaac", "Newton", "Ixelles"));
System.out.println("Overwrote old value: " + val);
val = cache.get(id);
System.out.println("Read value: " + val);
tx.commit();
}
System.out.println("Read value after commit: " + cache.get(id));
}
(其中许多代码行都是从CacheJdbcStoreExample复制的)
执行结果:
[22:53:35] Topology snapshot [ver=1, servers=1, clients=0, CPUs=4, heap=3.5GB]
>>> Loaded 10 keys with backups in 450ms.
Read value: Customer [firstName=Isaac, lastName=Newton, address=Ixelles]
Overwrote old value: Customer [firstName=Isaac, lastName=Newton, address=Ixelles]
Read value: Customer [firstName=Isaac, lastName=Newton, address=Ixelles]
Read value after commit: Customer [firstName=Isaac, lastName=Newton, address=Ixelles]
Read value skipping store (expecting null): null
Read value with store lookup (expecting NOT null): Customer [firstName=Isaac, lastName=Newton, address=Ixelles]
Read value skipping store (expecting NOT null): Customer [firstName=Isaac, lastName=Newton, address=Ixelles]
[22:53:36] Ignite node stopped OK [name=acmecorp, uptime=00:00:00:638]
因此,这适用于客户表/缓存。我想使用相同的代码,但使用另一个表/缓存(项),如下所示:
TestPersistentStore2
public TestPersistentStore2() throws Exception {
try (Ignite ignite = Ignition.start(ServerConfigurationFactory.createConfiguration())) {
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, Item> cache = ignite.cache("ItemCache")) {
// Make initial cache loading from persistent store. This is a
// distributed operation and will call CacheStore.loadCache(...)
// method on all nodes in topology.
loadCache(cache);
// Start transaction and execute several cache operations with
// read/write-through to persistent store.
executeTransaction(cache);
} catch (Exception e) {
System.out.println("sumthin happened");
e.printStackTrace();
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache("ItemCache");
}
} catch (Exception e) {
e.printStackTrace();
}
}
TestPersistentStore2#executeTransaction
private static void executeTransaction(IgniteCache<Integer, Item> cache) {
int id = 1;
try (Transaction tx = Ignition.ignite("acmecorp").transactions().txStart()) {
Item val = cache.get(id);
System.out.println("Read value: " + val);
val = cache.getAndPut(id, new Item(id, "n", "b", "t", "m", "d"));
System.out.println("Overwrote old value: " + val);
val = cache.get(id);
System.out.println("Read value: " + val);
tx.commit();
}
System.out.println("Read value after commit: " + cache.get(id));
// Clear entry from memory, but keep it in store.
cache.clear(id);
// Operations on this cache will not affect store.
IgniteCache<Integer, Item> cacheSkipStore = cache.withSkipStore();
System.out.println("Read value skipping store (expecting null): " + cacheSkipStore.get(id));
System.out.println("Read value with store lookup (expecting NOT null): " + cache.get(id));
// Expecting not null, since entry should be in memory since last call.
System.out.println("Read value skipping store (expecting NOT null): " + cacheSkipStore.get(id));
}
但是我在executeTransaction的cache.get(id)调用中得到以下异常:
>>> Loaded 72 keys with backups in 648ms.
javax.cache.CacheException: class org.apache.ignite.IgniteCheckedException: Unknown pair [platformId=0, typeId=123254525]
at org.apache.ignite.internal.processors.cache.GridCacheUtils.convertToCacheException(GridCacheUtils.java:1312)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.cacheException(IgniteCacheProxy.java:2630)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.get(IgniteCacheProxy.java:1188)
at infoh415.project.test.TestPersistentStore2.executeTransaction(TestPersistentStore2.java:98)
at infoh415.project.test.TestPersistentStore2.<init>(TestPersistentStore2.java:69)
at infoh415.project.test.TestPersistentStore2.main(TestPersistentStore2.java:130)
Caused by: class org.apache.ignite.IgniteCheckedException: Unknown pair [platformId=0, typeId=123254525]
at org.apache.ignite.internal.util.IgniteUtils.cast(IgniteUtils.java:7229)
at org.apache.ignite.internal.util.future.GridFutureAdapter.resolve(GridFutureAdapter.java:258)
at org.apache.ignite.internal.util.future.GridFutureAdapter.get0(GridFutureAdapter.java:170)
at org.apache.ignite.internal.util.future.GridFutureAdapter.get(GridFutureAdapter.java:139)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.get0(GridCacheAdapter.java:4499)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.get(GridCacheAdapter.java:4480)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.get(GridCacheAdapter.java:1324)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.get(IgniteCacheProxy.java:1181)
... 3 more
Caused by: java.lang.ClassNotFoundException: Unknown pair [platformId=0, typeId=123254525]
at org.apache.ignite.internal.MarshallerContextImpl.getClassName(MarshallerContextImpl.java:392)
at org.apache.ignite.internal.MarshallerContextImpl.getClass(MarshallerContextImpl.java:342)
at org.apache.ignite.internal.binary.BinaryContext.descriptorForTypeId(BinaryContext.java:686)
at org.apache.ignite.internal.binary.BinaryReaderExImpl.deserialize0(BinaryReaderExImpl.java:1755)
at org.apache.ignite.internal.binary.BinaryReaderExImpl.deserialize(BinaryReaderExImpl.java:1714)
at org.apache.ignite.internal.binary.BinaryObjectImpl.deserializeValue(BinaryObjectImpl.java:797)
at org.apache.ignite.internal.binary.BinaryObjectImpl.value(BinaryObjectImpl.java:143)
at org.apache.ignite.internal.processors.cache.CacheObjectUtils.unwrapBinary(CacheObjectUtils.java:161)
at org.apache.ignite.internal.processors.cache.CacheObjectUtils.unwrapBinaryIfNeeded(CacheObjectUtils.java:41)
at org.apache.ignite.internal.processors.cache.CacheObjectContext.unwrapBinaryIfNeeded(CacheObjectContext.java:125)
at org.apache.ignite.internal.processors.cache.GridCacheContext.unwrapBinaryIfNeeded(GridCacheContext.java:1734)
at org.apache.ignite.internal.processors.cache.GridCacheContext.unwrapBinaryIfNeeded(GridCacheContext.java:1722)
at org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture.setResult(GridPartitionedSingleGetFuture.java:645)
at org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture.localGet(GridPartitionedSingleGetFuture.java:438)
at org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture.mapKeyToNode(GridPartitionedSingleGetFuture.java:324)
at org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture.map(GridPartitionedSingleGetFuture.java:212)
at org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture.init(GridPartitionedSingleGetFuture.java:204)
at org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicCache.getAsync0(GridDhtAtomicCache.java:1445)
at org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicCache.access$1600(GridDhtAtomicCache.java:129)
at org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicCache$16.apply(GridDhtAtomicCache.java:513)
at org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicCache$16.apply(GridDhtAtomicCache.java:511)
at org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicCache.asyncOp(GridDhtAtomicCache.java:806)
at org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicCache.getAsync(GridDhtAtomicCache.java:511)
... 7 more
Disconnected from the target VM, address: '127.0.0.1:49513', transport: 'socket'
我仔细检查了CustomerCache和ItemCache的CacheConfiguration之间的差异,没有任何意外(表和字段名称中只有差异)。我还比较了模型类,它们也是相似的。
在此附加 CustomerCache
的配置 /**
* Create configuration for cache "CustomerCache".
*
* @return Configured cache.
* @throws Exception if failed to create cache configuration.
**/
public static CacheConfiguration cacheCustomerCache() throws Exception {
CacheConfiguration ccfg = new CacheConfiguration();
ccfg.setName("CustomerCache");
ccfg.setCacheMode(CacheMode.PARTITIONED);
ccfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
CacheJdbcPojoStoreFactory cacheStoreFactory = new CacheJdbcPojoStoreFactory();
cacheStoreFactory.setDataSourceFactory(new Factory<DataSource>() {
/** {@inheritDoc} **/
@Override public DataSource create() {
return DataSources.INSTANCE_dsSQLServer_Acmecorp;
};
});
cacheStoreFactory.setDialect(new SQLServerDialect());
cacheStoreFactory.setTypes(jdbcTypeCustomer(ccfg.getName()));
ccfg.setCacheStoreFactory(cacheStoreFactory);
ccfg.setReadThrough(true);
ccfg.setWriteThrough(true);
ArrayList<QueryEntity> qryEntities = new ArrayList<>();
QueryEntity qryEntity = new QueryEntity();
qryEntity.setKeyType("java.lang.Integer");
qryEntity.setValueType("infoh415.project.model.Customer");
qryEntity.setKeyFieldName("customerId");
HashSet<String> keyFields = new HashSet<>();
keyFields.add("customerId");
qryEntity.setKeyFields(keyFields);
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("firstName", "java.lang.String");
fields.put("lastName", "java.lang.String");
fields.put("address", "java.lang.String");
fields.put("customerId", "java.lang.Integer");
qryEntity.setFields(fields);
HashMap<String, String> aliases = new HashMap<>();
aliases.put("customerId", "customer_id");
aliases.put("firstName", "first_name");
aliases.put("lastName", "last_name");
qryEntity.setAliases(aliases);
qryEntities.add(qryEntity);
ccfg.setQueryEntities(qryEntities);
return ccfg;
}
/**
* Create JDBC type for "jdbcTypeCustomer".
*
* @param cacheName Cache name.
* @return Configured JDBC type.
**/
private static JdbcType jdbcTypeCustomer(String cacheName) {
JdbcType type = new JdbcType();
type.setCacheName(cacheName);
type.setKeyType(Integer.class);
type.setValueType("infoh415.project.model.Customer");
type.setDatabaseSchema("dbo");
type.setDatabaseTable("Customer");
type.setKeyFields(new JdbcTypeField(Types.INTEGER, "customer_id", int.class, "customerId"));
type.setValueFields(
new JdbcTypeField(Types.VARCHAR, "first_name", String.class, "firstName"),
new JdbcTypeField(Types.VARCHAR, "last_name", String.class, "lastName"),
new JdbcTypeField(Types.VARCHAR, "address", String.class, "address")
);
return type;
}
VS。配置 ItemCache
/**
* Create configuration for cache "ItemCache".
*
* @return Configured cache.
* @throws Exception if failed to create cache configuration.
**/
public static CacheConfiguration cacheItemCache() throws Exception {
CacheConfiguration ccfg = new CacheConfiguration();
ccfg.setName("ItemCache");
ccfg.setCacheMode(CacheMode.PARTITIONED);
ccfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
CacheJdbcPojoStoreFactory cacheStoreFactory = new CacheJdbcPojoStoreFactory();
cacheStoreFactory.setDataSourceFactory(new Factory<DataSource>() {
/** {@inheritDoc} **/
@Override public DataSource create() {
return DataSources.INSTANCE_dsSQLServer_Acmecorp;
};
});
cacheStoreFactory.setDialect(new SQLServerDialect());
cacheStoreFactory.setTypes(jdbcTypeItem(ccfg.getName()));
ccfg.setCacheStoreFactory(cacheStoreFactory);
ccfg.setReadThrough(true);
ccfg.setWriteThrough(true);
ArrayList<QueryEntity> qryEntities = new ArrayList<>();
QueryEntity qryEntity = new QueryEntity();
qryEntity.setKeyType("java.lang.Integer");
qryEntity.setValueType("infoh415.project.model.Item");
qryEntity.setKeyFieldName("itemId");
HashSet<String> keyFields = new HashSet<>();
keyFields.add("itemId");
qryEntity.setKeyFields(keyFields);
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("name", "java.lang.String");
fields.put("brand", "java.lang.String");
fields.put("type", "java.lang.String");
fields.put("manufacturer", "java.lang.String");
fields.put("description", "java.lang.String");
fields.put("itemId", "java.lang.Integer");
qryEntity.setFields(fields);
HashMap<String, String> aliases = new HashMap<>();
aliases.put("itemId", "item_id");
qryEntity.setAliases(aliases);
qryEntities.add(qryEntity);
ccfg.setQueryEntities(qryEntities);
return ccfg;
}
/**
* Create JDBC type for "jdbcTypeItem".
*
* @param cacheName Cache name.
* @return Configured JDBC type.
**/
private static JdbcType jdbcTypeItem(String cacheName) {
JdbcType type = new JdbcType();
type.setCacheName(cacheName);
type.setKeyType(Integer.class);
type.setValueType("infoh415.project.model.Item");
type.setDatabaseSchema("dbo");
type.setDatabaseTable("Item");
type.setKeyFields(new JdbcTypeField(Types.INTEGER, "item_id", int.class, "itemId"));
type.setValueFields(
new JdbcTypeField(Types.VARCHAR, "name", String.class, "name"),
new JdbcTypeField(Types.VARCHAR, "brand", String.class, "brand"),
new JdbcTypeField(Types.VARCHAR, "type", String.class, "type"),
new JdbcTypeField(Types.VARCHAR, "manufacturer", String.class, "manufacturer"),
new JdbcTypeField(Types.VARCHAR, "description", String.class, "description")
);
return type;
}
客户
的模型类package infoh415.project.model;
import java.io.Serializable;
/**
* Customer definition.
*
* This file was generated by Ignite Web Console (11/26/2017, 10:52)
**/
public class Customer implements Serializable {
/** */
private static final long serialVersionUID = 0L;
private int customerId;
/** Value for firstName. */
private String firstName;
/** Value for lastName. */
private String lastName;
/** Value for address. */
private String address;
public Customer(String firstName, String lastName, String address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public Customer(int customerId, String firstName, String lastName, String address) {
this.customerId = customerId;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
/**
* Gets firstName
*
* @return Value for firstName.
**/
public String getFirstName() {
return firstName;
}
/**
* Sets firstName
*
* @param firstName New value for firstName.
**/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets lastName
*
* @return Value for lastName.
**/
public String getLastName() {
return lastName;
}
/**
* Sets lastName
*
* @param lastName New value for lastName.
**/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Gets address
*
* @return Value for address.
**/
public String getAddress() {
return address;
}
/**
* Sets address
*
* @param address New value for address.
**/
public void setAddress(String address) {
this.address = address;
}
/** {@inheritDoc} **/
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Customer))
return false;
Customer that = (Customer)o;
if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null)
return false;
if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null)
return false;
if (address != null ? !address.equals(that.address) : that.address != null)
return false;
return true;
}
/** {@inheritDoc} **/
@Override public int hashCode() {
int res = firstName != null ? firstName.hashCode() : 0;
res = 31 * res + (lastName != null ? lastName.hashCode() : 0);
res = 31 * res + (address != null ? address.hashCode() : 0);
return res;
}
/** {@inheritDoc} **/
@Override public String toString() {
return "Customer [" +
"firstName=" + firstName + ", " +
"lastName=" + lastName + ", " +
"address=" + address +
"]";
}
}
项目
的模型类package infoh415.project.model;
import java.io.Serializable;
/**
* Item definition.
*
* This file was generated by Ignite Web Console (11/26/2017, 10:52)
**/
public class Item implements Serializable {
/** */
private static final long serialVersionUID = 0L;
private int itemId;
/** Value for name. */
private String name;
/** Value for brand. */
private String brand;
/** Value for type. */
private String type;
/** Value for manufacturer. */
private String manufacturer;
/** Value for description. */
private String description;
public Item(String name, String brand, String type, String manufacturer, String description) {
this.name = name;
this.brand = brand;
this.type = type;
this.manufacturer = manufacturer;
this.description = description;
}
public Item(int itemId, String name, String brand, String type, String manufacturer, String description) {
this.itemId = itemId;
this.name = name;
this.brand = brand;
this.type = type;
this.manufacturer = manufacturer;
this.description = description;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
/**
* Gets name
*
* @return Value for name.
**/
public String getName() {
return name;
}
/**
* Sets name
*
* @param name New value for name.
**/
public void setName(String name) {
this.name = name;
}
/**
* Gets brand
*
* @return Value for brand.
**/
public String getBrand() {
return brand;
}
/**
* Sets brand
*
* @param brand New value for brand.
**/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* Gets type
*
* @return Value for type.
**/
public String getType() {
return type;
}
/**
* Sets type
*
* @param type New value for type.
**/
public void setType(String type) {
this.type = type;
}
/**
* Gets manufacturer
*
* @return Value for manufacturer.
**/
public String getManufacturer() {
return manufacturer;
}
/**
* Sets manufacturer
*
* @param manufacturer New value for manufacturer.
**/
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
/**
* Gets description
*
* @return Value for description.
**/
public String getDescription() {
return description;
}
/**
* Sets description
*
* @param description New value for description.
**/
public void setDescription(String description) {
this.description = description;
}
/** {@inheritDoc} **/
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Item))
return false;
Item that = (Item)o;
if (name != null ? !name.equals(that.name) : that.name != null)
return false;
if (brand != null ? !brand.equals(that.brand) : that.brand != null)
return false;
if (type != null ? !type.equals(that.type) : that.type != null)
return false;
if (manufacturer != null ? !manufacturer.equals(that.manufacturer) : that.manufacturer != null)
return false;
if (description != null ? !description.equals(that.description) : that.description != null)
return false;
return true;
}
/** {@inheritDoc} **/
@Override public int hashCode() {
int res = name != null ? name.hashCode() : 0;
res = 31 * res + (brand != null ? brand.hashCode() : 0);
res = 31 * res + (type != null ? type.hashCode() : 0);
res = 31 * res + (manufacturer != null ? manufacturer.hashCode() : 0);
res = 31 * res + (description != null ? description.hashCode() : 0);
return res;
}
/** {@inheritDoc} **/
@Override public String toString() {
return "Item [" +
"name=" + name + ", " +
"brand=" + brand + ", " +
"type=" + type + ", " +
"manufacturer=" + manufacturer + ", " +
"description=" + description +
"]";
}
}
有人可以解释一下&#34;类org.apache.ignite.IgniteCheckedException:未知对[platformId = 0,typeId = 123254525]&#34;错误意味着如何跟踪这个问题的原因 - 我已经尝试在调试模式下单步执行Ignite代码,我知道在MarshallerContextImpl#getClassName第一行MappedName mappedName = cache.get(typeId);为Item产生null。但我不明白为什么。任何帮助,将不胜感激!!谢谢。
更新: 使用的点燃版本:ignite-core 2.2.0
更新:loadCache的内容:
private static void loadCache(IgniteCache<Integer, Item> cache) {
long start = System.currentTimeMillis();
// Start loading cache from persistent store on all caching nodes.
// cache.loadCache(null, ENTRY_COUNT);
cache.loadCache(null);
long end = System.currentTimeMillis();
System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms.");
}
Item表的表定义:
create table Item
(
item_id int not null
primary key,
name varchar(50) not null,
brand varchar(20) not null,
type varchar(20) not null,
manufacturer varchar(30) not null,
description varchar(200)
)
答案 0 :(得分:1)
我可以在next
中添加一些与缓存中的预期项完全不同的内容时重现它:
loadCaches()
请分享您的private void loadCache(IgniteCache<Integer, Item> cache, /* Ignite.binary() */ IgniteBinary binary) {
// Note the absence of package name here:
BinaryObjectBuilder builder = binary.builder("Item");
builder.setField("name", "a");
builder.setField("brand", "B");
builder.setField("type", "c");
builder.setField("manufacturer", "D");
builder.setField("description", "e");
builder.setField("itemId", 1);
cache.withKeepBinary().put(1, builder.build());
}
方法进行详细审核。