SlideShare a Scribd company logo
Maxym Kharchenko
Manage ORACLE databases
with Python
Whoami
■ Started as a database kernel developer with C and C++
■ ORACLE DBA for ~ 14 years: + SQL, PL/SQL and Perl
■ Persistence Engineer for the last 4: + Python, R and Scala
■ OCM, ORACLE Ace Associate
■ Blog: http://intermediatesql.com
■ Twitter: @maxymkh
Agenda
■ Talk about why Python is awesome
■ Design ORACLE multi db “ping” in Python
■ (hopefully) Demo it
The job of an engineer is
to make complex things simple
Why script outside the database ?
Do I have a good datafilebackup ?
SELECT …
FROM v$backup_datafile
 v$backup_datafile
 Backup files actually exist
 and can be validated
 Backup size is plausible
 On the end storage
 Network transfer as well
 No critical errors in logs
 Etc
Some things are
just difficult to do in a database
You will, generally, get better Zen
i := i + 1 i += 1
You will, generally, get better Zen
You will, generally, get better Zen
You, probably, have >1 database
ORACLE ORACLE
ORACLE ORACLE ORACLE
ORACLE ORACLE ORACLE
ORACLE MySql MySql
Postgres Cassandra
S3 S3
So, why Python ?
There should be one way to do it
And it should be obvious
Python enforces good coding practices
Python enforces good coding practices
foreach my $p (keys %$p1) {if(exists($p1->{$p})
and exists($p2->{$p})) {
if(uc($p1->{$p}) eq uc($p2->{$p})) {$diff-
>{SAME}->{$p} = 1;
} else {$diff->{DIFF}->{$p} = 1;
}}} elsif(! exists($p2->{$p})) {$diff-
>{ONLY_IN_1}->{$p} = 1;}}
Python enforces good coding practices
for p in db1_params:
if p in db2_params:
if db1_params[p] == db2_params[p]:
same_parameters.append(p)
else:
diff_parameters.append(p)
else:
only_in_1.append(p)
Python should come
with all the batteries included
Python interfaces to everything
import urllib
import json
url =
"http://api.openweathermap.org/data/2.5/weather?q=Seattle
,WA"
url_obj = urllib.urlopen(url)
data = url_obj.read().strip()
data = json.loads(data)
print(data['weather'])
Python interfaces to everything
import cx_Oracle
sql = "SELECT user FROM dual"
conn = cx_Oracle.connect('scott/tiger@orcl')
cursor = conn.cursor()
cursor.execute(sql)
db_user = cursor.fetchall()
Python interfaces to everything
import boto
from boto.s3.key import Key
s3 = boto.connect_s3()
bucket = s3.get_bucket('my_cats')
k.key = 'lazy_cat.jpg'
k.set_contents_from_filename('/tmp/lazy_cat.jpg')
Python is pretty popular
Python is pretty popular
If you know any scripting language
you (almost) know Python
Python is similar to other languages
def is_accessible(db_name):
""" Check if database is accessible """
ret = False
db_status = ping_db(db_name)
if "ACTIVE" == db_status:
ret = True
return ret
But: Spaces are first class citizens
def print_databases():
""" Print all databases from /etc/oratab """
with open("/etc/oratab") as file:
for line in file:
if line:
print line.strip().split(':')[0]
But: Spaces are first class citizens
def print_databases():
""" Print all databases from /etc/oratab ""“
with open("/etc/oratab") as file:
for line in file:
if line:
print line.strip().split(':')[0]
File "./a.py", line 7
print line.strip().split(':')[0]
^
IndentationError:
expected an indented block
Compare to Perl
sub print_databases() {
open(my $f, "<", "/etc/oratab")
or die ("Unable to open: $!");
while(my $line = <$f>) {
if ($line =~ /S+/) {
my @aItems = split(':', $line);
print $aItems[0] . "n";
}
}
close($f);
}
Compare to Perl
sub print_databases(){open(my $f, "<", "/etc/oratab") or
die ("Unable to open: $!"); while(my $line = <$f>) {
if ($line =~ /S+/) {my @aItems = split(':', $line);
print $aItems[0] . "n";}} close($f);}
Functions are first class citizens too
Fun with functions
def outer_function(parameter_function):
""" This function accepts function as a parameter """
def inner_function(inner_parameter):
""" This is a nested function """
return inner_parameter
# This returns a function from a function
return inner_function(parameter_function)
# This "calls" function return value as a function
print outer_function(external_function)()
Fun with functions: Decorators
def do_stuff():
result = heavy_processing()
def do_stuff():
start = time()
result = heavy_processing()
end = time()
print "Elapsed: %f" % (end-start)
def do_stuff2():
…
def do_stuff3076():
Fun with functions: Decorators
def timeit(func):
""" Generic time profiling function """
def time_exec(*args, **kwargs):
start_time = time()
ret = func(*args, **kwargs)
end = time()
print "Elapsed: %f" % (end-start)
return ret
return time_exec
Fun with functions: Decorators
do_stuff = timeit(do_stuff)
@timeit
def do_stuff():
…
@timeit
def do_stuff2():
…
@timeit
def do_stuff3076():
…
Learn to think Pythonian. It helps!
def print_databases():
file = open('/etc/oratab', 'r')
while True:
line = file.readline()
if len(line) == 0 and not line.endswith('n'):
break
print line.strip().split(':')[0]
file.close()
def print_databases():
with open('/etc/oratab') as file:
for line in file:
print line.strip().split(':')[0]
Python and ORACLE
Database “multi ping” tool
■ Ping ORACLE database
▪ Report AVAILABLE / NOT AVAILABLE status
■ Option to ping multiple databases
▪ Preferably in parallel
■ Bonus: track execution timing
Demo
cx_Oracle: Running SQL
import cx_Oracle
def is_db_alive(db_name):
is_alive = False
try:
conn = cx_Oracle.connect("user/password@%s" % db_name)
cursor = conn.cursor()
cursor.execute("SELECT user FROM dual")
except:
is_alive = False
else:
is_alive = True
return is_alive
Database ping
> dbping.py c15lv1
PING [c15lv1]: OK
> dbping.py c15lv2
PING [c15lv2]: UNAVAILABLE
Database “multi ping”
import dbping
def multi_dbping(db_list, ping_routine):
""" Ping all databases in a list """
for db in db_list:
ping_routine(db)
> dbping_list.py c15lv1 c15lv2 c15lv3
PING [c15lv1]: OK
PING [c15lv2]: UNAVAILABLE
PING [c15lv3]: OK
Parallel database “multi ping”
import multiprocessing
def parallel_ping(db_list, target=dbping.print_dbping):
""" Ping db_list databases in parallel """
jobs = []
for d in db_list:
p = multiprocessing.Process(
target=target, args=(d,)
)
jobs.append(p)
p.start()
for p in jobs:
p.join()
Parallel database “multi ping”
> dbping_parallel.py c15lv1 c15lv2 c15lv3 c15lv4
PING [c15lv1]: OK
PING [c15lv3]: OK
PING [c15lv4]: OK
PING [c15lv2]: UNAVAILABLE
Decorator: Execution Timing
def timeit(func):
""" Generic time profiling function """
def time_exec(*args, **kwargs):
start_time = time()
ret = func(*args, **kwargs)
ela = time() - start_time
print “tElapsed: %.3f seconds" % ela
return ret
return time_exec
Execution Timing:
> dbping_parallel_timing.py c15lv1 c15lv2 c15lv3
PING [c15lv3]: OK
Elapsed: 1.186 seconds
PING [c15lv1]: OK
Elapsed: 2.309 seconds
PING [c15lv2]: UNAVAILABLE
Elapsed: 22.511 seconds
@timeit
def print_dbping_with_timing(db_name):
return dbping.print_dbping(db_name)
How to start with Python
■ Lots of free resources on the web
▪ Tutorials
▪ Documentation
▪ Stackoverflow.com
▪ “Play” environments
▪ Even books
■ Python self documentation:
▪ dir()
▪ help()
Thank you!

More Related Content

PPTX
Commit2015 kharchenko - python generators - ext
PPTX
Visualizing ORACLE performance data with R @ #C16LV
PDF
When RegEx is not enough
PPTX
Naughty And Nice Bash Features
PDF
"PostgreSQL and Python" Lightning Talk @EuroPython2014
PDF
Programming with Python and PostgreSQL
PPT
Realtime Analytics Using MongoDB, Python, Gevent, and ZeroMQ
PDF
Python postgre sql a wonderful wedding
Commit2015 kharchenko - python generators - ext
Visualizing ORACLE performance data with R @ #C16LV
When RegEx is not enough
Naughty And Nice Bash Features
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Programming with Python and PostgreSQL
Realtime Analytics Using MongoDB, Python, Gevent, and ZeroMQ
Python postgre sql a wonderful wedding

What's hot (20)

PDF
KubeCon EU 2016: Custom Volume Plugins
PDF
Shell Script to Extract IP Address, MAC Address Information
PDF
PuppetDB, Puppet Explorer and puppetdbquery
PDF
File Space Usage Information and EMail Report - Shell Script
PDF
Redis as a message queue
PDF
Bash Script Disk Space Utilization Report and EMail
PPTX
Parse, scale to millions
PDF
Go memory
PDF
并发模型介绍
PPTX
Value protocols and codables
PDF
R Data Analysis/Rを使った人事データ分析入門
PPTX
All you need to know about the JavaScript event loop
PDF
Go Memory
PPTX
CouchDB Day NYC 2017: Full Text Search
PPTX
MySQL Slow Query log Monitoring using Beats & ELK
PDF
Go Profiling - John Graham-Cumming
PDF
Go debugging and troubleshooting tips - from real life lessons at SignalFx
PDF
node ffi
PPTX
App-o-Lockalypse now!
PDF
Impala: A Modern, Open-Source SQL Engine for Hadoop
KubeCon EU 2016: Custom Volume Plugins
Shell Script to Extract IP Address, MAC Address Information
PuppetDB, Puppet Explorer and puppetdbquery
File Space Usage Information and EMail Report - Shell Script
Redis as a message queue
Bash Script Disk Space Utilization Report and EMail
Parse, scale to millions
Go memory
并发模型介绍
Value protocols and codables
R Data Analysis/Rを使った人事データ分析入門
All you need to know about the JavaScript event loop
Go Memory
CouchDB Day NYC 2017: Full Text Search
MySQL Slow Query log Monitoring using Beats & ELK
Go Profiling - John Graham-Cumming
Go debugging and troubleshooting tips - from real life lessons at SignalFx
node ffi
App-o-Lockalypse now!
Impala: A Modern, Open-Source SQL Engine for Hadoop
Ad

Similar to 2015 555 kharchenko_ppt (20)

PDF
Develop Python Applications with MySQL Connector/Python
PPTX
Hitchhiker's Guide to free Oracle tuning tools
PPTX
Relational Database Access with Python ‘sans’ ORM
PDF
pandas.(to/from)_sql is simple but not fast
PPTX
Python and Oracle : allies for best of data management
PDF
Python and the MySQL Document Store
PDF
Con4445 jesus
PDF
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
PDF
Php Applications with Oracle by Kuassi Mensah
PPTX
Relational Database Access with Python
PDF
20180921_DOAG_BigDataDays_OracleSpatialandPython_kpatenge
PPTX
Python database interfaces
PDF
Oracle to Postgres Migration - part 1
PPTX
Database connectivity in python
PPTX
Database Connectivity using Python and MySQL
PDF
Pluggable Databases: What they will break and why you should use them anyway!
PDF
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...
PDF
MySQL Shell - The Best MySQL DBA Tool
PDF
MySQL Tech Café #8: MySQL 8.0 for Python Developers
PDF
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUG
Develop Python Applications with MySQL Connector/Python
Hitchhiker's Guide to free Oracle tuning tools
Relational Database Access with Python ‘sans’ ORM
pandas.(to/from)_sql is simple but not fast
Python and Oracle : allies for best of data management
Python and the MySQL Document Store
Con4445 jesus
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
Php Applications with Oracle by Kuassi Mensah
Relational Database Access with Python
20180921_DOAG_BigDataDays_OracleSpatialandPython_kpatenge
Python database interfaces
Oracle to Postgres Migration - part 1
Database connectivity in python
Database Connectivity using Python and MySQL
Pluggable Databases: What they will break and why you should use them anyway!
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...
MySQL Shell - The Best MySQL DBA Tool
MySQL Tech Café #8: MySQL 8.0 for Python Developers
Embracing Database Diversity: The New Oracle / MySQL DBA - UKOUG
Ad

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
System and Network Administration Chapter 2
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPT
Introduction Database Management System for Course Database
PPTX
history of c programming in notes for students .pptx
top salesforce developer skills in 2025.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
System and Network Administration Chapter 2
Upgrade and Innovation Strategies for SAP ERP Customers
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Which alternative to Crystal Reports is best for small or large businesses.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
ISO 45001 Occupational Health and Safety Management System
PTS Company Brochure 2025 (1).pdf.......
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administraation Chapter 3
Wondershare Filmora 15 Crack With Activation Key [2025
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
VVF-Customer-Presentation2025-Ver1.9.pptx
Design an Analysis of Algorithms II-SECS-1021-03
Odoo Companies in India – Driving Business Transformation.pdf
Introduction Database Management System for Course Database
history of c programming in notes for students .pptx

2015 555 kharchenko_ppt

  • 1. Maxym Kharchenko Manage ORACLE databases with Python
  • 2. Whoami ■ Started as a database kernel developer with C and C++ ■ ORACLE DBA for ~ 14 years: + SQL, PL/SQL and Perl ■ Persistence Engineer for the last 4: + Python, R and Scala ■ OCM, ORACLE Ace Associate ■ Blog: http://intermediatesql.com ■ Twitter: @maxymkh
  • 3. Agenda ■ Talk about why Python is awesome ■ Design ORACLE multi db “ping” in Python ■ (hopefully) Demo it
  • 4. The job of an engineer is to make complex things simple
  • 5. Why script outside the database ?
  • 6. Do I have a good datafilebackup ? SELECT … FROM v$backup_datafile  v$backup_datafile  Backup files actually exist  and can be validated  Backup size is plausible  On the end storage  Network transfer as well  No critical errors in logs  Etc
  • 7. Some things are just difficult to do in a database
  • 8. You will, generally, get better Zen i := i + 1 i += 1
  • 9. You will, generally, get better Zen
  • 10. You will, generally, get better Zen
  • 11. You, probably, have >1 database ORACLE ORACLE ORACLE ORACLE ORACLE ORACLE ORACLE ORACLE ORACLE MySql MySql Postgres Cassandra S3 S3
  • 13. There should be one way to do it And it should be obvious
  • 14. Python enforces good coding practices
  • 15. Python enforces good coding practices foreach my $p (keys %$p1) {if(exists($p1->{$p}) and exists($p2->{$p})) { if(uc($p1->{$p}) eq uc($p2->{$p})) {$diff- >{SAME}->{$p} = 1; } else {$diff->{DIFF}->{$p} = 1; }}} elsif(! exists($p2->{$p})) {$diff- >{ONLY_IN_1}->{$p} = 1;}}
  • 16. Python enforces good coding practices for p in db1_params: if p in db2_params: if db1_params[p] == db2_params[p]: same_parameters.append(p) else: diff_parameters.append(p) else: only_in_1.append(p)
  • 17. Python should come with all the batteries included
  • 18. Python interfaces to everything import urllib import json url = "http://api.openweathermap.org/data/2.5/weather?q=Seattle ,WA" url_obj = urllib.urlopen(url) data = url_obj.read().strip() data = json.loads(data) print(data['weather'])
  • 19. Python interfaces to everything import cx_Oracle sql = "SELECT user FROM dual" conn = cx_Oracle.connect('scott/tiger@orcl') cursor = conn.cursor() cursor.execute(sql) db_user = cursor.fetchall()
  • 20. Python interfaces to everything import boto from boto.s3.key import Key s3 = boto.connect_s3() bucket = s3.get_bucket('my_cats') k.key = 'lazy_cat.jpg' k.set_contents_from_filename('/tmp/lazy_cat.jpg')
  • 21. Python is pretty popular
  • 22. Python is pretty popular
  • 23. If you know any scripting language you (almost) know Python
  • 24. Python is similar to other languages def is_accessible(db_name): """ Check if database is accessible """ ret = False db_status = ping_db(db_name) if "ACTIVE" == db_status: ret = True return ret
  • 25. But: Spaces are first class citizens def print_databases(): """ Print all databases from /etc/oratab """ with open("/etc/oratab") as file: for line in file: if line: print line.strip().split(':')[0]
  • 26. But: Spaces are first class citizens def print_databases(): """ Print all databases from /etc/oratab ""“ with open("/etc/oratab") as file: for line in file: if line: print line.strip().split(':')[0] File "./a.py", line 7 print line.strip().split(':')[0] ^ IndentationError: expected an indented block
  • 27. Compare to Perl sub print_databases() { open(my $f, "<", "/etc/oratab") or die ("Unable to open: $!"); while(my $line = <$f>) { if ($line =~ /S+/) { my @aItems = split(':', $line); print $aItems[0] . "n"; } } close($f); }
  • 28. Compare to Perl sub print_databases(){open(my $f, "<", "/etc/oratab") or die ("Unable to open: $!"); while(my $line = <$f>) { if ($line =~ /S+/) {my @aItems = split(':', $line); print $aItems[0] . "n";}} close($f);}
  • 29. Functions are first class citizens too
  • 30. Fun with functions def outer_function(parameter_function): """ This function accepts function as a parameter """ def inner_function(inner_parameter): """ This is a nested function """ return inner_parameter # This returns a function from a function return inner_function(parameter_function) # This "calls" function return value as a function print outer_function(external_function)()
  • 31. Fun with functions: Decorators def do_stuff(): result = heavy_processing() def do_stuff(): start = time() result = heavy_processing() end = time() print "Elapsed: %f" % (end-start) def do_stuff2(): … def do_stuff3076():
  • 32. Fun with functions: Decorators def timeit(func): """ Generic time profiling function """ def time_exec(*args, **kwargs): start_time = time() ret = func(*args, **kwargs) end = time() print "Elapsed: %f" % (end-start) return ret return time_exec
  • 33. Fun with functions: Decorators do_stuff = timeit(do_stuff) @timeit def do_stuff(): … @timeit def do_stuff2(): … @timeit def do_stuff3076(): …
  • 34. Learn to think Pythonian. It helps! def print_databases(): file = open('/etc/oratab', 'r') while True: line = file.readline() if len(line) == 0 and not line.endswith('n'): break print line.strip().split(':')[0] file.close() def print_databases(): with open('/etc/oratab') as file: for line in file: print line.strip().split(':')[0]
  • 36. Database “multi ping” tool ■ Ping ORACLE database ▪ Report AVAILABLE / NOT AVAILABLE status ■ Option to ping multiple databases ▪ Preferably in parallel ■ Bonus: track execution timing
  • 37. Demo
  • 38. cx_Oracle: Running SQL import cx_Oracle def is_db_alive(db_name): is_alive = False try: conn = cx_Oracle.connect("user/password@%s" % db_name) cursor = conn.cursor() cursor.execute("SELECT user FROM dual") except: is_alive = False else: is_alive = True return is_alive
  • 39. Database ping > dbping.py c15lv1 PING [c15lv1]: OK > dbping.py c15lv2 PING [c15lv2]: UNAVAILABLE
  • 40. Database “multi ping” import dbping def multi_dbping(db_list, ping_routine): """ Ping all databases in a list """ for db in db_list: ping_routine(db) > dbping_list.py c15lv1 c15lv2 c15lv3 PING [c15lv1]: OK PING [c15lv2]: UNAVAILABLE PING [c15lv3]: OK
  • 41. Parallel database “multi ping” import multiprocessing def parallel_ping(db_list, target=dbping.print_dbping): """ Ping db_list databases in parallel """ jobs = [] for d in db_list: p = multiprocessing.Process( target=target, args=(d,) ) jobs.append(p) p.start() for p in jobs: p.join()
  • 42. Parallel database “multi ping” > dbping_parallel.py c15lv1 c15lv2 c15lv3 c15lv4 PING [c15lv1]: OK PING [c15lv3]: OK PING [c15lv4]: OK PING [c15lv2]: UNAVAILABLE
  • 43. Decorator: Execution Timing def timeit(func): """ Generic time profiling function """ def time_exec(*args, **kwargs): start_time = time() ret = func(*args, **kwargs) ela = time() - start_time print “tElapsed: %.3f seconds" % ela return ret return time_exec
  • 44. Execution Timing: > dbping_parallel_timing.py c15lv1 c15lv2 c15lv3 PING [c15lv3]: OK Elapsed: 1.186 seconds PING [c15lv1]: OK Elapsed: 2.309 seconds PING [c15lv2]: UNAVAILABLE Elapsed: 22.511 seconds @timeit def print_dbping_with_timing(db_name): return dbping.print_dbping(db_name)
  • 45. How to start with Python ■ Lots of free resources on the web ▪ Tutorials ▪ Documentation ▪ Stackoverflow.com ▪ “Play” environments ▪ Even books ■ Python self documentation: ▪ dir() ▪ help()

Editor's Notes

  • #8: Rupe Goldberg machine