How to write code to access python dashDB on Bluemix or local

This is a memo of code development to be executed as a CF application of local and Bluemix, following Note of accessing dashDB from python.

Read the sample code IBM-Bluemix / get-started-python (1) for the Bluemix Cloud Foundry app (BMX-CFA). Then, I found that the code was based on two cases, one running locally and the other running on Bluemix. Therefore, while referring to how to write this code, consider the code to access dashDB.

And a sample code to access dashDB from Python [dashDB and Python] (https://hub.jazz.net/project/ibmdatabase/dashDB/overview#https://hub.jazz.net/git/ibmdatabase%252FdashDB/list/master/samples/dashDBPython) For reference (2) There are many issues to be solved, and there are useless codes including duplicate codes, which are not very helpful, so I decided to create sophisticated code while looking sideways.

The GitHub repository corresponding to this article is https://github.com/takara9/python-dashDB. See the README.md at this URL for installation instructions.

Use the dashDB service in your local environment and Bluemix

In order to check the operation of the basic program, we routinely create snippet code, think about how to write it, and incorporate it into the main program. However, in order to write a snippet on Bluemix and check the operation, it is necessary to repeat bx cf push and check the log from the web screen, which requires several times more effort. Ideally, for this reason, it can run locally, in the early stages of development and maintenance, and the service can run from Bluemix's PaaS platform.

In the operating environment of BMX-CFA, as a method of connecting the service linked with the runtime environment of the program, the environment variable VCAP_SERVICES is set with access credentials and passed to the program. When executing in the BMX-CFA environment, you can refer to the environment variables, but when executing in the local environment, you need to read the JSON file etc. to obtain the service credentials.

The Bluemix CLI has commands for getting service credentials, so it's a good idea to use them. The following command displays a list of service credential names defined in one instance of dasDB. The part enclosed in double quotes is the instance name of dashDB.

bx cf service-keys "dashDB for Analytics-iq"

You can get the JSON format information by selecting from the list with the following command. The final parameter, Credentials-1, is the name of the service credentials obtained with the above command.

bx cf service-key "dashDB for Analytics-iq" Credentials-1

The following code is a snippet that sets the required information from the environment variable VCAP_SERVICES and a JSON-formatted file. When this code is executed, the service credentials of dashDB will be set in creds. Information corresponding to various access methods is provided, but the character string to be connected by the encrypted communication given to the ODBC driver is set in ssldsn on the last line.

    creds = None
 #Set from environment variables
    if 'VCAP_SERVICES' in os.environ:
	vcap = json.loads(os.getenv('VCAP_SERVICES'))
	print('Found VCAP_SERVICES')
	if 'dashDB' in vcap:
            creds = vcap['dashDB'][0]['credentials']
 #Set from local file
    elif os.path.isfile('vcap-local.json'):
	with open('vcap-local.json') as f:
            print('Found local VCAP_SERVICES')
            vcap = json.load(f)
            creds = vcap['services']['dashDB'][0]['credentials']
 # Encrypted communication string
    url = creds["ssldsn"]

Connect to dashDB by calling the function in the ODBC driver (3) ibm_db with the variable url.

    conn = ibm_db.connect(url, '', '')

Local environment and port number on Bluemix

In the BMX-CFA execution environment, PaaS specifies the port number that the app can use. This port number is also used as a runtime environment variable to convey the port number dynamically assigned during provisioning. On the other hand, when executing in a local environment, know the port numbers that can be used by self-management and assign them manually. The snippets below are for both.

    port = int(os.getenv('PORT', 8080))
    app.run(host='0.0.0.0', port=port, debug=True)

Python Flask framework

This app uses Flask to display the web screen. Flask is a lightweight web application framework for the programming language Python. He calls himself a "micro-framework" because it keeps the functionality provided as standard to a minimum. It is based on the Werkzeug WSGI toolkit and the Jinja2 template engine. It is released under the BSD license. (6), (5)

An example of the smallest app (5) is presented and explained below. The following will return the string "Hello, World!" When accessed. hello_world () is called back when the route is GET with @ app.route ('/').

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!\n'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9080, debug=True)

Just start the web server from the command line.

ubuntu@ubuntu-xenial:~/dashDB$ python test7.py
 * Running on http://0.0.0.0:9080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 195-308-734

I'm running as a client with the curl command.

buntu@ubuntu-xenial:~/dashDB/python-dashDB$ curl http://localhost:9080/
Hello, World!

In the code of this app, an HTTP GET to / is triggered to call back hello () to allow ODBC to access dashDB.

# Callback on access
@app.route('/')
def hello():
    conn = ibm_db.connect(url, '', '')
    stmt = ibm_db.prepare(conn, 'SELECT RACE_CODE, RACE_DESC from SAMPLES.RACE_CODE FETCH FIRST 5 ROWS ONLY')
    ibm_db.execute(stmt)

App execution result

This is the execution result after being deployed to Bluemix. スクリーンショット 2017-06-03 16.15.07.png

Fileset description

bx cf push The previous folders and files are listed below for a description.

ubuntu@ubuntu-xenial:~/dashDB$ tree python-dashDB
python-dashDB/
├── LICENSE
├── main.py
├── manifest.yml
├── Procfile
├── README.md
├── requirements.txt
└── vcap-local.json

At deployment time, read manifest.yml to reserve resources for the Bluemix ClooudFoundry runtime, install the package from requirements.txt, and run the Procfile command to start the app.

Reference (7) can help explain the manifest items. A description of the Procfile can be found in Reference (8).

Summary

Bluemix is not a concept of developing only in PaaS environment, it supports operation in both local development environment and CF application, it is efficient with tools familiar to developers, and troublesome server operation is left to PaaS By doing so, we aim to provide the happiest environment for developers. I understood that.

Reference material

(1) IBM-Bluemix/get-started-python https://github.com/IBM-Bluemix/get-started-python (2) dashDB and Python https://hub.jazz.net/project/ibmdatabase/dashDB/overview#https://hub.jazz.net/git/ibmdatabase%252FdashDB/list/master/samples/dashDBPython (3) Python DBI driver for DB2 (LUW, zOS, i5) and IDS https://pypi.python.org/pypi/ibm_db/2.0.7 (4) Python Packaging User Guide https://packaging.python.org/ (5) Flask Home page http://flask.pocoo.org/ (6) Wikipedia Flask https://ja.wikipedia.org/wiki/Flask (7) Application manifestn https://console.ng.bluemix.net/docs/manageapps/depapps.html#appmanifest (8) CloudFoundry Application about Procfile https://docs.cloudfoundry.org/buildpacks/prod-server.html#procfile

Recommended Posts

How to write code to access python dashDB on Bluemix or local
How to write a Python class
How to access wikipedia from python
Notes on how to write requirements.txt
How to access environment variables in Python
Qiita (1) How to write a code name
How to read pydoc on python interpreter
[Kivy] How to install Kivy on Windows [Python]
How to easily create an environment where python code runs on Jupyter without polluting the local environment
How to write Python document comments (Docstrings)
How to erase Python 2.x on Mac.
Memorandum on how to use gremlin python
How to access RDS from Lambda (python)
How to write Ruby to_s in Python
Try to write python code to generate go code --Try porting JSON-to-Go and so on
How to write urlfetch unittest on GAE / P
[2020 version] How to install Python3 on AWS EC2
Strategy on how to monetize with Python Java
How to install OpenCV on Jetson Nano Python
How to use Python Kivy ④ ~ Execution on Android ~
How to run MeCab on Ubuntu 18.04 LTS Python
To write to Error Repoting in Python on GAE
[Python] How to install OpenCV on Anaconda [Windows]
[Go] How to write or call a function
How to write environment variables that you don't want to put on [GitHub] Python
I want to write in Python! (1) Code format check
20th Offline Real-time How to Write Problems in Python
Think about how to program Python on the iPad
How to install Python
How to write string concatenation in multiple lines in Python
How to measure processing time in Python or Java
Rewrite Python2 code to Python3 (2to3)
Yum command to access MySQL with Python 3 on Linux
How to install python
How to write a list / dictionary type of Python3
How to build a Django (python) environment on docker
How to make a Python package using VS Code
[Python] How to write a docstring that conforms to PEP8
[Note] How to write QR code and description in the same image with python
How to use Django on Google App Engine / Python
Write code to Unit Test a Python web app
How to enjoy Python on Android !! Programming on the go !!
How to build a Python environment on amazon linux 2
[Introduction to Python] How to write repetitive statements using for statements
How to create a kubernetes pod from python code
"Cython" tutorial to make Python explosive: When C ++ code depends on the library. Write setup.py.
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
How to manage Python minor version (build virtual environment) on Windows (without Pyenv or WSL)
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
[Introduction to Python] How to write conditional branches using if statements
Offline real-time how to write Python implementation example of E14
How to build a new python virtual environment on Ubuntu
[Hyperledger Iroha] Notes on how to use the Python SDK
Don't lose to Ruby! How to run Python (Django) on Heroku
How to use VS Code in venv environment on windows
[Ev3dev] How to display bmp image on LCD with python
[Python] How to write an if statement in one sentence.
How to deploy the easiest python textbook pybot on Heroku
How to write a metaclass that supports both python2 and python3
How doi may be useful when asking how to write code?
[ROS] How to write Publisher and Subscriber on one node