Question

I have following Objectify entity to store data in Google DataStore.

public class Record implements Serializable {

    private static final long serialVersionUID = 201203171843L;
    @Id  
    private Long id;                
    private String name;            // John Smith
    private Integer age;            // 43
    private String gender;          // Male/Female
    private String eventName;       // Name of the marathon/event
    private String eventCityName;   // City of the event
    private String eventStateName;  // State of the event
    private Date eventDate;         // event date

    //Getters & Setters
}

Now, my question is how can I query my database to get count of Records for a given eventName or event City+State? Or get a list of all City+Name.

Was it helpful?

Solution

On App Engine counting is very expensive: you basically need to query with certain condition (eventName = something), then count all results. The cost is a key-only query (1 read + 1 small operation) and increases with number of entities counted. For example counting 1 million entities would cost $0.8.

What is normally done is to keep count of things as a property inside a dedicated entity: increase the property value when count goes up (entity added) and decrease when it goes down (entity deleted).

If you plan to do this on a larger scale then understand there is a write/update limitation of about 5 writes/s per entity (entity group actually). See sharded counters for how to work around this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top