Go to content Go to navigation and search

Home

Current Oracle Spatial Blog Articles


Search

Browse

RSS / Atom

Email me

textpattern

Creative Commons License
All Blog Articles, Data Models and Free Source Code by Simon Greener, The SpatialDB Advisor is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Writing xSV (eg csv) files from within the Oracle database using Java and PL/SQL

Wednesday October 12 2011 at 17:51

My EXPORTER package + Java code has a function called WriteDelimitedTextfile that can be used to export any SQL SELECT statement to disk using Oracle database’s DBMS_JOB or DBMS_SCHEDULER.

Yes, you can export to disk via pure PL/SQL but I have added this function to my EXPORTER to create an integrated solution for exports that include SDO_GEOMETRY objects.

You will note that when exporting a SQL SELECT statement with an SDO_GEOMETRY object that you can specify the export text format for all the SDO_GEOMETRY objects in the result set. This format can be Well Known Text (WKT) or Geography Markup Language (GML) version 2 (version 3 can only be made available for Oracle databases supporting JVM 1.5 or above).

Here is an example of an export.

  1. BEGIN
  2.   codesys.exporter.WriteDelimitedTextfile(
  3.      p_sql             => 'SELECT ID,SPECIES,STATION_NUMBER,LENGTH,WEIGHT,SEX,MATURITY,WATER_CHECK,SYSDATE as export_date,GEOM FROM GUTDATA',
  4.      p_outputDirectory => 'C:\temp\',
  5.     p_fileName        => 'gutdata.csv',
  6.     p_FieldSeparator  => ',',
  7.     p_TextDelimiter   => '"',
  8.     p_DateFormat      => 'yyyy/MM/dd hh:mm:ss a',
  9.     p_geomFormat     => EXPORTER.c_WKT_FORMAT,
  10.     p_precision      => 3 );
  11. End;
  12. /

With the export file looking like….

ID,SPECIES,STATION_NUMBER,LENGTH,WEIGHT,SEX,MATURITY,WATER_CHECK,EXPORT_DATE,GEOM
204,2,6,218,121.1,“F”,“M”,“O”,“2011/10/12 06:01:33 PM”,“POINT (133.5381 -5.9327)”
205,2,2,383,199.5,“M”,“M”,“O”,“2011/10/12 06:01:33 PM”,“POINT (155.0342 -39.1499)”
206,7,21,338,116.2,“M”,“M”,“O”,“2011/10/12 06:01:33 PM”,“POINT (107.1845 -30.9735)”
207,2,28,379,258.5,“M”,“M”,“O”,“2011/10/12 06:01:33 PM”,“POINT (107.0394 -14.7986)”
208,1,11,452,276.5,“F”,“I”,“O”,“2011/10/12 06:01:33 PM”,“POINT (153.999 -45.8678)”
210,6,38,300,138.4,“F”,“M”,“O”,“2011/10/12 06:01:33 PM”,“POINT (158.1937 -42.6954)”
212,8,59,252,243.6,“M”,“I”,“O”,“2011/10/12 06:01:33 PM”,“POINT (142.0212 -5.7516)”
…..

Here is another example:

  1. CREATE TABLE source_data
  2.     ( x, y, z
  3.     , CONSTRAINT source_data_pk
  4.         PRIMARY KEY (x,y,z)
  5.     )
  6.     ORGANIZATION INDEX
  7.     AS
  8.        SELECT ROWNUM           AS x
  9.        ,      RPAD('x',50,'x') AS y
  10.        ,      RPAD('y',50,'y') AS z
  11.       FROM   dual
  12.       CONNECT BY ROWNUM <= 100000;
  13. -- Results
  14. TABLE SOURCE_DATA created.
  15. -- Display structure
  16. DESCRIBE source_data
  17. -- Results
  18. Name NULL     TYPE        
  19. ---- -------- ------------
  20. X    NOT NULL NUMBER      
  21. Y    NOT NULL VARCHAR2(50)
  22. Z    NOT NULL VARCHAR2(50)
  23. -- Show some data
  24. SELECT x,y,z FROM codesys.source_data;
  25. -- Results...
  26. X Y                                                  Z                                                  
  27. - -------------------------------------------------- --------------------------------------------------
  28. 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
  29. 2 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
  30. 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
  31. 4 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
  32. -- Now do export
  33. SET timing ON autotrace ON
  34. -- Results
  35. Autotrace Enabled
  36. Shows the execution plan AS well AS statistics OF the statement.
  37. -- Run Export
  38. BEGIN
  39.   codesys.exporter.WriteDelimitedTextfile(
  40.    p_sql             => 'select x,y,z from codesys.source_data',
  41.    p_outputDirectory => 'c:\temp',
  42.    p_fileName        => 'source_data.csv',
  43.    p_FieldSeparator  => ',',
  44.    p_TextDelimiter   => '"',
  45.    p_DateFormat      => codesys.EXPORTER.c_DATEFORMAT,
  46.    p_geomFormat      => codesys.EXPORTER.c_WKT_Format,
  47.    p_precision       => 3);
  48. END;
  49. /
  50. -- Results
  51. anonymous block completed
  52. Elapsed: 00:00:03.813
  53.    Statistics
  54. -----------------------------------------------------------
  55.                3  USER calls
  56.          9019392  physical READ total bytes
  57.                0  physical WRITE total bytes
  58.                0  spare statistic 3
  59.                0  commit cleanout failures: cannot pin
  60.                0  TBS Extension: bytes extended
  61.                0  total NUMBER OF times SMON posted
  62.                0  SMON posted FOR undo segment recovery
  63.                0  SMON posted FOR dropping temp segment
  64.                0  segment prealloc tasks
  65. -- Clean up
  66. SET timing off autotrace off
  67. -- Results
  68. Autotrace Disabled
  69. -- And ....
  70. DROP TABLE source_data purge;
  71. TABLE SOURCE_DATA dropped.

The first four rows in c:\temp\source_data.csv:

X,Y,Z
1.0,“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,“yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy”
2.0,“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,“yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy”
3.0,“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,“yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy”
4.0,“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,“yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy”

I hope this helps someone.

Creative Commons License

post this at del.icio.uspost this at Diggpost this at Technoratipost this at Redditpost this at Farkpost this at Yahoo! my webpost this at Windows Livepost this at Google Bookmarkspost this to Twitter

Comment