It is possible to update datastore entities or delete entities of a Kind?

141 views
Skip to first unread message

Juan de Dios Becerra

unread,
Feb 5, 2014, 11:35:05 AM2/5/14
to google-a...@googlegroups.com

I would like to make some kind of dashboard about certain information that has to be updated every week approximately, but I would like to update the actual registers not to add more, I have not found a good example of how to update datastore, so I am doing manually delete the entities and then insert again I know is completely awful for this reason I think it could be a better way, there exists?
Btw I am using Java.

Vinny P

unread,
Feb 5, 2014, 8:01:49 PM2/5/14
to google-a...@googlegroups.com
On Wed, Feb 5, 2014 at 10:35 AM, Juan de Dios Becerra <j.bece...@gmail.com> wrote:
I have not found a good example of how to update datastore, so I am doing manually delete the entities and then insert again I know is completely awful for this reason I think it could be a better way, there exists? Btw I am using Java.
 

Hi Juan,

In the App Engine datastore, an update operation is the same as an insert operation; if you insert an entity with the same Kind and ID/Name as a currently existing entity, the pre-existing entity will be overwritten with information from the new entity. Also, the indexes will be automatically updated to reflect the new values. 

So you can simply reuse the code that is currently doing the entity insertion. Just remember to set the same Kind and ID/Name as the entity you want to overwrite. If you don't know the current IDs in use, you can query for the entities and extract the IDs.
 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com

Juan de Dios Becerra

unread,
Feb 6, 2014, 6:06:05 PM2/6/14
to google-a...@googlegroups.com
How can I set the ID for an entity? for inserting I create a Key and an entity:

Key collectionData = KeyFactory.createKey("FinalCount", "FinalCollection");
   Entity countData = new Entity("Final", collectionData);

but for set the ID I don't know how, do you have examples?

Thank you.

PK

unread,
Feb 6, 2014, 6:43:51 PM2/6/14
to google-a...@googlegroups.com, Juan de Dios Becerra
There is a get() method where you pass the key and get the entity back. This is described in the docs here: 


Retrieving an entity

To retrieve an entity identified by a given key, pass the Key object to the DatastoreService.get() method:

// Key employeeKey = ...;
Entity employee = datastore.get(employeeKey);

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/groups/opt_out.

Juan de Dios Becerra

unread,
Feb 7, 2014, 10:22:10 AM2/7/14
to google-a...@googlegroups.com, Juan de Dios Becerra
Yes, but I don't see where to associate the ID, when I create my entities I use only one key for all the entities, see this snippet:

Key activityData = KeyFactory.createKey("Activity", "ActivityData");

for(int a = 0 a < activities.size(); a++){
    Entity activityDesglose = new Entity("Activities", activityData);
    activityDesglose.setProperty("publishDate", date);
    activityDesglose.setProperty("title", activities.get(i).getTitle());
    activityDesglose.setProperty("type", activities.get(i).getVerb());
}

I don't know if I explain my point correctly, where or how con I use the ID to get the correct entity.

PK

unread,
Feb 7, 2014, 12:10:41 PM2/7/14
to google-a...@googlegroups.com, Juan de Dios Becerra, Juan de Dios Becerra
If you are using one and the same key, you are creating only one entity which you keep overwriting. Go to the console and look at the entities in the datastore. By the way I do not see a put() in this code

Juan de Dios Becerra

unread,
Feb 10, 2014, 12:23:00 PM2/10/14
to google-a...@googlegroups.com, Juan de Dios Becerra
It seems like I finally could retrieve an entity and modify the content, my actual doubt is how can I retrieve more than 1,000 entities, because I got a warning because I have more than 1,000 entitites in a kind, how can I make this?

Vinny P

unread,
Feb 10, 2014, 6:07:08 PM2/10/14
to google-a...@googlegroups.com
On Mon, Feb 10, 2014 at 11:23 AM, Juan de Dios Becerra <j.bece...@gmail.com> wrote:
It seems like I finally could retrieve an entity and modify the content, my actual doubt is how can I retrieve more than 1,000 entities, because I got a warning because I have more than 1,000 entitites in a kind, how can I make this?



You'll need to use datastore cursors to "page" through sets of query results.

You can see an example of how to use cursors here: https://developers.google.com/appengine/docs/java/datastore/queries#Java_Query_cursors, but the general idea is that you can retrieve the results of a datastore query in batches. When you're done processing a page of results, you can obtain a cursor to retrieve the next results page.

Admin iBlogee

unread,
Feb 7, 2014, 12:11:30 PM2/7/14
to google-a...@googlegroups.com
Posting Again
I have a simply java web app. It parses some non-english text and display
them using header "Content-Type", "text/plain; charset=UTF-8". It shows all
text properly in local google app engine. However when I deploy at remote
engine, I can see all ????

Servlet Code: resp.setHeader("Content-Type", "text/plain; charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println(jsonText);
Local app Display: ਅਜੀਤ : ਤਾਜ਼ਾ ਖ਼ਬਰਾਂ - ਸੜਕ ਹਾਦਸੇ \\'ਚ ਇੱਕੋ ਪਰਿਵਾਰ ਦੇ ਚਾਰ
ਮੈਂਬਰਾਂ ਦੀ ਮੌਤ
Remote app display: ???????????? : ??????????????? ?????????????????? -
????????? ???????????????

Any reason for different behavior?

Admin iBlogee.com

unread,
Feb 10, 2014, 1:24:01 AM2/10/14
to google-a...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages