I have a value in orientdb, which is a JSON object.
Assume that JSON object is :
a = {"abc":123}
When I am sending a query using pyorient, it is not able to get this value in the select query, and hangs.
In the orientdb console, this JSON object seems to be converted in some other format like
a = {abc=123}
I guess it is hanging because of the same issue.
The query from pyorient is:
client.query("select a from <tablename>")
This hangs and doesn't seems to be working.
Could you please help ho to parse this JSON object in pyorient?
I used OrientDb's REST API service to fetch JSON object fields from the database. PyOrient hangs when requested for a JSON object field.
So fetch the rid of the record you want, and use REST services to get all the fields, which worked perfectly fine.
pyorient gives you the output something like:
a = {'abc': '123'}
and json.loads() function works with " and not with ', so to solve it, you need to do this:
b=str(a)
b.replace("'",'"')
json_data = json.loads(b)
print(json_data.keys())
I have defined a function to get vertex and after you get your vertex you can use for loop to parse the json result. Lets say the vertex "Root" have an attribute "name", and in the for loop after the query execution we can parse the value like "res.name" to fetch the value.
I think in recent version, they fixed the hanging issue. I am not facing any issue with hanging while query execution.
def get_verted(vertex):
result = client.command("select * from "+vertex)
for res in result:
print res.name
get_vertex("Root")
Related
Problem: I want to pick a field of index in elasticsearch and look for all the values against it. Like if I give a key I should get the value for that key and if that key exists more than once, so each whats the each value. Or even if I get one of the values would work for me.
How I am trying to work through it: Query the elasticsearch
I am trying to query my data from Elasticsearch;
r = es.search(index="test",body = {'query': {'wildcard': {'task_name': "*"}}})
I thought to load the data to a python object ( dictionary) to read a key values. However, when I try json.loads(r.json) it gives me an error : AttributeError: 'dict' object has no attribute 'json'.
I even tried with json.load(r) but the error remains the same.
When I use MySql function AES_DECRYPT() in a raw query in Django, this function didn't work. My code is like this:
sql = "select AES_DECRYPT(myfield, mykey) as ssn from mytable "
people_list = Peopletable.objects.raw(sql)
for p in people_list:
print p.ssn
It printed out None, which means AES_DECRYPT() didn't work. But if I run the query in python side then I get what I need. I tried other mysql functions like SUBSTR() and they worked perfectly. Seems like only this AES_DECRYPT() doesn't work in Django. Can anyone help? Thanks a lot!
It doesn't actually mean it doesn't work just that result of AES_DECRYPT(myfield, mykey) is None (null).
If AES_DECRYPT() detects invalid data or incorrect padding, it returns NULL. However, it is possible for AES_DECRYPT() to return a non-NULL value (possibly garbage) if the input data or the key is invalid.
Try to run same query directly on database it will probably have same result.
I'm having an issue getting what I think is a simple script working and I think it's just me not knowing how to get a variable working.
i'm working on a salesforce script that grabs a list of objects, then looks through the list of objects to get all fields on the table.
this query in python works perfectly for giving me my list of objects that I've pulled into the DB
query = ("SELECT obj_name FROM syncsfobjects")
cursor.execute(query)
i then loop through these records
for x in cursor:
now this is where my issue lies I want to use the obj_name that comes in from my query within the next statement
for xy in sf.%obj_name%.describe()["field"]:
what i'm having massive issues with is getting the obj name into this simple-salesforce query.
if I create a string it works fine
objectname = str(x)
sfquery = 'sf. %s .describe()["fields"]' % objectname
but then when I use the sfquery for my next loop all the loop does it run through each letter within the string instead of run the sf connection command.
any I just missing something simple?
cheers
dan
for xy in sf.%obj_name%.describe()["field"]:
Python doesn't let you do "percent substitution" outside of strings, but you can still access attributes if all you have are their names:
for xy in getattr(sf, objectname).describe()["field"]:
So I am trying to use the $in operator in Pymongo where I want to search with a bunch of MongoIDs.
First I have this query to find an array of MongoIDs:
findUsers = db.users.find_one({'_id':user_id},{'_id':0, 'f':1})
If I print the findUsers['f'] it looks like this:
[ObjectId('53b2dc0b24c4310292e6def5'), ObjectId('53b6dbb654a7820416a12767')]
These object IDs are user ids and what I want to do is to find all users that are in the users collection with this array of ObjectID. So my thought was this:
foundUsers = db.users.find({'_id':{'$in':findUsers['f']}})
However when I print the foundUsers the outcome is this:
<pymongo.cursor.Cursor object at 0x10d972c50>
which is not what I normally get when I print a query out :(
What am I doing wrong here?
Many thanks.
Also just for you reference, I have queried in the mongo shell and it works as expected:
db.users.find({_id: {$in:[ObjectId('53b2dc0b24c4310292e6def5'), ObjectId('53b6dbb654a7820416a12767')]}})
You are encountering the difference between findOne() and find() in MongoDB. findOne returns a single document. find() returns a mongoDB cursor. Normally you have to iterate over the cursor to show the results. The reason your code works in the mongo shell is that the mongo shell treats cursors differently if they return 20 documents or less - it handles iterating over the cursor for you:
Cursors
In the mongo shell, the primary method for the read operation is the
db.collection.find() method. This method queries a collection and
returns a cursor to the returning documents.
To access the documents, you need to iterate the cursor. However, in
the mongo shell, if the returned cursor is not assigned to a variable
using the var keyword, then the cursor is automatically iterated up to
20 times [1] to print up to the first 20 documents in the results.
http://docs.mongodb.org/manual/core/cursors/
The pymongo manual page on iterating over cursors would probably be a good place to start:
http://api.mongodb.org/python/current/api/pymongo/cursor.html
but here's a piece of code that should illustrate the basics for you. After your call to find() run this:
for doc in findUsers:
print(doc)
I save an object in a database in python but when I try to print it it just prints the location of the object. How can I print the actual data of the object?
Assuming the object is student with an id as "key" and email, address as the objects data
def getrecord(self,Id):
rec=self.db[Id]
print(rec)
The output I get:
...object at 0x02605B10>
What I would like to see:
"student#gmail.com, central park, new york"
Or something like that.
Looks like rec is a database record object.
What mechanism are you using to retrieve from the database? An ORM like sqlalchemy? Or directly via an interface such as MySQLdb? The api of your library might explain the data better.
You might try something like print rec.__dict__ to see if the values are part of the rec object, such that you could access the individual return parameters directly print '{0}, {1}, {2}'.format(rec.email, rec.address, rec.city).
Also, depending on what environment you're working in, the console in pycharm, pydev/eclipse can give you some insight into the methods and fields on your record object.