17.00 - Code Example - Parallel Transporter

Teradata® Parallel Transporter Application Programming Interface Programmer Guide

prodname
Parallel Transporter
vrm_release
17.00
created_date
June 2020
category
Programming Reference
featnum
B035-2516-220K
#include "connection.h"
#include "schema.h"
#include "DMLGroup.h"

using namespace teradata::client::API;

int returnValue = 0;
char* errorMsg = NULL;
TD_ErrorType errorType;

cout << "*** Update Driver Example ***" << endl;

Connection *conn = new Connection();
/**********************************************
* Set Operator Type and Trace/Log Levels
**********************************************/
conn->AddAttribute(TD_SYSTEM_OPERATOR,TD_UPDATE);
//conn->AddAttribute(TD_TRACE_OUTPUT,"update.txt");
//conn->AddArrayAttribute(TD_TRACE_LEVEL,2,TD_OPER_ALL,TD_OFF,NULL);
/**********************************************
* Add Attributes
**********************************************/
conn->AddAttribute(TD_USER_NAME,"user");
conn->AddAttribute(TD_USER_PASSWORD,"password");
conn->AddAttribute(TD_TDP_ID,"database");
conn->AddAttribute(TD_LOG_TABLE,"tdupdate_log");

conn->AddArrayAttribute(TD_TARGET_TABLE, 2,"tdupdateA",   "tdupdateB", NULL);
conn->AddArrayAttribute(TD_WORK_TABLE  , 2,"tdupdateA_wt","tdupdateB_wt", NULL);
conn->AddArrayAttribute(TD_ERROR_TABLE_1,2,"tdupdateA_e1","tdupdateB_e1", NULL);
conn->AddArrayAttribute(TD_ERROR_TABLE_2,2,"tdupdateA_e2","tdupdateB_e2", NULL);
/**********************************************
* Add Schema
**********************************************/
Schema * schema = new Schema("input");
schema->AddColumn("Associate_Id",TD_INTEGER,4);
schema->AddColumn("Associate_Name",TD_CHAR,25);
schema->AddColumn("Salary",TD_FLOAT,8);
schema->AddColumn("DOJ",TD_DATE,4);
schema->AddColumn("Designation",TD_VARCHAR,25);
schema->AddColumn("Loan_Amount",TD_DECIMAL,4,5,2);
schema->AddColumn("Martial_Status",TD_CHAR,1);
schema->AddColumn("No_Of_Dependents",TD_BYTEINT,1);
conn->AddSchema(schema);
/**********************************************
* Set DMLGroups
**********************************************/
TD_Index dmlGroupIndex[2];

DMLGroup * dmlGr = new DMLGroup();
dmlGr->AddStatement("INSERT INTO tdupdateA( :Associate_Id, :Associate_Name, :Salary, :DOJ, :Designation, :Loan_Amount, :Martial_Status, :No_Of_Dependents);");
dmlGr->AddDMLOption(MARK_DUPLICATE_ROWS);
returnValue = conn->AddDMLGroup(dmlGr,&dmlGroupIndex[0]);
delete dmlGr;

dmlGr = new DMLGroup();
dmlGr->AddStatement("INSERT INTO tdupdateB( :Associate_Id, :Associate_Name, :Salary, :DOJ, :Designation, :Loan_Amount, :Martial_Status, :No_Of_Dependents);");
dmlGr->AddDMLOption(IGNORE_DUPLICATE_ROWS);
returnValue = conn->AddDMLGroup(dmlGr,&dmlGroupIndex[1]);
delete dmlGr;

cout << "DMLGroups added with status " << returnValue << endl;

if ( returnValue < TD_Error )
{
	/**********************************************
	* Initiate
	**********************************************/

	returnValue = conn->Initiate();

	cout << "Driver Initiated with status " << returnValue << endl;

	if( returnValue < TD_Error )
	{
		/**********************************************
		* Acquisition
		**********************************************/

		char rowBuffer[78]; /* must include the EOL byte */
		TD_Length bytes;
		int loadStatus = 0;

		//Get A Row of Data for Insertion
		loadStatus = GetRowData(rowBuffer,78);  // user function - reads in a
									     // row of data from a file
										// returns -1 when end offile
										// reached

		if( loadStatus != -1 ){
			bytes = *((unsigned short *)rowBuffer);

			//Insert the row twice using each DML group
			for( int i = 0; i < 2 ; i++ ){

				returnValue = conn->UseDMLGroups(&dmlGroupIndex[i],1);

				if( returnValue >= TD_Error){
					break;
				}

				cout << "Sending First Row" << endl;
				returnValue = conn->PutRow( rowBuffer + 2 , bytes);

				if( returnValue >= TD_Error){
					break;
				}

				cout << "Sending Duplicate Row" << endl;
				returnValue = conn->PutRow( rowBuffer + 2 , bytes);

				if( returnValue >= TD_Error){
					break;
				}

			}
		}

		cout << "Rows sent with status " << returnValue << endl;

		loadStatus = GetRowData(NULL,0); //user function - close file

		if( returnValue < TD_Error )
		{
			/**********************************************
			* End Acquisition
			**********************************************/

			returnValue = conn->EndAcquisition();

			cout << "Acquisition completed with status " << returnValue << endl;

			if( returnValue < TD_Error )
			{
				/**********************************************
				* Application
				**********************************************/

				returnValue = conn->ApplyRows();

				cout << "Application completed with status " << returnValue << endl;

				if ( returnValue < TD_Error )
				{
					cout << "Update completed successfully" << endl;

				}else{
					//Get Error Information
					cout << "Error occured during Application" << endl;
					conn->GetErrorInfo(&errorMsg,&errorType);
					if ( errorMsg != NULL ){
						cout << errorMsg << endl;
						cout << "Type: " << errorType << endl;
					}else{
						cout << "No Error Info Available" << endl;
					}
				}

			}else{
				//Get Error Information
				cout << "Error occured during EndAcquisition" << endl;
				conn->GetErrorInfo(&errorMsg,&errorType);
				if ( errorMsg != NULL ){
					cout << errorMsg << endl;
					cout << "Type: " << errorType << endl;
				}else{
					cout << "No Error Info Available" << endl;
				}
			}

		}else{
			//Get Error Information
			cout << "Error occured during Acquisition" << endl;
			conn->GetErrorInfo(&errorMsg,&errorType);
			if ( errorMsg != NULL ){
				cout << errorMsg << endl;
				cout << "Type: " << errorType << endl;
			}else{
				cout << "No Error Info Available" << endl;
			}
		}
	}else{
		//Get Error Information
		cout << "Error occured during Initiate" << endl;
		conn->GetErrorInfo(&errorMsg,&errorType);
		if ( errorMsg != NULL ){
			cout << errorMsg << endl;
			cout << "Type: " << errorType << endl;
		}else{
			cout << "No Error Info Available" << endl;
		}
	}
}else{
	//Get Error Information
	cout << "Error occured during AddDMLGroups" << endl;
	conn->GetErrorInfo(&errorMsg,&errorType);
	if ( errorMsg != NULL ){
		cout << errorMsg << endl;
		cout << "Type: " << errorType << endl;
	}else{
		cout << "No Error Info Available" << endl;
	}
}
/**********************************************
* Terminate
**********************************************/

returnValue = conn->Terminate();

cout << "Driver Terminated with status " << returnValue << endl;

if( returnValue >= TD_Error )
{
	//Get Error Information
	cout << "Error occured during Terminate" << endl;
	conn->GetErrorInfo(&errorMsg,&errorType);
	if ( errorMsg != NULL ){
		cout << errorMsg << endl;
		cout << "Type: " << errorType << endl;
	}else{
		cout << "No Error Info Available" << endl;
	}
}
/**********************************************
* Clean Up
**********************************************/

cout << "Deleting Objects" << endl;
delete schema;
delete conn;

cout << "*** Update Complete ***" << endl;