One Small Program, Two Powerful Messages
To further explain the value of open source, Jesse Gorzinski highlights a small, open-source Python program.
By Jesse Gorzinski03/06/2017
(image not available)
Some may be familiar with the NETSTAT CL command. This utility is similar, but has a couple advantages. It can be run from more places, like an SSH terminal or as part of a shell script. It also combines IPv4 and IPv6 data for convenience.
Before going further, however, I want to pause for a moment. It’s hard to write about open-source topics without giving a “tip of the hat” to someone. This time, the recipient is Alan Seiden. Alan is a well-known public speaker who often provides usable, real-life examples in his sessions. Today’s topic is one such example. After creating the tool, Alan did the right thing and published the work on GitHub. Well done, Alan! Please go check out the project here.
So, why did I choose to put this program in the spotlight? It’s a simple little program, but it has two very strong implications that I wanted to discuss!
The Power of CommunityAlan wrote a command-line utility and wanted its output to be in a readable, tabular format. To have output in this manner, one could analyze the metadata of the database result set, determine maximum lengths for each column and then pad appropriately when printing the results.
But we’re swimming in the waters of open source, here. A large community uses this language. As such, the odds are good that someone else has encountered the same requirement, and that a solution is readily available! Indeed, the tabulate package does exactly what we need: it prints out our data in an easy-to-read table in the terminal. If we were building a web page, we could use the same package, because it supports HTML format, too (as well as a bunch of others like Wikipedia-style markup).
Yep, tabulate is a handy package, providing a simple, usable solution to a common problem. It was easy to download the package and use it within our Python program.
But it doesn’t stop there. The Python Package Index has almost 100,000 packages, and thousands of additional programs with source are posted on GitHub. Some of these packages are incredibly powerful. Need to consume or create social media content? There are packages for that. One can post to Twitter or consume a feed in just a few lines of Python code, thanks to the twitter package. Code is also available to write Excel spreadsheets, connect to a Redis database, backup data to the cloud, talk to IBM Watson, and many, many more! Best of all, the community packages are derived from real-world needs.
Having all these packages at your fingertips means that your applications can do tons of powerful things, and it also has a great impact on the time to market. Many of these packages take just minutes to learn and use. That’s the power of community! While today’s sample is Python, large communities for other languages exist as well, like PHP, Ruby or Node.JS.
The Power of DB2 IntegrationOne of the key messages I like to convey about open source is that it adds value to DB2 by allowing you to leverage it in more ways than ever before. That’s why I also want to put the spotlight on our Python DB2 connector. The package makes it simple to integrate with your database from Python. Alan’s netstat utility uses it to do just that!
So let’s get nerdy for a minute and walk through the code. After importing the appropriate package, there’s a simple connect() method. Calling it with no arguments connects to the local database as the current user. Because our DB2 connector follows the Python standard specifications, you can now just follow the documentation provided at python.org. For instance, simply use the cursor() method to get a Cursor object, and now we can use any of the cursor methods. As you can see, not only has IBM provided new open-source languages, we’ve also added value of integration with your system.
While today’s example queries the database, it’s not just fetching a database file. It’s utilizing one of the many IBM i services that DB2 has delivered. These services let you retrieve or modify system information through simple SQL. What kind of information? The broad categories include PTFs, security, message handling, work management, storage, user management and more.
Sure, you could also get this information via IBM i APIs. In fact, we’ve delivered a toolkit to enable that, too (something I plan to highlight in a future post). So why use these services? For one, simplicity. Rather than IBM i proprietary APIs, you can use a more standard solution in SQL. It’s pretty easy to call a query and process the results. In this case, it’s even simpler; since we’re using a standard Python database cursor, the tabulate package traversed the results for us!
Depending on the type of service, the SQL “where” or “order by” clauses can implement sorting or filtering with ease. Our netstat command used the NETSTAT_JOB_INFO service to retrieve information about active connections with a humble query. If the user requested a specific port, it’s just another “where” clause.
They also enable you to push more of your logic into the database. Once you finalize the SQL of interest, it can be called from virtually any language. Also, you can tweak or create your queries with database tools, like the Run SQL Scripts function within IBM i Access Client Solutions, which is handy. By the way, did you know that you can get to plenty of IBM i Services examples from the “Insert from Examples” feature of Run SQL Scripts?
The offering set keeps growing, too. For instance, IBM i 7.3 TR2 and IBM i 7.2 TR6 announcements included enhancements or additions in the realms of security, message queues and more. To stay current with the latest offerings, you will need the appropriate DB2 group PTF installed, as documented on the IBM i services page (they are not necessarily delivered with the TR itself). For a quick visual of the latest offerings in IBM i Services (as well as DB2 for i Catalogs and Services), bookmark this link.
These services provide yet another device to make application development on IBM i easy. With an open-source language and some basic SQL skills, you can write powerful applications to meet all sorts of your IBM i-centric needs.
What’s This Mean?As we all know, the ‘i’ in “IBM i” stands for “integration,” a celebrated aspect of our platform’s heritage. Now, thanks in part to open source, we have more integration than we’ve ever had before. When we delivered new languages, we delivered integration with IBM i. The languages come with communities that have given us integration with cognitive technologies, social media, analytics, cloud and more. Today’s sample program is concise and illustrative of these points. The advantages of database integration and a greater community are messages with could mean a lot to your enterprise!
One last thing. In my previous post, I forgot to mention the links on the right-hand side of this page. For this blog, I chose links that can be great starting points for your open-source journey. If you haven’t done so already, come join our community and experience open source’s benefits for yourself!
Jesse Gorzinski is the business architect of open source technologies.More →