3 Advanced Features : Using CSV Files

Using CSV Files
As described in “Exporting Data to a CSV File”, the driver can create a CSV file by exporting data from a table or ResultSet object. For example, suppose you want to export data from a 4-column table named GBMAXTABLE into a CSV file. The contents of the CSV file, named GBMAXTABLE.csv, might look like the following example:
1,0x6263,"bc","bc"
2,0x636465,"cde","cde"
3,0x64656667,"defg","defg"
4,0x6566676869,"efghi","efghi"
5,0x666768696a6b,"fghijk","fghijk"
6,0x6768696a6b6c6d,"ghijklm","ghijklm"
7,0x68696a6b6c6d6e6f,"hijklmno","hijklmno"
8,0x696a6b6c6d6e6f7071,"ijklmnopq","ijklmnopq"
9,0x6a6b6c6d6e6f70717273,"jklmnopqrs","jklmnopqrs"
10,0x6b,"k","k"
Bulk Load Configuration File
Each time data is exported to a CSV file, a bulk load configuration file is created. This file has the same name as the CSV file, but with an .xml extension (for example, GBMAXTABLE.xml).
The bulk load configuration file defines in its metadata the names and data types of the columns in the CSV file based on those in the table or ResultSet object. It also defines other data properties, such as the source code page for character data types, precision and scale for numeric data types, and nullablity for all data types. The format of GBMAXTABLE.xml might look like the following example.
<?xml version="1.0" encoding="utf-8"?>
<table codepage="UTF-8" xsi:noNamespaceSchemaLocation=
"http://www.datadirect.com/ns/bulk/BulkData.xsd" xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance">
<row>
   <column datatype="DECIMAL" precision="38" scale="0" nullable="false">
      INTEGERCOL</column>
   <column datatype="VARBINARY" length="10" nullable="true">VARBINCOL</column>
   <column datatype="VARCHAR" length="10" sourcecodepage="Windows-1252"
      externalfilecodepage="Windows-1252" nullable="true">VCHARCOL</column>
   <column datatype="VARCHAR" length="10" sourcecodepage="Windows-1252"
      externalfilecodepage="Windows-1252" nullable="true">UNIVCHARCOL</column>
</row>
</table>
If the driver cannot read a bulk load configuration file (for example, because it was inadvertently deleted), the driver reads all data as character data. The character set used by the driver is UTF-8.
Bulk Load Configuration File Schema
The bulk load configuration file must conform to the bulk load configuration XML schema defined at the following Web site:
http://www.datadirect.com/ns/bulk/BulkData.xsd
The driver throws an exception if either of the following circumstances occur:
■
■
If the driver performs a bulk load operation and the driver detects that the CSV file does not comply with the XML Schema described in the bulk load configuration file
Verifying the Bulk Load Configuration File for Database Connections
Before performing a bulk load operation, your application can verify the metadata in the bulk load configuration file against the structure of the target table using the validateTableFromFile() method. This ensures that the data in the CSV file is compatible with the target table structure.
NOTE: The Salesforce driver does not support verifying the bulk load configuration file.
Because the verification does not check the actual data in the CSV file, it is possible that the load can fail even if the verification succeeds. For example, suppose your bulk load configuration file has an Integer column that matches an Integer column in the target database. if you modified the data for the Integer column in the CSV file to contain non-digit characters, the bulk load operation would fail even though a verification using validateTableFromFile() would succeed.
Not all error messages or warnings generated by the verification process mean that the load will fail. Some messages notify you about possible incompatibilities between the source and target tables. For example, if the CSV file has a column defined as an integer and the column in the target table is defined as smallint, the load may still succeed if the values in the source column are small enough to fit in a smallint column.
Character Set Conversions
When you export data from a database to a CSV file, the CSV file uses the same code page as the table from which the data was exported. If the CSV file and the target table use different code pages, performance for bulk load operations can suffer because the driver must perform a character set conversion.
To avoid character set conversions, your application can specify which code page to use for the CSV file when exporting data. You can specify any of the following code pages:
US_ASCII
ISO_8859_1
ISO_8859_2
ISO_8859_3
ISO_8859_4
ISO_8859_5
ISO_8859_6
ISO_8859_7
ISO_8859_8
ISO_8859_9
JIS_Encoding
Shift_JIS
EUC_JP
KS_C_5601
ISO_2022_KR
EUC_KR
ISO_2022_JP
GB2312
ISO_8859_13
ISO_8859_15
GBK
IBM850
IBM852
IBM437
IBM862
Big5
MACINTOSH
IBM037
IBM273
IBM277
IBM278
IBM280
IBM284
IBM285
IBM290
IBM297
IBM420
IBM424
IBM500
IBM851
IBM855
IBM857
IBM860
IBM861
IBM863
IBM864
IBM865
IBM869
IBM870
IBM871
IBM1026
KOI8_R
HZ_GB_2312
IBM866
IBM775
IBM00858
IBM01140
IBM01141
IBM01142
IBM01143
IBM01144
IBM01145
IBM01146
IBM01147
IBM01148
IBM01149
WINDOWS-1250
WINDOWS-1251
WINDOWS-1252
WINDOWS-1253
WINDOWS-1254
WINDOWS-1255
WINDOWS-1256
WINDOWS-1257
WINDOWS-1258
WINDOWS-854
IBM-939
IBM-943_P14A-2000
IBM-4396
IBM-5026
IBM-5035
UTF-8
UTF-16LE
UTF-16BE
For example, if the source database table uses a SHIFT-JIS code page and the target table uses a EUC-JP code page, specify setCodePage("EUC_JP") to ensure that the CSV file will use the same code page as the target table. If the code page you need to use is not listed, contact Technical Support to request support for that code page.
External Overflow Files
When you export data into a CSV file, you can choose to create one large file or multiple smaller files. For example, if you are exporting BLOB data that is a total of several GB, you may want to break the data that into multiple smaller files of 100 MB each.
If the values set by the setCharacterThreshold() or setBinaryThreshold() methods are exceeded, separate files are generated to store character or binary data, respectively. Overflow files are located in the same directory as the CSV file.
The format for overflow file names is:
CSV_file_name.xxxxxx.lob
where:
CSV_file_name is the name of the CSV file.
xxxxxx is a 6-digit number that increments an overflow file.
For example, if multiple overflow files are created for a CSV file named CSV1, the file names would look like this:
CSV1.000001.lob
CSV1.000002.lob
CSV1.000003.lob
...
If the overflow file contains character data, the code page used by the file is the code page specified in the bulk load configuration file for the CSV file.
Discard File
If the driver was unable to load rows into the database for a bulk load operation from a CSV file, it can record all the rows that were unable to be loaded into the database in the discard file. The contents of the discard file is in the same format as that of the CSV file. After fixing reported issues in the discard file, the bulk load can be reissued, using the discard file as the CSV file.
A discard file is created by specifying a file name and location for the discard file using the setDiscardFile() method.
Example A: Creating a Discard File on Windows
To create a discard file named discard.csv located in the C:\temp directory, specify:
bulkLoad.setDiscardFile(C:\\temp\\discard.csv)
NOTE: If coding a path on Windows to the log file in a Java string, the backslash character (\) must be preceded by the Java escape character, a backslash. For example: C:\\temp\\discard.csv.
Example B: Creating a Discard File on UNIX/Linux
To create a discard file named discard.csv located in the /tmp directory, specify:
bulkLoad.setDiscardFile(/tmp/discard.csv)