couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "DumpOracleDbToCouchDbPython" by DougShawhan
Date Fri, 25 Feb 2011 14:46:00 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "DumpOracleDbToCouchDbPython" page has been changed by DougShawhan.
http://wiki.apache.org/couchdb/DumpOracleDbToCouchDbPython?action=diff&rev1=8&rev2=9

--------------------------------------------------

  = Unique Values in SQL Table Converted to Individual Documents =
- Cro-Magnon simple python script which will take the contents of an SQL table and place it
into CouchDB. A somewhat better version can be found at https://github.com/lysdexia/Oracle2CouchDB
+ Cro-Magnon simple python script which will take the contents of an SQL table and place it
into CouchDB.
  
  This particular example depends on the SQL table having one unique field. The values in
the unique field become individual documents in the CouchDB database.
  
@@ -23, +23 @@

  # production anything at all! :-)
  # Should work for any db api 2 compliant sql database.
  # Script guaranteed 100% slower than christmas.
- # There is a somewhat better version at https://github.com/lysdexia/Oracle2CouchDB
  
  db_name = 'mydatabase'
  table_names = [ ('TABLE_NAME0', 'UNIQUE_FIELD'),
@@ -163, +162 @@

          cursor.close()
  }}}
  
+ = All Values from SQL Table Imported, Let CouchDB Assign Keys =
+ 
+ This is most likely what you want!
+ 
+ {{{#!python
+ #!/usr/bin/env python
+ # -*- coding: utf-8 -*-
+ # You can get this from https://github.com/lysdexia/Oracle2CouchDB
+ #################################################################
+ # USAGE: Oracle2CouchDB
+ # Edit the o2c.ini file, in the [oracle] section, supply your oracle
+ # authentication values, the oracle table to be cloned and an optional
+ # larger cursor_arraysize value. (A larger arraysize will often be 
+ # advantageous when using fetchmany())
+ # supply corresponding data for couchdb in the [couch] section
+ #################################################################
+ 
+ import cx_Oracle, datetime, sys, types, urllib2, ConfigParser, types
+ from subprocess import Popen, PIPE
+ import couchdb, couchdb.design
+ couchdb.json.use('cjson')
+ 
+ class OraSuck(object):
+     def get_connection(self, db):
+         self.connection = cx_Oracle.Connection(tns)
+         self.cursor = cx_Oracle.Cursor(self.connection)
+ 
+     def header(self, table_name):
+         self.cursor.execute("select * from %s where 1=0"%table_name)
+         return [i[0].lower() for i in self.cursor.description]
+ 
+     def sample(self):
+         rows = []
+         hdr = self.header(oratable)
+         cmd = """select %s from %s"""%(", ".join(hdr), oratable)
+ 
+         # if we have no specified arraysize, we'll use the fetchmany() default
+         # of fifty records.
+         if cursor_arraysize:
+             self.cursor.arraysize = int(cursor_arraysize)
+         self.cursor.prepare(cmd)
+         self.cursor.execute(cmd)
+         while True:
+             rows = [dict([(k, v) for k, v in zip(hdr, r)])
+                     for r in self.cursor.fetchmany()]
+             for row in rows:
+                 for key in row:
+                     # try to convert DATE to standard ISO format
+                     if hasattr(row[key], "isoformat"):
+                         row[key] = datetime.datetime.isoformat(row[key])
+             yield rows
+ 
+ class CouchBlow(object):
+ 
+     def connect(self):
+         couch = couchdb.Server(couch_server)
+ 
+         if not all([username, password]):
+             sys.exit("You'll need a username and password")
+ 
+         couch.resource.credentials = (username, password)
+         try:
+             db = couch[oratable]
+         except:
+             db = couch.create(oratable)
+         return db
+ 
+     # use ora.sample() generator to load db
+     def load(self):
+         get_rows = ora.sample()
+         while get_rows:
+             rows = get_rows.next()
+             for row in rows:
+                 try:
+                     self.db.save(row)
+                     print (row["searchtime"])
+                 except couchdb.http.ResourceConflict, error:
+                     err = "%s: %s"%(error.message)
+                     print ("%s: %s"%(row["searchtime"], err))
+ 
+ if __name__ == "__main__":
+     config = ConfigParser.ConfigParser()
+     config.optionxform = str
+     config.read("o2c.ini")
+ 
+     couch_server = config.get("couch", "couch_server")
+     username = config.get("couch", "username")
+     password = config.get("couch", "password")
+ 
+     oratable = config.get("oracle", "oratable")
+     oradb = config.get("oracle", "oradb")
+     tns = "@".join([config.get("oracle", "tns"), oradb])
+     cursor_arraysize = config.get("oracle", "cursor_arraysize")
+ 
+     cb = CouchBlow()
+     cb.db = cb.connect()
+ 
+     ora = OraSuck()
+     ora.get_connection(oradb)
+ 
+     cb.load()
+ }}}
+ 
+ Example .ini file
+ 
+ {{{#!python
+ [oracle]
+ tns = scott/tiger
+ oradb = mydb
+ oratable = mytable
+ cursor_arraysize = 256
+ 
+ [couch]
+ couch_server = http://my.couchdb:5984
+ username = dragon_lady
+ password = what part of get thee gone do you not understand
+ }}}
+ 

Mime
View raw message