javagriddb

Parameter binding issue in GridDB Java API query returns empty result set


I am working on a project using GridDB's Java API and I am facing an issue with parameterized queries. I created a container with two columns (an integer column id and a string column value), inserted some sample data, and then attempted to retrieve records using a parameterized query. However, when I bind a parameter (for example, to fetch all records where id >= 5), the query unexpectedly returns an empty result set—even though I have matching records in the container.

My code:

import com.toshiba.mwcloud.gs.*;
import java.util.*;

public class GridDBExample {
    public static void main(String[] args) {
        try {
            // Establish connection to GridDB
            GridStore store = GridStoreFactory.getInstance().getStore(
                "notificationAddress=239.0.0.1;notificationPort=31999;clusterName=defaultCluster;user=admin;password=admin;"
            );
            
            // Define container schema and create container
            ContainerInfo containerInfo = new ContainerInfo(
                "sample_container",
                ContainerType.COLLECTION,
                new String[]{"id", "value"},
                new Type[]{Type.INTEGER, Type.STRING}
            );
            Collection con = store.putContainer(containerInfo);
            
            // Insert sample data
            for (int i = 0; i < 10; i++) {
                con.put(new Object[]{i, "value" + i});
            }
            
            // Parameterized query: expecting to retrieve rows where id >= 5
            Query<Row> query = con.query("SELECT * WHERE id >= ?");
            query.bind(5);
            RowSet<Row> rs = query.fetch();
            
            // Iterate and print results
            while (rs.hasNext()) {
                Row row = rs.next();
                System.out.println("id: " + row.getInteger(0) + ", value: " + row.getString(1));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I am using the latest version of the GridDB Java API (if there's a specific version, please mention it in your comments).


Solution

  • To fix these errors, I updated My code as follows:
    Missing Import for Date Class
    The Date class is not recognized because it is not imported. (import java.util.Date;)

    Undefined TimeSeries Class
    The TimeSeries class is missing an import statement. (import com.toshiba.mwcloud.gs.TimeSeries;)

    package gsSample;
    import java.util.Arrays;
    import java.util.Properties;
    import java.util.Date;  // Import Date class
    
    import com.toshiba.mwcloud.gs.Collection;
    import com.toshiba.mwcloud.gs.GSException;
    import com.toshiba.mwcloud.gs.GridStore;
    import com.toshiba.mwcloud.gs.GridStoreFactory;
    import com.toshiba.mwcloud.gs.Query;
    import com.toshiba.mwcloud.gs.RowKey;
    import com.toshiba.mwcloud.gs.RowSet;
    import com.toshiba.mwcloud.gs.TimeSeries;  // Import TimeSeries class
    
    public class MyApp2 {
        // Container schema
        static class Person {
            @RowKey String name;
            int age;
        }
    
        static class HeartRate {
            @RowKey Date ts;
            int heartRate;  // Correct field name
            String activity;
        }
    
        public static void main(String[] args) throws GSException {
    
            // Create a Properties instance to connect to GridDB
            Properties props = new Properties();
            props.setProperty("notificationAddress", "239.0.0.1");
            props.setProperty("notificationPort", "31999");
            props.setProperty("clusterName", "defaultCluster");
            props.setProperty("user", "administrator");
            props.setProperty("password", "password");
    
            GridStore store = GridStoreFactory.getInstance().getGridStore(props);
    
            // Create a person object (fix person1 issue)
            Person person1 = new Person();
            person1.name = "JohnDoe";  // Assign a valid name
            person1.age = 30;
    
            // Get the container to write records
            Collection<String, Person> people = store.putCollection("PEOPLE", Person.class);
            people.put(person1);  // Add a person record
    
            // Write a record
            HeartRate hr = new HeartRate();
            hr.ts = new Date();  // Fix Date issue
            hr.heartRate = 60;  // Fix case-sensitive issue
            hr.activity = "resting";
    
            // Create TimeSeries and insert heart rate data
            TimeSeries<HeartRate> heartRate = store.putTimeSeries("HR_" + person1.name, HeartRate.class);
            heartRate.put(hr);
        }
    }