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.
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, '', '')
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)
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)
This is the execution result after being deployed to Bluemix.
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).
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.
(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