/
CacheLookupValues.java
52 lines (40 loc) · 2 KB
/
CacheLookupValues.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.northconcepts.datapipeline.examples.cookbook;
import java.io.File;
import java.sql.Connection;
import java.sql.Driver;
import java.util.Properties;
import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.FieldList;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.transform.TransformingReader;
import com.northconcepts.datapipeline.transform.lookup.CachedLookup;
import com.northconcepts.datapipeline.transform.lookup.JdbcLookup;
import com.northconcepts.datapipeline.transform.lookup.Lookup;
import com.northconcepts.datapipeline.transform.lookup.LookupTransformer;
public class CacheLookupValues {
public static void main(String[] args) throws Throwable {
// connect to the database
Driver driver = (Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Properties properties = new Properties();
properties.put("user", "scott");
properties.put("password", "tiger");
Connection connection = driver.connect("jdbc:odbc:dp-cookbook", properties);
DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
.setFieldNamesInFirstRow(true);
/* This lookup matches
* [example/data/input/credit-balance-01.csv].[Rating] to [dp_rating].[rating_code]
* to return
* [dp_rating].[rating_description]
*/
Lookup lookup = new JdbcLookup(connection,
"SELECT rating_decription FROM dp_rating WHERE rating_code=?");
// cache JdbcLookup
lookup = new CachedLookup(lookup);
reader = new TransformingReader(reader)
.add(new LookupTransformer(new FieldList("Rating"), lookup));
Job.run(reader, new StreamWriter(System.out));
connection.close();
}
}