You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by tankmarshal <ta...@163.com> on 2021/05/27 00:49:14 UTC
Can ignite kv put data, sql query data in C/S mode?
with ignite 2.10.0 C/S deploy mode, I'm trying to mix using K/V and sql query
in a certain cache. Ignite server config like
this(cacheconfiguration-queryEntities attribute):
<property name="queryEntities">
<list>
<bean
class="org.apache.ignite.cache.QueryEntity">
<property name="keyType"
value="java.lang.Integer" />
<property name="keyFieldName" value="uid"/>
<property name="valueType"
value="com.chinacnd.cdc_idm.IgniteClients.SampleBean" />
<property name="fields">
<map>
<entry key="uid"
value="java.lang.Integer" />
<entry key="attr1"
value="java.lang.String" />
<entry key="attr2"
value="java.lang.String" />
</map>
</property>
<property name="indexes">
<list>
<bean
class="org.apache.ignite.cache.QueryIndex">
<constructor-arg value="attr1"/>
</bean>
</list>
</property>
</bean>
</list>
</property>
And after ignite server started, I run client code like:
try (IgniteClient client = Ignition.startClient(cfg)) {
ClientCache<Integer, SampleBean> cc =
client.getOrCreateCache("FUTURE_TEST_CACHE_");
System.out.format("ignite cache '%s' created....\n", cc);
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(106, "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);
// cc.clear();
}
Note that if like the post code, I comment "2.a" code, then query will
never get result put by "1.a". cann't we mix using K/V put and sql query in
the same cache? I read the "User Guide" word by word again and again: only
see example that under the server-server mode, mix using K/V and sql query.
--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by tankmarshal <ta...@163.com>.
It works. Thank you.
--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by Stephen Darlington <st...@gridgain.com>.
You have the wrong data type in your configuration file.
In your XML file it says:
<property name="valueType" value="com.shawn.ignites_test.IgniteClients.SampleBean"/>
It should say:
<property name="valueType" value="com.shawn.ignites_test.IgniteClients$SampleBean" />
> On 31 May 2021, at 08:54, tankmarshal <ta...@163.com> wrote:
>
> So, as you say, there is something wrong with the ignite server-config.xml?
>
> But I review the server-config.xml, compare to Ignite Guide Book again, It's
> still puzzles me that where is wrong in the xml?
>
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by tankmarshal <ta...@163.com>.
So, as you say, there is something wrong with the ignite server-config.xml?
But I review the server-config.xml, compare to Ignite Guide Book again, It's
still puzzles me that where is wrong in the xml?
--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by Stephen Darlington <st...@gridgain.com>.
I was running as a thick client.
This also works:
// Create cache
Ignition.setClientMode(true);
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);
}
// Access cache
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setAddresses("127.0.0.1:10800");
try (IgniteClient igniteClient = Ignition.startClient(clientConfiguration)) {
ClientCache<Integer,SampleBean> cc = igniteClient.cache("FUTURE_TEST_CACHE");
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);
}
}
> On 28 May 2021, at 10:43, tankmarshal <ta...@163.com> wrote:
>
> Yes, I know, but you may have a mistake: with your code, your application run
> as a ignite server, too.
>
> I was trying the scene that Ignite running as a server, and application just
> running in client-mode. So, client have no need to set queryEntity
> attributes, cause it's already configured in the server config file.
>
> you may try the scene, do a test.
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by tankmarshal <ta...@163.com>.
Yes, I know, but you may have a mistake: with your code, your application run
as a ignite server, too.
I was trying the scene that Ignite running as a server, and application just
running in client-mode. So, client have no need to set queryEntity
attributes, cause it's already configured in the server config file.
you may try the scene, do a test.
--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by Stephen Darlington <st...@gridgain.com>.
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 <ta...@163.com> 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/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by tankmarshal <ta...@163.com>.
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/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by Stephen Darlington <st...@gridgain.com>.
You tell the query entity that your key field is a String but it’s actually an integer.
entity.setKeyType("java.lang.String");
Should be:
entity.setKeyType(Integer.class.getName());
> On 27 May 2021, at 02:20, tankmarshal <ta...@163.com> wrote:
>
> full config file, and test code will be put here.
> server-config2.xml
> <http://apache-ignite-users.70518.x6.nabble.com/file/t3162/server-config2.xml>
> IgniteClients.java
> <http://apache-ignite-users.70518.x6.nabble.com/file/t3162/IgniteClients.java>
>
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
Re: Can ignite kv put data, sql query data in C/S mode?
Posted by tankmarshal <ta...@163.com>.
full config file, and test code will be put here.
server-config2.xml
<http://apache-ignite-users.70518.x6.nabble.com/file/t3162/server-config2.xml>
IgniteClients.java
<http://apache-ignite-users.70518.x6.nabble.com/file/t3162/IgniteClients.java>
--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/