Add, Update, Delete from a ndb.KeyProperty() - Google Cloud Datastore NDB - google-cloud-datastore

Here are my many-to-many relationship models:
class ModelA(ndb.Model):
name = ndb.StringProperty(required=true)
model_b = ndb.KeyProperty(kind=ModelB,repeated=True)
class ModelB(ndb.Model):
name = ndb.StringProperty(required=true)
model_a = ndb.KeyProperty(kind=ModelA,repeated=True)
My question is, how do I add/update/delete a single (or many) KeyProperty from let's say model_b?

I managed to do it like this:
pos = ModelA.model_b.index(ndb.Key('ModelB',213)) # Get position from list
ModelA.model_b.pop(pos) # Remove from list
ModelA.put() # Update

Related

Telegraf JSON v2 Topic Parsing

I want to combine the topic to a single field. Currently I am trying this:
data_format = "json_v2"
[[inputs.mqtt_consumer.json_v2]]
[[inputs.mqtt_consumer.topic_parsing]]
topic = "+/+/+/"
tags = "name/id/value"
fields = "name/id/_"
[[inputs.mqtt_consumer.json_v2.tag]]
path = "timestamp"
But it will split it into name and id, but it should combine both to a new string and save that one.

i can't do makemigrations in django 1.11 (fields.E312)

class Player(models.Model):
name = models.CharField(max_length = 256)
number = models.IntegerField()
age = models.IntegerField()
is_captain = models.BooleanField(default = False)
class Injuries(models.Model):
player = models.ForeignKey(Player, Player.name)
team = models.ForeignKey(Team)
Below are my suggestions, as per my capability.
As per the documentation,
A many-to-one relationship. Requires two positional arguments: the
class to which the model is related and the on_delete option.
See the reference here. So the first positional argument is the class which is related by Foreign Key and the second position argument is on_delete. You should define it as models.CASCADE or as appropriate for your app.
Thus in this case the second positional argument is "Player.name". I think you have to first replace it with models.CASCADE.
Thus change the code from what is below
class Injuries(models.Model):
player = models.ForeignKey(Player, Player.name)
team = models.ForeignKey(Team)
to the one below
class Injuries(models.Model):
player = models.ForeignKey(Player, on_delete=models.CASCADE)
team = models.ForeignKey(Team)
The foreign key is set to the primary key of the related model. In this case (since you have not defined it), it will be player_id that is automatically assigned by django. If you want it to force it to use the "name" column from the Player model, then you have to set "unique=True" in the name field of the Player model. Also notice the use of to_field='name' option.
In such a case the changed code will be as below.
class Player(models.Model):
name = models.CharField(max_length = 256, unique=True)
number = models.IntegerField()
age = models.IntegerField()
is_captain = models.BooleanField(default = False)
class Injuries(models.Model):
player = models.ForeignKey(Player, to_field='name', on_delete=models.CASCADE)
team = models.ForeignKey(Team)
Hopefully it will work. If there is still an error, please let me know.

How do I perform an NDB query on the properties of models that have a parent-child relationship?

So I have a root model that looks like this:
class Contact(ndb.Model):
first_name= ndb.StringProperty()
last_name= ndb.StringProperty()
age = ndb.IntegerProperty()
and a child model that looks like this:
class Address(ndb.Model)
address_type=ndb.StringProperty(choices=['Home','Office','School'],default='Home')
street = ndb.StringProperty()
city = ndb.StringProperty()
state = ndb.StringProperty()
I want to be able to perform a query similar to this:
Select first_name, last_name, street, city, state WHERE contact.age > 25 and address.city = 'Miami' and address_type = 'School'
I know I can perform searches more easily if I were to setup the addresses as a structured property within the contact model, but I don't like using Structured Properties because they don't have their own keys, thus making entity maintenance more challenging.
I tried doing a search for contacts first and then feeding the resulting keys into a WHERE IN clause but it didn't work, example:
query1 = Contact.query(Contact.age>25).iter(keys_only = True)
query2 = Address.query(Address.city=='Miami', Address.address_type=='School',Address.ancestor.IN(query1))
Any ideas as to how to go about this would be appreciated.
OK so it looks like my original idea of filtering one query by passing in the keys of another will work. The problem is that you can't perform a WHERE-IN clause against an ancestor property so you have to store the parent key as a standard ndb.KeyProperty() inside of the child entity, then perform the WHERE-IN clause against that KeyProperty field.
Here's an example that will work directly from the interactive console in the Appengine SDK:
from google.appengine.ext import ndb
class Contact(ndb.Model):
first_name= ndb.StringProperty()
last_name= ndb.StringProperty()
age = ndb.IntegerProperty()
class Address(ndb.Model):
address_type=ndb.StringProperty(choices=['Home','Office','School'],default='Home')
street = ndb.StringProperty()
city = ndb.StringProperty()
state = ndb.StringProperty()
contact = ndb.KeyProperty()
# Contact 1
contact1 = Contact(first_name='Homer', last_name='Simpson', age=45)
contact1_result = contact1.put()
contact1_address1 = Address(address_type='Home',street='742 Evergreen Terrace', city='Springfield', state='Illinois', contact=contact1_result, parent=contact1_result)
contact1_address1.put()
contact1_address2 = Address(address_type='Office',street=' 1 Industry Row', city='Springfield', state='Illinois', contact=contact1_result, parent=contact1_result)
contact1_address2.put()
# Contact 2
contact2 = Contact(first_name='Peter', last_name='Griffan', age=42)
contact2_result = contact2.put()
contact2_address1 = Address(address_type='Home',street='31 Spooner Street', city='Quahog', state='Rhode Island', contact=contact2_result, parent=contact2_result)
contact2_address1.put()
# This gets the keys of all the contacts that are over the age of 25
qry1 = Contact.query(Contact.age>25).fetch(keys_only=True)
# This query gets all addresses of type 'Home' where the contacts are in the result set of qry1
qry2 = Address.query(Address.address_type=='Home').filter(Address.contact.IN(qry1))
for item in qry2:
print 'Contact: %s,%s,%s,%s'% (item.contact.get().first_name, item.contact.get().last_name, item.address_type, item.street)
This will render a result that looks kinda like this:
Contact: Peter,Griffan,Home,31 Spooner Street
Contact: Homer,Simpson,Home,742 Evergreen Terrace
Can you use an Ancestor query?
query1 = Contact.query(Contact.age>25).iter(keys_only = True)
for contact in query1:
query2 = Address.query(Address.city=='Miami',
Address.address_type=='School',
ancestor=contact)
If that's not efficient enough, how about filtering the addresses?
query1 = Contact.query(Contact.age>25).iter(keys_only = True)
contacts = set(query1)
query2 = Address.query(Address.city=='Miami', Address.address_type=='School')
addresses = [address for address in query2 if address.key.parent() in contacts]

Cassandra - CqlEngine - using collection

I want to know how I can work with collection in cqlengine
I can insert value to list but just one value so I can't append some value to my list
I want to do this:
In CQL3:
UPDATE users
SET top_places = [ 'the shire' ] + top_places WHERE user_id = 'frodo';
In CqlEngine:
connection.setup(['127.0.0.1:9160'])
TestModel.create(id=1,field1 = [2])
this code will add 2 to my list but when I insert new value it replace by old value in list.
The only help in Cqlengine :
https://cqlengine.readthedocs.org/en/latest/topics/columns.html#collection-type-columns
And I want to know that how I can Read collection field by cqlengine.
Is it an dictionary in my django project? how I can use it?!!
Please help.
Thanks
Looking at your example it's a list.
Given a table based on the Cassandra CQL documentation:
CREATE TABLE plays (
id text PRIMARY KEY,
game text,
players int,
scores list<int>
)
You have to declare model like this:
class Plays(Model):
id = columns.Text(primary_key=True)
game = columns.Text()
players = columns.Integer()
scores = columns.List(columns.Integer())
You can create a new entry like this (omitting the code how to connect):
Plays.create(id = '123-afde', game = 'quake', players = 3, scores = [1, 2, 3])
Then to update the list of scores one does:
play = Plays.objects.filter(id = '123-afde').get()
play.scores.append(20) # <- this will add a new entry at the end of the list
play.save() # <- this will propagate the update to Cassandra - don't forget it
Now if you query your data with the CQL client you should see new values:
id | game | players | scores
----------+-------+---------+---------------
123-afde | quake | 3 | [1, 2, 3, 20]
To get the values in python you can simply use an index of an array:
print "Length is %(len)s and 3rd element is %(val)d" %\
{ "len" : len(play.scores), "val": play.scores[2] }

Identify paths between two nodes in neo4j

I have two paths in a graph: A-B-C-D and A-B-E-F. I would like to assign identification numbers to those paths, i.e. A-B-C-D would be 1 and A-B-E-F would be 2.
Is it possible? If yes, how?
You mean like a persistent path ID? This isn't directly featured, but you can do it in a Query in Cypher.
If you want somthing persistent, you can always use an index, so create a Relationship index that would store the Relationships of Path 1 under the key/value of Path:1.
EDIT: After getting more information, here's a use case using the index:
It would be up to you to define this in the Index. Here is what you do:
Node a = db.createNode();
Node b = db.createNode();
Node c = db.createNode();
Node d = db.createNode();
Node e = db.createNode();
Node f = db.createNode();
Relationship aTob = a.createRelationshipTo(b, DynamicRelationshipType.withName("RELATIONSHIP"));
Relationship bToc = b.createRelationshipTo(c, DynamicRelationshipType.withName("RELATIONSHIP"));
Relationship cTod = c.createRelationshipTo(d, DynamicRelationshipType.withName("RELATIONSHIP"));
Relationship bToe = b.createRelationshipTo(e, DynamicRelationshipType.withName("RELATIONSHIP"));
Relationship eTof = e.createRelationshipTo(f, DynamicRelationshipType.withName("RELATIONSHIP"));
Index<Relationship> relationshipIndex = db.index().forRelationships("PathIndex");
String pathRId = UUID.randomUUID().toString();
String pathMId = UUID.randomUUID().toString();
relationshipIndex.add(aTob, "PathId", pathRId);
relationshipIndex.add(bToc, "PathId", pathRId);
relationshipIndex.add(cTod, "PathId", pathRId);
relationshipIndex.add(aTob, "PathId", pathMId);
relationshipIndex.add(bToe, "PathId", pathMId);
relationshipIndex.add(eTof, "PathId", pathMId);
Then when you want to find a path, you would search by the ID. You would be responsible for maintaining the Set Id in the index, here I use UUID, but you can use something more representative of your information. The relationships would not be in any repeatable order when returned from the Index.

Resources