Does documents.create_bulk(docs) exist? How to access it? Can it be used too to update existing documents? - arangodb

I stumbled upon the following code in the official ArangoDB-Phyton documentation:
from arango import create
# connection & collection `test`
c = create(db="test")
c.database.create() # make sure database exists
c.test.create()
docs = [
{"name": "doc1"},
{"name": "doc2"},
{"name": "doc3"}]
response = c.test.documents.create_bulk(docs)
This code was something I was looking for, especially the later on the page mentioned update-method as I have several documents modified locally and want to bulk-update it.
However I can't make it run. I am not able to even find any of the the methods.
ipdb> import arango #works!
ipdb> from arango import create
*** ImportError: cannot import name 'create' from 'arango' (/usr/local/lib/python3.8/site-packages/arango/__init__.py)
I try to use it with the old-fashioned way (https://docs.python-arango.com/en/main/database.html):
ipdb> mydb=self.getDB() #Gets a client and requests the db
ipdb> mydb
<StandardDatabase verstionTestDB>
ipdb> v1=mydb.collection("V1")
ipdb> v1
<StandardCollection V1>
At this point how would I get the collection.document?
ipdb> v1.documents
*** AttributeError: 'StandardCollection' object has no attribute 'documents'
It doesn't seem to be reasonable that the DB-object has it, however there is something. Not sure what though.
<bound method Database.document of <StandardDatabase verstionTestDB>>
ipdb> mydb.documents()
*** AttributeError: 'StandardDatabase' object has no attribute 'documents'
ipdb> mydb.document()
*** TypeError: document() missing 1 required positional argument: 'document'
The current versions according to pip:
pip list | grep arango
arango 0.2.1
python-arango 6.1.0
What am I doing wrong here? Why can't I find that

Related

nodejs gremlin update if exist or else create

I am using nodejs gremlin against AWS neptune, the requirement is to update properties if vertice exist, or else, create a new vertice, i tried below
g.V().has('event','id','1').
fold().
coalesce(unfold(),
addV('event').property('id','1'))
but I got 'unfold is not defined' error, how do I resolve this?
You probably just need to import unfold() properly. Some common imports for working with Gremlin can be found here but in your case I think you just need to do:
const __ = gremlin.process.statics
and then refer to unfold() as __.unfold() - or just import unfold() as a function explicitly to use it as you were using it.

How to connect python with vertica using ODBC?

import sqlalchemy as sa
conn = "vertica+pyodbc://dbadmin:password#VMart"
sa.create_engine(conn, pool_size=10, max_overflow=20)
%load_ext sql
%sql vertica+pyodbc://VMart
error
(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver
Manager] Data source name not found and no default driver specified
(0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/rvf5)
Connection info needed in SQLAlchemy format, example:
postgresql://username:password#hostname/dbname<br>
or an existing connection: dict_keys([])
First
import sqlalchemy as sa
import urllib
sa.create_engine('vertica+vertica_python://dbadmin:password#192.168.11.132:5433/VMart')
%load_ext sql
%sql vertica+pyodbc://VMart
Second
import sqlalchemy as sa
sa.create_engine('vertica+vertica_python://dbadmin:password#VMart')
import pyodbc
Third
conn = pyodbc.connect("DRIVER=Vertica;SERVER=192.168.11.132;DATABASE=VMart;PORT=5433;UID=dbadmin;PWD=password")
same error
error but I already test vertica odbc in windows10. It connect
successfuled
(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/rvf5)
Connection info needed in SQLAlchemy format, example:
postgresql://username:password#hostname/dbname
or an existing connection: dict_keys([])
Might not get to the root of your problem, but below are a few steps that could help in making a connection:
Have you tried installing sqlalchemy-vertica driver?. Specifically I would recommend installing with vertica-python. This is one driver for the connection with vertica in python and I found it works pretty well (caveats later)
pip install sqlalchemy-vertica[vertica-python]
Have you imported vertica-python? Not sure this will impact the code, but it definitely helps to remove as an issue. In case you have not installed it:
pip install vertica-python
Edit: As a matter of fact if all you need is to read data, this package worked well for me on itself. Below is a sample code that is currently working for me:
import vertica_python
conn_info = {
'host': 'host-ip',
'port': port,
'user': 'usr',
'password':password,
'database': 'db',
# autogenerated session label by default,
'session_label': 'current_session',
# default throw error on invalid UTF-8 results
'unicode_error': 'strict',
# SSL is disabled by default
'ssl': False,
# using server-side prepared statements is disabled by default
'use_prepared_statements': False,
# connection timeout is not enabled by default
# 'connection_timeout': 1
}
vertica_python.connect(**conn_info)
Making the connection. I would recommend your first line to connect:
sa.create_engine('vertica+vertica_python://dbadmin:password#192.168.11.132:5433/VMart')
Caveats using Pandas:
I had issues with sending str variables to the db using pandas.to_sql. If that is your case, just make sure to pass the types explicitly for those columns as VARCHAR (it will force to TEXT for some reason). This function from this question might help you:
def updateType(df_para):
dtypedict = {} # create and empty dictionary
for i,j in zip(df_para.columns, df_para.dtypes):
if "object" in str(j):
dtypedict.update({i: sa.types.VARCHAR})
return dtypedict
updatedict = updateType(df)
I would receive a weird error when using to_sql:
NotImplementedError: executemany is implemented for simple INSERT statements only.
Did not find a proper solution but tried to remediate it using the chunksize param, and it seems to have worked.

I can get entity by ID after getting entity by username only. Is it a bug? Video from shell

I can get entity by ID after getting entity by username only. Is it a bug? Video from shell
I'm using Mac and python 3
I'm trying to get entity by id
entity = client.get_entity(1151511560)
but get an exception:
ValueError: Could not find the input entity for <telethon.tl.types.PeerUser object at 0x1172312e8>. Please read https://telethon.readthedocs.io/en/latest/extra/basic/entities.html to find out more details.
Then I'm successfully getting an entity by username "ekat01"
After that i successfully get an entity by id.
Why I can't get an entity by id only? I think, it's a bug, isn't it?
Video with proofs by the link: https://youtu.be/mnDNZZir5PY
Github -------------------------------------------------
From juanvelascogomez:
If I am not wrong, that is explained in the docs "Users, chat and channel, Important section": https://telethon.readthedocs.io/en/stable/extra/basic/entities.html
Once the library has “seen” the entity, you can use their integer ID. You can’t use entities from IDs the library hasn’t seen. You must make the library see them at least once and disconnect properly. You know where the entities are and you must tell the library. It won’t guess for you.
From Lonami:
On a clean session,
with client:
try:
client.get_entity(1151511560)
except ValueError:
print('Error as expected')
client.get_entity("ekat01")
client.get_entity(1151511560)
print('Works as expected')
prints:
Error as expected
Works as expected
On a second run,
with client:
client.get_entity(1151511560)
print('Works as expected')
prints:
Works as expected

How to watch for an updated boolean value in a pymongo document?

I am trying to listen for an update to a collection using pymongo 3.6.1.
The collection gets updated with a document that looks something like this:
{"End_Word":"bit","Success":true,"Score":1,"Term_Index":5}
Where if the key Success gets updated to true in any of the documents a def dosomething() gets called.
The mongodb api documentation shows this example, as a first step:
with db.collection.watch() as stream:
for change in stream:
print(change)
In attempting to replicate this I am getting a OperationFailure: Unrecognized pipeline stage name: '$changeStream' error. When researching other's with similar issues on stack The answer was connection driver versions. I don't think this is the case here:
Code:
db_name = 'mapstore'
coll_name = 'oxygen'
MONGO_DB_DRIVER = pymongo atlas M2 instance driver
conn = pymongo.MongoClient(MONGO_DB_DRIVER)
db = conn[db_name]
print(db.collection_names())
Update:
I also looked in these places
How to listen for changes to a MongoDB collection?
https://docs.mongodb.com/master/changeStreams/
What is the right approach?
The below error
OperationFailure: Unrecognized pipeline stage name: '$changeStream'
Indicates that your Mongodb server is below 3.6.0 and that is why it doesn't recognize this command. Even though you have latest client, doesn't change how the server behaves

Mongoengine check if object exists without fetching the object

On Django we can use the QuerySet.exists() to check if an object exists in the DB in the most efficient way, without actually fetching the record.
Is there an equivalent function on Mongoengine?
According to official documentation, here is solution how to make this if you have object id.Its the best solution for your case, that i have seen in mongoengine documentation.It works as below:
# Returns None or Object if it exists
result = Collection.objects.with_id(object_id=*your object id*)
if result is None:
# raise error
else:
# make some actions with this object
Is this is what are you looking for?

Resources