With the exception of the SampleBean, here is a complete example that works in
my environment. The server has no cache configuration.
try (Ignite ignite = Ignition.start(AppConfiguration.getConfiguration())) {
LinkedHashMap<String,String> fields = new LinkedHashMap<>();
fields.put("uid", Integer.class.getName());
fields.put("attr1", String.class.getName());
fields.put("attr2", String.class.getName());
CacheConfiguration<Integer,SampleBean> cacheConfiguration = new
CacheConfiguration<>();
cacheConfiguration.setName("FUTURE_TEST_CACHE")
.setQueryEntities(Arrays.asList(
new QueryEntity()
.setKeyType(Integer.class.getName())
.setKeyFieldName("uid")
.setValueType("stackoverflow.SampleBean")
.setFields(fields)
.setIndexes(Arrays.asList(new QueryIndex("attr1")))
));
IgniteCache<Integer,SampleBean> cc =
ignite.getOrCreateCache(cacheConfiguration);
for (int i = 1; i < 10; i++) {
SampleBean bean = new SampleBean();
bean.setUid(100 + i);
bean.setAttr1("attribute1 10" + i);
bean.setAttr2("attribute2 10" + i);
cc.put(bean.getUid(), bean);
}// 1. initialized cache data by KV-mode
System.out.println("ignite cache putting 10 element over.");
SampleBean bean = cc.get(105);// 1.a load by KV mode
System.out.format("Load from cache: '%s'.\n", bean);
cc.query(new SqlFieldsQuery("insert into SampleBean(uid, attr1, attr2)
values(?, ?, ?) ").setArgs(206, "attribute1 106", "attribute2 106"))
.getAll();// 2.a. Insert first
List list = cc.query(new SqlFieldsQuery("select * from SampleBean where
attr1 like 'attribute1%'"))
.getAll();// 3. Here, if using "2.a"can get result; otherwise
only with "1" to initialize, will be empty list ever.
System.out.printf("query from cache is: %s.\n", list);
}
}
In the SQL insert, I change the key from 106 to 206 to avoid a duplicate key.
The query returns all the rows you’d expect.
Regards,
Stephen
> On 28 May 2021, at 01:58, tankmarshal <[email protected]> wrote:
>
> Do you mean the key type declared in method SampleBean.entityDescribe()?
>
> While I'm trying to use c/s mode, application client didn't pass this
> method return-value to the ignite-sever in actually running. Cache keyType
> is configured in "server-config.xml".
>
> And also I've tried what you said:
> 1)changing SampleBean.entityDescribe() keyType to
> "java.lang.Integer",
> 2)in IgniteClients add code :
> ClientCacheConfiguration ccc = new
> ClientCacheConfiguration()
> .setName("FUTURE_TEST_CACHE_")
>
> .setQueryEntities(SampleBean.entityDescribe());
> ClientCache<Integer, SampleBean> cc =
> client.getOrCreateCache(ccc);
> // Others the same with old version.....
>
> I got the same result: cc.get() can find value, but cc.query("select *
> from SampleBean") still got none result.
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/