Wednesday, November 1, 2017

[google-cloud-sql-discuss] Re: Migrate from Cloud SQL to DataStore

You are correct, if you require scalability and speed at lower costs (as seen in the pricing calculator) it is recommended to use the Datastore. The Datastore scales to handle any load, meaning any number of your users can be interacting with the Datastore at any given time and it will continue to be fast. Because of the optimized structure of the Datastore you are forced to use APIs instead of running SQL queries. 

- Every Datastore 'Kind' (aka table) contains 'Entities' (aka rows). You would have three 'Kinds' in the Datastore, 'User', 'Post', and 'Comment'. You can think of these likes classes where the 'Entity' is an object of that class (e.g a 'User' entity). 

- When you save an entity to the Datastore you give it key. The key specifies the Kind to save it to, the ancestors it has (aka the keys of its parent 'Post' and 'User' entities), and the ID of the actual entity. The actual entity ID can be auto-generated by the Datastore or specified by you. For example:

KeyFactory keyFactory = datastore.newKeyFactory()
   
.addAncestors(PathElement.of("User", "u123"), PathElement.of("Post", "p098"))
   
.setKind("Comment");
Key commentKey = keyFactory.newKey("m392");

Entity comment = Entity.newBuilder(commentKey)
   
.set("comment", "Some Text")
   
.build();

datastore
.put(comment);


- Your comment in this example would now only have one text property called 'comment' and one Datastore key which contains the keys of its ancestors and its own unique ID. You can then directly ask Datastore for all the child Comment entities of a given Post entity just by using the Post's key. For example:

Query<Entity> comments = Query.newEntityQueryBuilder()
   
.setKind("Comment")
   
.setFilter(PropertyFilter.hasAncestor(
        datastore
.newKeyFactory().addAncestors(PathElement.of("User", "u123"))
           
.setKind("Post").newKey("p098")))
   
.build();

QueryResults<Entity> results = datastore.run(comments);


You can think of the actual keys in Datastore as the JOINs that provide relations between Kinds. Of course you will need to create a script that migrates your current data into the Datastore, but once it is migrated your queries will be very fast. Behind the scenes Datastore builds indexes (aka tables) that already contains the answers to your queries. If you require more complex queries with sort orders you must build your own index configuration file

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/9a355ddd-3fcd-4089-b1df-4d950daae8d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment