Download Generating Unique and Random Primary Keys with a Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Data vault modeling wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Operational transformation wikipedia , lookup

Transcript
Generating Unique and Random Primary Keys
with a Java Transformation
© 2011 Informatica Corporation. No part of this document may be reproduced or transmitted in any form, by any means
(electronic, photocopying, recording or otherwise) without prior consent of Informatica Corporation.
Abstract
The primary key of a relational table uniquely identifies each record in the table. A unique and random primary key within a
range provides an efficient way to access the database when the table is clustered based on the primary key.This article
describes how customers can use a Java transformation in PowerCenter to generate unique and random numeric primary
keys for a relational target within a range.
Supported Versions
¨ PowerCenter 9.1.0
Table of Contents
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Importing the Java Transformation into the Repository. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Example Java Transformation to Generate Unique and Random Primary Keys. . . . . . . . . . . . . . . . . . . . . . . . 3
Source File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Java Transformation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Lookup Transformation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Target Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Java Source Code for Java Transformation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Import Packages Tab. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Helper Code Tab. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
On Input Row Tab. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Overview
You can include a primary key that is unique and random within a range for a relational database table. A unique primary
key allows child records in other tables to point uniquely to the parent record in the target table. A random key within a range
provides an efficient way to access the database when the table is clustered based on the primary key.
You can use a Java transformation to generate unique and random numeric keys within a specific range. You can use the
numeric keys for the primary key field of a target table.
For example, you have order records in a flat file. You need to assign unique, random primary keys to each record and load
them into a relational target. You can use a Java transformation to generate and assign the keys.
Importing the Java Transformation into the Repository
You can download the files that provide a sample source flat file, Java transformation, and target objects from the following
location: https://communities.informatica.com/docs/DOC-5848
After you download the files, save the files in your local storage. Import the Java transformation xml file into a repository
using the PowerCenter Repository Manager. Use the sample source flat file, Java transformation, and target objects to
generate unique and random primary keys when you load data into the target table.
2
Example Java Transformation to Generate Unique and Random
Primary Keys
You can use a Java transformation to generate unique and random primary keys within a specific range.
This example shows a mapping that takes rows from a flat file and passes it to a Java transformation. The Java
transformation generates a random numeric key value within a specified range for each row of data and passes the value to
an unconnected cached Lookup transformation.
An unconnected cached Lookup transformation can look up key values in a table of used keys and verifies that key is
unique. If the Lookup transformation returns a value, the primary key exists in the target table. The Java transformation
generates and tests a new key value.
If the Lookup transformation returns a null value, it compares the candidate key to values in a synchronized array list that
maintains the keys generated during the current mapping run. This array list stores keys generated during the current
mapping run, since these keys may not show up in the lookup if they are not yet committed or if the lookup is cached.
If the primary key value does not exist in this synchronized array list, the Java transformation inserts the key value into the
list and passes it out of the transformation for use as a primary key.
You can insert the key in the target for the primary key field and store it in a separate table of used keys. If the key does
exist in the array list, the Java transformation rejects it. The Java transformation generates a new primary key value and
repeats the process.
The following figure shows the Java transformation mapping:
Source File
The input source file is a flat file that contains a rows of records with no unique identifiers. You have to insert each row with a
unique and random primary key when you load the target relational table.
You have the following source data in the flat file:
RecordType
record1
record1
record2
record1
record1
record2
record1
record1
record2
record2
3
RecordValue
1
2
100
1
2
100
1
2
100
100
Java Transformation
You can use the Java transformation to generate numeric primary key values that are unique and random in a specific range.
You can view the Java transformation and the ports from the ports tab in the Mapping Designer. The following table
describes the ports for the Java transformation:
Name
Port
Datatype
Precision
RecordType
Input and Output
string
7
RecordValue
Input and Output
decimal
4
Key
Output
decimal
15
Lookup Transformation
You can use the unconnected cached Lookup transformation to verify that the keys generated by Java transformation are
not in the target table.
You can view the Lookup transformation and the ports from the ports tab in the Mapping Designer. The following table
describes the ports for the Lookup transformation:
Name
Port
Datatype
Precision
KEY
Output and Lookup
decimal
15
InputKey
Input and Output
decimal
15
Target Objects
The mapping contains two relational targets. The Java transformation loads source data into the taget table REC_TYPE1
with a unique and random primary key for each row of data. The Java transformation also loads used keys to second target
table, USED_KEYS.
The following table describes the ports of the target table called REC_TYPE1:
Name
Port
Datatype
Precision
Description
Key
Input
number(p,s)
15
Primary Key
Value
Input
number(p,s)
38
Input source data
The following table describes the ports of the look up table for used keys called USED_KEYS:
4
Name
Port
Datatype
Precision
Description
Key
Input
number(p,s)
15
Used Primary key value
Java Source Code for Java Transformation
You can define the Java transformation logic in snippets of Java code on the Java Code tab.
The following code entry tabs contain the Java code snippets that define the logic for the Java transformation:
¨ Import Packages Tab
¨ Helper Code Tab
¨ On Input Row Tab
Import Packages Tab
Enter the Java packages to import.
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
Helper Code Tab
Declare static and non-static partition-level variables and functions.
static List<Double> usedNumbers = Collections.synchronizedList(new ArrayList<Double>());
On Input Row Tab
Enter code to process the input row. You can access an input column data by referring to the input column name. You can
set an output column data by referring to the output column name. You can define the range using min and max values. The
min defines the minimum key and max defines the maximum key.
long min = 100000000000000l;
long max = 999999999999999l;
long range = max - min +1;
long fraction = (long) (range * (new Random()).nextDouble());
double randNumber = (long)(fraction + min);
logInfo("*****Try " + randNumber);
boolean done = false;
while(!done) {
Double lkupValue = ((Double)invokeJExpression (":LKP.LKP_KEY(X1)", new Object [] {randNumber}));
logInfo("*****Returned was: " + lkupValue);
synchronized(usedNumbers) {
if(lkupValue == null && !usedNumbers.contains(randNumber)) {
logInfo("*****Number is good, not found.");
usedNumbers.add(randNumber);
done = true;
} else {
fraction = (long) (range * (new Random()).nextDouble());
randNumber = (long)(fraction + min);
logInfo("*****Got collision, try " + randNumber);
}
}
}
Key = randNumber;
Author
Srilakshmi Sitaraman
Technical Writer
Acknowledgements
The author would like to acknowledge Wayne Pullam, Senior Sales Consultant, for his technical assistance.
5