3 Advanced Features : Using DataDirect Bulk Load

Using DataDirect Bulk Load
The drivers support DataDirect Bulk Load, a feature that allows your application to send large numbers of rows of data to a database or Salesforce instance.
Table 3-9 and Table 3-10 describe the bulk load behavior for the drivers.
Oracle1
Microsoft SQL Server2
Sybase
The driver sends the data to the database in a continuous stream instead of numerous smaller database packets. Similar to batch operations, using bulk load improves performance because far fewer network round trips are required. Bulk load bypasses the data parsing usually done by the database, providing an additional performance gain over batch operations.
DB2
Informix
MySQL
Because DB2, Informix, and MySQL do not have native bulk load support, these drivers support bulk load through the standard batch mechanism.

1
Supports bulk load for Oracle9i R2 and higher.

2
Supports bulk load for Microsoft SQL Server 2000 and higher.

The driver sends data to a Salesforce instance using the Salesforce Bulk API instead of the Web Service API. Using the Bulk API significantly reduces the number of Web service calls the driver uses to transfer data and may improve performance.
IMPORTANT:
■
DataDirect Bulk Load requires a licensed installation of the drivers. If the drivers are installed with an evaluation license, the bulk load feature is available for prototyping with your applications, but with limited scope. Contact your sales representative or DataDirect SupportLink for further information.
■
Because a bulk load operation may bypass data integrity checks, your application must ensure that the data it is transferring does not violate integrity constraints in the database. For example, suppose you are bulk loading data into a database table and some of that data duplicates data stored as a primary key, which must be unique. The driver will not throw an exception to alert you to the error; your application must provide its own data integrity checks.
For all drivers that support bulk load, you can perform bulk load operations in either of the following ways:
■
Create a DDBulkLoad object and use the methods provided by the DDBulkLoad interface in the com.ddtek.jdbc.extensions package for bulk load. You may want to use this method if you are developing a new application that needs to perform bulk load operations. See “Using a DDBulkLoad Object”.
■
Configure the driver to use the bulk load protocol in the database when it encounters a request to execute a batch insert instead of the batch mechanism. This method allows existing applications to take advantage of bulk load without requiring changes to the application code. See “Using Bulk Load for Batch Inserts”.
In addition, for the Salesforce driver, you can configure the driver to use the bulk load protocol when it encounters a request to execute a single Insert, Update, or Delete statement that affects multiple rows. See “Using Bulk Load for Single Inserts/Updates/Deletes (Salesforce Driver)”.
Using a DDBulkLoad Object
To create a DDBulkLoad object, create an instance of the DDBulkLoadFactory class as shown in the following examples.
Example A: Creating a DDBulkLoad Object for an Oracle Connection
import com.ddtek.jdbc.extensions.*
// Get Database Connection
Connection con = DriverManager.getConnection(
"jdbc:datadirect:oracle://server3:1521;ServiceName=ORCL;User=test;Password=secret");
 
// Get a DDBulkLoad object
DDBulkLoad bulkLoad = com.ddtek.jdbc.extensions.DDBulkLoadFactory.getInstance(con);
Example B: Creating a DDBulkLoad Object for a Salesforce Connection
import com.ddtek.jdbc.extensions.*
// Get Salesforce Connection
Connection con = DriverManager.getConnection(
"jdbc:datadirect:sforce://login.salesforce.com;User=test@abccorp.com;Password=secret");
 
// Get a DDBulkLoad object
DDBulkLoad bulkLoad = com.ddtek.jdbc.extensions.DDBulkLoadFactory.getInstance(con);
Once your application has created a DDBulkLoad object, you can use the DDBulkLoad methods to provide the driver with the following instructions:
■
●
●
Comma-separated value (CSV) file. The driver can export data from a table or ResultSet object into a CSV file, or the driver can use a CSV file created by another DataDirect Connect product.
■
Example C: Loading Data Using a ResultSet Object
Suppose after upgrading from an Oracle 10g database to an Oracle 11g database, you need to migrate data from the old database to the new database. Figure 3-3 shows how your application would load the data from a ResultSet object created from a query. This scenario assumes that the Oracle driver is connected to both databases.
Figure 3-3. Loading Data Using a ResultSet Object
The figure shows how the driver bulk exports and loads the results of a query.
1
The application executes a query to the Oracle 10g database in the normal way and retrieves the results in a ResultSet object.
2
3
 
Example D: Loading Data Using a CSV File
Using the same scenario as described in Example C, Figure 3-4 shows how you would load the data using a CSV file instead of a ResultSet object. The CSV file is created by exporting data from an Oracle 10g table.
Figure 3-4. Loading Data Using a CSV File
The figure shows how the driver bulk exports and loads the results of a query.
1
2
3
The application instructs the driver to load data from the CSV file into the Oracle 11g table. The driver loads the data from the CSV file into the Oracle 11g table.
 
For instructions on exporting data to a CSV file, see “Exporting Data to a CSV File”.
For instructions on loading data, see “Loading Data From a ResultSet Object” and “Loading Data From a CSV File”.
See “Permissions for Bulk Load from a CSV File” for information about granting permissions for bulk loading from a CSV file.
Refer to Chapter 2 “JDBC Extensions” in the DataDirect Connect Series for JDBC Reference for a description of the bulk load methods.
Exporting Data to a CSV File
Using the methods provided for bulk load, the driver can export data from either of the following sources into a CSV file:
■
Database table or Salesforce table. Use the setTableName() method, specifying the table name. For example, to export data from a table named GBMAXTABLE to a file named tmp.csv, you would specify:
bulkLoad.setTableName("GBMAXTABLE");
and
bulkLoad.export("tmp.csv");
NOTE: Alternatively, you can create a file reference to the CSV file and use the export() method to specify the file reference:
File csvFile = new File ("tmp.csv");
bulkLoad.export(csvFile);
■
ResultSet object. Create a file reference to a CSV file, and use the export() method, specifying the ResultSet object and the file reference. For example, to export data from a ResultSet Object named rs to a file named tmp.csv, you would specify:
File csvFile = new File ("tmp.csv");
bulkLoad.export(rs, csvFile);
If the CSV file does not already exist, the driver creates it when the export() method is executed. The driver also creates a bulk load configuration file, which describes the structure of the CSV file. For more information about using CSV files, see “Using CSV Files”. Refer to Chapter 2 “JDBC Extensions” in the DataDirect Connect Series for JDBC Reference for more information about bulk load methods.
Loading Data From a ResultSet Object
Use the setTableName() method to specify the table to load the data. Then, use the load() method, specifying the ResultSet object. For example, to load data from a ResultSet object named rs into a table named GBMAXTABLE, you would specify:
bulkLoad.setTableName("GBMAXTABLE");
and
bulkLoad.load(rs);
This example loads the first column in the result set to the ColName1 column, the second column in the result set to the ColName2 column, and so on.
Use the BulkLoadBatchSize property to specify the number of rows the driver loads at a time when bulk loading data. Performance can be improved by increasing the number of rows the driver loads at a time because fewer network round trips are required. Be aware that increasing the number of rows that are loaded also causes the driver to consume more memory on the client. Refer to Chapter 2 “JDBC Extensions” in the DataDirect Connect Series for JDBC Reference for more information about bulk load methods.
Loading Data From a CSV File
Use the setTableName() method to specify the table to load the data. Then, use the load() method, specifying the CSV file. For example, to load data from a file named tmp.csv into a table named GBMAXTABLE, you would specify:
bulkLoad.setTableName("GBMAXTABLE");
and
bulkLoad.load("tmp.csv");
Alternatively, you can create a file reference to the CSV file, and use the load() method to specify the file reference:
File csvFile = new File("tmp.csv");
bulkLoad.load(csvFile);
For the Salesforce driver, you also can specify the columns to which you want to load the data. This example loads the first column in the CSV file to the ColName1 column, the second column in the CSV file to the ColName2 column, and the third column in the CSV file to the ColName3 column:
bulkLoad.setTableName("GBMAXTABLE(ColName1, ColName2, ColName3)");
and
bulkLoad.load("tmp.csv");
Use the BulkLoadBatchSize property to specify the number of rows the driver loads at a time when bulk loading data. Performance can be improved by increasing the number of rows the driver loads at a time because fewer network round trips are required. Be aware that increasing the number of rows that are loaded also causes the driver to consume more memory on the client. Refer to Chapter 2 “JDBC Extensions” in the DataDirect Connect Series for JDBC Reference for more information about bulk load methods.
Logging
If logging is enabled for bulk load, a log file records information for each bulk load operation. Logging is enabled by specifying a file name and location for the log file using the setLogFile() method.
The log file records the following types of information about each bulk load operation:
■
■
■
For example, the following log file shows that of 11 rows that were read, all successfully loaded:
/*----- Load Started: <Feb 25, 2009 11:20:09 AM EST>---------------------*/
Total number of rows read 11
Total number of rows successfully loaded 11
Total number of rows that failed to load 0
Example A: Enabling Logging on Windows
To enable logging using a log file named bulk_load.log located in the C:\temp directory, specify:
bulkLoad.setLogFile(C:\\temp\\bulk_load.log)
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\\bulk_load.log.
Example B: Enabling Logging on UNIX/Linux
To enable logging using a log file named bulk_load.log located in the /tmp directory, specify:
bulkLoad.setLogFile(/tmp/bulk_load.log)
Using Bulk Load for Batch Inserts
For all drivers, except the Salesforce driver, the driver uses the native bulk load protocol for database connections when the EnableBulkLoad property is set to true. For example, if you set the EnableBulkLoad property to true, the driver would use bulk load for the following batch insert request.
PreparedStatement ps = conn.prepareStatement(
   "INSERT INTO employees VALUES (?, ?, ?)");
for (n = 0; n < 100; n++) {
   ps.setString(name[n]);
   ps.setLong(id[n]);
   ps.setInt(salary[n]);
   ps.addBatch();
}
ps.executeBatch();
In some cases, the driver may not be able to use bulk load because of restrictions enforced by the bulk load protocol and will downgrade to a batch mechanism. For example, if the data being loaded has a data type that is not supported by the bulk load protocol, the driver cannot use bulk load, but will use the batch mechanism instead.
For the Salesforce driver, when the EnableBulkLoad property is set to true and the number of rows to be inserted in the batch is larger than BulkLoadThreshold, the driver uses the Salesforce Bulk API instead of the Web service API.
For all drivers, use the BulkLoadBatchSize property to specify the number of rows the driver loads at a time when bulk loading data. Performance can be improved by increasing the number of rows the driver loads at a time because fewer network round trips are required. Be aware that increasing the number of rows that are loaded also causes the driver to consume more memory on the client.
Using Bulk Load for Single Inserts/Updates/Deletes (Salesforce Driver)
When the EnableBulkLoad property is set to true, the driver uses the Salesforce Bulk API for single Insert, Update, and Delete statements if the number of rows affected by the operation exceeds the threshold set by the BulkLoadThreshold connection property. For example, if you set the EnableBulkLoad property to true and the BulkLoadThreshold property to 2000, executing the following statement would use the Bulk API if the number of rows returned by SELECT rowid, sys_name FROM account is more than 2000 rows.
INSERT INTO tmpAccounts(accountId, accountName)
SELECT rowid, sys_name FROM account