Oracle NoSQL Database Python SDK
About
This is the Python SDK for Oracle NoSQL Database. Python versions 3.5+ are
supported. The SDK provides interfaces, documentation, and examples to help
develop Python applications that connect to the Oracle NoSQL Database Cloud
Service, Oracle NoSQL Database and to the Oracle NoSQL Cloud Simulator (which
runs on a local machine).
In order to run the Oracle NoSQL Cloud Simulator, a separate download is
necessary from the Oracle NoSQL OTN download page. Throughout the documentation,
the Oracle NoSQL Database Cloud Service and Cloud Simulator are referred to as
the "cloud service" while the Oracle NoSQL Database is referred to as
"on-premise." In the API reference classes and interfaces are noted if they are only relevant to
a specific environment.
The on-premise configuration requires a running instance of the Oracle NoSQL
database. In addition a running proxy service is required. See Oracle NoSQL
Database Downloads for downloads, and see Information about the proxy for
proxy configuration information.
This project is open source and maintained by Oracle Corp. The home page for the
project is here.
Installation
The SDK can be installed using pip. If using Python 3 the command may be pip3::
pip install borneo
If you are using the Oracle NoSQL Database cloud service you will also need to
install the oci package::
pip install oci
See the installation guide for additional requirements and and alternative install
methods.
Examples
Examples can be found on GitHub
Examples include simple, standalone programs. They include comments about how
they can be configured and run in the different supported environments.
Documentation
The documentation has
information on using the SDK as well as an API reference describing the classes.
Changes
See the Changelog
Help
When requesting help please be sure to include as much detail as possible,
including version of the SDK and simple, standalone example code as needed.
Quickstart
The following is a quick start tutorial to run a simple program in the supported
environments. The same template source code is used for all environments. The
first step is to cut the program below and paste it into an editor for minor
modifications. The instructions assume that is stored as quickstart.py, but you
can use any name you like. The quickstart example supports 3 environments:
- Oracle NoSQL Database Cloud Service
- Oracle NoSQL Cloud Simulator
- Oracle NoSQL Database on-premise, using the proxy server
See Running Quickstart
_ to
run the quickstart program in different environments. The instructions assume
that the borneo package has been installed.
import sys
from borneo import (
AuthorizationProvider, DeleteRequest, GetRequest,
IllegalArgumentException, NoSQLHandle, NoSQLHandleConfig, PutRequest,
QueryRequest, Regions, TableLimits, TableRequest)
from borneo.iam import SignatureProvider
from borneo.kv import StoreAccessTokenProvider
cloud_region = Regions.EU_ZURICH_1
cloudsim_endpoint = 'localhost:8080'
kvstore_endpoint = 'localhost:80'
cloudsim_id = 'cloudsim'
tenancy = None
user = None
private_key = 'path-to-private-key-or-private-key-content'
fingerprint = 'fingerprint for uploaded public key'
pass_phrase = None
class CloudsimAuthorizationProvider(AuthorizationProvider):
"""
Cloud Simulator Only.
This class is used as an AuthorizationProvider when using the Cloud
Simulator, which has no security configuration. It accepts a string
tenant_id that is used as a simple namespace for tables.
"""
def __init__(self, tenant_id):
super(CloudsimAuthorizationProvider, self).__init__()
self._tenant_id = tenant_id
def close(self):
pass
def get_authorization_string(self, request=None):
return 'Bearer ' + self._tenant_id
def get_handle(nosql_env):
"""
Returns a NoSQLHandle based on the requested environment. The
differences among the supported environments are encapsulated in this
method.
"""
if nosql_env == 'cloud':
endpoint = cloud_region
if tenancy is not None:
print('Using directly provided credentials')
provider = SignatureProvider(tenant_id=tenancy,
user_id=user,
fingerprint=fingerprint,
private_key=private_key,
pass_phrase=pass_phrase)
else:
print('Using credentials and DEFAULT profile from ' +
'~/.oci/config')
provider = SignatureProvider()
elif nosql_env == 'cloudsim':
print('Using cloud simulator endpoint ' + cloudsim_endpoint)
endpoint = cloudsim_endpoint
provider = CloudsimAuthorizationProvider(cloudsim_id)
elif nosql_env == 'kvstore':
print('Using on-premise endpoint ' + kvstore_endpoint)
endpoint = kvstore_endpoint
provider = StoreAccessTokenProvider()
else:
raise IllegalArgumentException('Unknown environment: ' + nosql_env)
return NoSQLHandle(NoSQLHandleConfig(endpoint, provider))
def main():
table_name = 'PythonQuickstart'
if len(sys.argv) != 2:
print('Usage: python quickstart.py <cloud | cloudsim | kvstore>')
raise SystemExit
nosql_env = sys.argv[1:][0]
print('Using environment: ' + str(nosql_env))
handle = None
try:
handle = get_handle(nosql_env)
statement = (
'Create table if not exists {} (id integer, sid integer, ' +
'name string, primary key(shard(sid), id))').format(table_name)
request = TableRequest().set_statement(statement).set_table_limits(
TableLimits(30, 10, 1))
handle.do_table_request(request, 50000, 3000)
print('After create table')
request = PutRequest().set_table_name(table_name)
for i in range(10):
value = {'id': i, 'sid': 0, 'name': 'myname' + str(i)}
request.set_value(value)
handle.put(request)
print('After put of 10 rows')
request = GetRequest().set_key({'id': 1, 'sid': 0}).set_table_name(
table_name)
result = handle.get(request)
print('After get: ' + str(result))
statement = (
'select * from ' + table_name + ' where id > 2 and id < 8')
request = QueryRequest().set_statement(statement)
print('Query results for: ' + statement)
while True:
result = handle.query(request)
for r in result.get_results():
print('\t' + str(r))
if request.is_done():
break
request = DeleteRequest().set_table_name(table_name).set_key(
{'id': 1, 'sid': 0})
result = handle.delete(request)
print('After delete: ' + str(result))
request = GetRequest().set_key({'id': 1, 'sid': 0}).set_table_name(
table_name)
result = handle.get(request)
print('After get (should be None): ' + str(result))
request = TableRequest().set_statement(
'drop table if exists {} '.format(table_name))
result = handle.table_request(request)
result.wait_for_completion(handle, 40000, 2000)
print('After drop table')
print('Quickstart is complete')
except Exception as e:
print(e)
finally:
if handle is not None:
handle.close()
if __name__ == '__main__':
main()
Running Quickstart
Run Against the Oracle NoSQL Database Cloud Service
Running against the Cloud Service requires an Oracle Cloud account. See
Configure for the Cloud Service for information on
getting an account and acquiring required credentials.
- Collect the following information:
- Tenancy ID
- User ID
- API signing key (private key file in PEM format)
- Fingerprint for the public key uploaded to the user's account
- Private key pass phrase, needed only if the private key is encrypted
-
Edit quickstart.py and add your information. There are 2 ways to supply
credentials in the program:
-
Directly provide the credential information. To use this method, modify the
values of the variables at the top of the program: tenancy, user,
private_key, fingerprint, and pass_phrase, setting them to the
corresponding information you've collected.
-
Using a configuration file. In this case the information you've collected
goes into a file, ~/.oci/config. Configure for the Cloud Service describes the contents of the file. It will look like
this::
[DEFAULT]
tenancy=
user=
fingerprint=
key_file=
pass_phrase=
-
Decide which region you want to use and modify the cloud_region variable to
the desired region. See Regions documentation for possible regions. Not
all support the Oracle NoSQL Database Cloud Service.
-
Run the program:
.. code-block:: pycon
python quickstart.py cloud
Run Against the Oracle NoSQL Cloud Simulator
Running against the Oracle NoSQL Cloud Simulator requires a running Cloud
Simulator instance. See Configure for the Cloud Simulator for information on how to
download and start the Cloud Simulator.
-
Start the Cloud Simulator based on instructions above. Note the HTTP port
used. By default it is 8080 on localhost.
-
The quickstart.py program defaults to localhost:8080 so if the Cloud
Simulator was started using default values no editing is required.
-
Run the program:
.. code-block:: pycon
python quickstart.py cloudsim
Run Against Oracle NoSQL on-premise
Running against the Oracle NoSQL Database on-premise requires a running Oracle
NoSQL Database instance as well as a running NoSQL Proxy server instance. The
program will connect to the proxy server.
See Configure for On-Premise Oracle NoSQL Database for information on how to
download and start the database instance and proxy server. The database and
proxy should be started without security enabled for this quickstart program to
operate correctly. A secure configuration requires a secure proxy and more
complex configuration.
-
Start the Oracle NoSQL Database and proxy server based on instructions above.
Note the HTTP port used. By default the endpoint is localhost:80.
-
The quickstart.py program defaults to localhost:80. If the proxy was
started using a different host or port edit the settings accordingly.
-
Run the program:
.. code-block:: pycon
python quickstart.py kvstore
Contributing
This project welcomes contributions from the community. Before submitting a pull request, please review our contribution guide
Security
Please consult the security guide for our responsible security vulnerability disclosure process
License
Copyright (c) 2018, 2024 Oracle and/or its affiliates.
Released under the Universal Permissive License v1.0 as shown at
https://oss.oracle.com/licenses/upl/.