mapr-demos / python-bindings

Python bindings for MapR DB JSON API
Apache License 2.0
6 stars 15 forks source link

find() with conditions not working? #15

Open namato opened 9 years ago

namato commented 9 years ago

I have not yet gotten find() to work with conditions -- here are a couple of code examples that fail. I could be doing this wrong.

Example 1

p = "/user/mapr/new_wdata2"
connection = maprdb.connect()
print("opening table %s" % p)
if connection.exists(p):
    connection.delete(p)
t = connection.create(p)
newdoc = maprdb.Document({"_id": "123", "foo": "000", "bar": "111"})
t.insert_or_replace(newdoc)
matching = t.find({"foo":"000"})
print(matching)

This code fails with: Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/maprdb-0.0.1-py3.5.egg/maprdb/utils.py", line 98, in wrapper ret = f(_args, *_kwargs) File "/usr/local/lib/python3.5/site-packages/maprdb-0.0.1-py3.5.egg/maprdb/tables.py", line 51, in find document_stream = self.java_table.find(columns) if columns else self.java_table.find() RuntimeError: No matching overloads found. at native/common/jp_method.cpp:121

The same error occurs if I build a maprdb.Condition() by hand and calling t.find() using that as the first argument.

Example 2

Same as in Example #1, changing the 'find' line to:

matching = t.find({"foo":"000"}, columns=None)

Traceback (most recent call last): File "test.py", line 15, in matching = t.find({"foo":"000"}, columns=None) File "/usr/local/lib/python3.5/site-packages/maprdb-0.0.1-py3.5.egg/maprdb/utils.py", line 98, in wrapper ret = f(_args, *_kwargs) TypeError: find() got multiple values for argument 'columns'

barun12 commented 5 years ago

Hey, 3 years later, I am still seeing this problem :( - Not able to use find with condition. Has there been some work around this? My problem is similar to https://stackoverflow.com/questions/52529038/maprdb-find-by-condition-in-python-throws-exception-class-com-mapr-db-conditio.

iandow commented 5 years ago

Try using the maprdb-python-client package which is officially supported by MapR:

https://pypi.org/project/maprdb-python-client/#description https://mapr.com/docs/home/MapR-DB/JSON_DB/GettingStartedPythonOJAI.html

You can install the package with pip install maprdb-python-client.

Here's an example of submitting a conditional query:

from mapr.ojai.ojai_query.QueryOp import QueryOp
from mapr.ojai.storage.ConnectionFactory import ConnectionFactory

# Create a connection to data access server
connection_str = 'nodeb:5678?auth=basic;user=mapr;password=mapr;ssl=false;'
connection = ConnectionFactory.get_connection(connection_str=connection_str)

# Get a store and assign it as a DocumentStore object
store = connection.get_store('/crm_data')

# Create an OJAI query
query = {"$where": {"$like": {"phone_number": '%552-2152%'}}}

# options for find request
options = {'ojai.mapr.query.result-as-document': True}

# fetch OJAI Documents by query
query_result = store.find(query, options=options)

# Print OJAI Documents from document stream
for doc in query_result: print(doc.as_dictionary())

# close the OJAI connection
connection.close()

For more info about the conditional query operators, see: https://mapr.com/docs/61/MapR-DB/JSON_DB/OJAIQueryConditionOperators.html#QueryingJSONDocumentFields__section_rdp_sjd_kdb

Here's another reference for getting started with maprdb: https://github.com/mapr-demos/mapr-db-60-getting-started