You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by takumi <bl...@kss.biglobe.ne.jp> on 2018/03/07 15:40:03 UTC

How to use SqlQuery for multi tables

I want to use sql "select" from 2 caches.

For example.
  SqlQuery sql = new SqlQuery(cache1, "select a.* from cache1 a, cache2 b
where a.id = b.id");
  QueryCursor<Entry&lt;String, BinaryObject>> results = cache.query(sql);

  ※ cache1, cache2 have BinaryObject Data.
      I call QueryEntity#addQueryField("id", String.class.getName(), "id").

When above-mentioned sql execute, exception occured. It is reason cache2 is
not found.
SqlQuery has the 1st parameter which is target cache' name (=cache1).
So cache2 is not found.
How I use sql from 2 tables (cache1 and cache2)?




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

RE: How to use SqlQuery for multi tables

Posted by Stanislav Lukyanov <st...@gmail.com>.
You don’t have to explicitly set sqlSchema for a cache – the default value is the cache name.

Both cache1.query() and cache2.query() can access the data in both caches,
the only difference is the default schema name that will be used in the query.

If both caches have the same schema name or if you always specify schema explicitly in SQL query,
there is no difference in which one to use.

Thanks,
Stan

From: takumi
Sent: 8 марта 2018 г. 18:02
To: user@ignite.apache.org
Subject: RE: How to use SqlQuery for multi tables

I solved my self.
It was the cause that I didn't set to SqlSchema of CacheConfigration.

Please tell me in connection with this.
I don't know which instance query() method should be called.

If it is an instance of the same schema, whichever one I am using will not
change?




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/


RE: How to use SqlQuery for multi tables

Posted by takumi <bl...@kss.biglobe.ne.jp>.
I solved my self.
It was the cause that I didn't set to SqlSchema of CacheConfigration.

Please tell me in connection with this.
I don't know which instance query() method should be called.

If it is an instance of the same schema, whichever one I am using will not
change?




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

RE: How to use SqlQuery for multi tables

Posted by takumi <bl...@kss.biglobe.ne.jp>.
Thank you for your reply.

In addition to this, I want to know how to use SqlFieldsQuery instead of
SqlQuery.
I would like you to teach me about that.

My code is here.

--------------------------------------
public class Main {
	private static final String CACHE_NAME1 = "binaryCache1";
	private static final String CACHE_NAME2 = "binaryCache2";

	public static void main(String[] args) {
        IgniteConfiguration cfg = new IgniteConfiguration();

        BinaryConfiguration bc = new BinaryConfiguration();
        bc.setCompactFooter(false);
        cfg.setBinaryConfiguration(bc);

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

        CacheConfiguration<String, BinaryObject> cc1 = new
CacheConfiguration<String,
BinaryObject>(CACHE_NAME1).setStoreKeepBinary(true);
        CacheConfiguration<String, BinaryObject> cc2 = new
CacheConfiguration<String,
BinaryObject>(CACHE_NAME2).setStoreKeepBinary(true);

        List<QueryEntity> entities = new ArrayList<>();
        QueryEntity qe = new QueryEntity(String.class.getName(),
CACHE_NAME1);
        qe.addQueryField("prop1", Double.class.getName(), "prop1");
        entities.add(qe);
        cc1.setQueryEntities(entities);

        List<QueryEntity> entities2 = new ArrayList<>();
        QueryEntity qe2 = new QueryEntity(String.class.getName(),
CACHE_NAME2);
        qe2.addQueryField("prop1", Double.class.getName(), "prop1");
        entities2.add(qe2);
        cc2.setQueryEntities(entities2);

        IgniteCache<String, BinaryObject> cache1 =
ignite.getOrCreateCache(cc1).withKeepBinary();
        IgniteCache<String, BinaryObject> cache2 =
ignite.getOrCreateCache(cc2).withKeepBinary();
        setData(ignite, cache1, cache2);


    	QueryCursor<Entry&lt;String, BinaryObject>> results1 = cache1.query(new
SqlQuery(CACHE_NAME1, "select a.* from " + CACHE_NAME1 + " a"));
    	for(Entry<String, BinaryObject> result : results1) {
    		System.out.println("result :" +result.getKey() + "," +
result.getValue());
    	}

    	QueryCursor<Entry&lt;String, BinaryObject>> results2 = cache2.query(new
SqlQuery(CACHE_NAME2, "select b.* from " + CACHE_NAME2 + " b"));
    	for(Entry<String, BinaryObject> result : results2) {
    		System.out.println("result :" +result.getKey() + "," +
result.getValue());
    	}

// this line throws exception
    	QueryCursor<Entry&lt;String, BinaryObject>> results3 = cache1.query(new
SqlQuery(CACHE_NAME1, "select a.* from " + CACHE_NAME1 + " a," + CACHE_NAME2
+ " b where a.prop1 = b.prop1"));
    	for(Entry<String, BinaryObject> result : results3) {
    		System.out.println("result :" +result.getKey() + "," +
result.getValue());
    	}

	}

    private static void setData(Ignite ignite, IgniteCache<String,
BinaryObject> cache1, IgniteCache<String, BinaryObject> cache2) {
    	cache1.put("device1",
ignite.binary().builder(CACHE_NAME1).setField("prop1", 50.0,
Double.class).build());
    	cache1.put("device2", ignite.binary().builder(CACHE_NAME1)
.setField("prop1", 10.0, Double.class).build());
    	cache2.put("device1", ignite.binary().builder(CACHE_NAME2)
.setField("prop1",  1.0, Double.class).build());
    	cache2.put("device2", ignite.binary().builder(CACHE_NAME2)
.setField("prop1", 10.0, Double.class).build());
    }
}



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

RE: How to use SqlQuery for multi tables

Posted by Stanislav Lukyanov <st...@gmail.com>.
Hi,

When you create a cache via Java API you provide a value type to the QueryEntity.
Then the table name for SQL queries will have a form <cache_name>.<value_type_name>.

Can you share the code that you use to create the caches?
Then it would be easier to tell what will be the exact names of the tables.

Thanks,
Stan


From: takumi
Sent: 7 марта 2018 г. 18:40
To: user@ignite.apache.org
Subject: How to use SqlQuery for multi tables

I want to use sql "select" from 2 caches.

For example.
  SqlQuery sql = new SqlQuery(cache1, "select a.* from cache1 a, cache2 b
where a.id = b.id");
  QueryCursor<Entry&lt;String, BinaryObject>> results = cache.query(sql);

  ※ cache1, cache2 have BinaryObject Data.
      I call QueryEntity#addQueryField("id", String.class.getName(), "id").

When above-mentioned sql execute, exception occured. It is reason cache2 is
not found.
SqlQuery has the 1st parameter which is target cache' name (=cache1).
So cache2 is not found.
How I use sql from 2 tables (cache1 and cache2)?




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/