How to use BinaryObject Cache API to query a table created from JDBC?

classic Classic list List threaded Threaded
2 messages Options
Ray Ray
Reply | Threaded
Open this post in threaded view
|

How to use BinaryObject Cache API to query a table created from JDBC?

Let's say I create a table from jdbc client using this command.

create table test(a varchar, b varchar, c varchar,primary key(a,b));

I inserted one record in this table.

 select * from test;
+--------------------------------+--------------------------------+--------------------------------+
|               A                |               B                |              
C                |
+--------------------------------+--------------------------------+--------------------------------+
| 8                              | 8                              | 9                            
|
+--------------------------------+--------------------------------+--------------------------------+

How can I query this table using BinaryObject Cache API?

I tried the code below, but it returns null for object o.

public class TestKey {

    private String a;

    private String b;

    public TestKey() {
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }
}



        TestKey tk = new TestKey();
        tk.setA("8");
        tk.setB("8");
        Object o = ignite.cache("SQL_PUBLIC_TEST").get(tk);



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
aealexsandrov aealexsandrov
Reply | Threaded
Open this post in threaded view
|

Re: How to use BinaryObject Cache API to query a table created from JDBC?

Hi,

Try the next example and follow the same way for your:

        // Open the JDBC connection.
        try {
            Connection conn =
DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1;user=user;password=password");
//remove user password if you don't have security

            conn.createStatement().execute("CREATE TABLE IF NOT EXISTS
Person(\n" +
                "  id int,\n" +
                "  city_id int,\n" +
                "  name varchar,\n" +
                "  age int, \n" +
                "  company varchar,\n" +
                "  PRIMARY KEY (id, city_id)\n" +
                ") WITH
\"template=partitioned,backups=1,affinity_key=city_id, key_type=PersonKey,
value_type=Person\";"); //IMPORTANT add key_type and value_type

            conn.createStatement().execute("INSERT INTO Person (id, name,
city_id) VALUES (1, 'John Doe1', 3);");
            conn.createStatement().execute("INSERT INTO Person (id, name,
city_id) VALUES (2, 'John Doe2', 3);");
            conn.createStatement().execute("INSERT INTO Person (id, name,
city_id) VALUES (3, 'John Doe3', 3);");
            conn.createStatement().execute("INSERT INTO Person (id, name,
city_id) VALUES (4, 'John Doe4', 3);");
            conn.createStatement().execute("INSERT INTO Person (id, name,
city_id) VALUES (5, 'John Doe5', 3);");
            conn.createStatement().execute("INSERT INTO Person (id, name,
city_id) VALUES (6, 'John Doe6', 3);");
            conn.createStatement().execute("INSERT INTO Person (id, name,
city_id) VALUES (7, 'John Doe7', 3);");

            conn.close();

            BinaryObject key = ignite.binary().builder("PersonKey") //same
as in key type
                .setField("id", 1)
                .setField("city_id", 3)
                .build();

            IgniteCache<BinaryObject, BinaryObject> cache1 =
ignite.getOrCreateCache("SQL_PUBLIC_PERSON").withKeepBinary();

            BinaryObject value = cache1.get(key);

            System.out.println("Name is " + value.field("name"));
        }
        catch (SQLException e) {
            e.printStackTrace();

Output is:

Name is John Doe1

BR,
Andrei
       



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