Skip to content

Commit 202d486

Browse files
#789 - Created new tables;#801 - Closed connections; #796 - Created temp files properly
1 parent 8a3f4f9 commit 202d486

File tree

5 files changed

+48
-71
lines changed

5 files changed

+48
-71
lines changed

src/main/java/com/marklogic/client/example/cookbook/datamovement/BulkExportOpticResults.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class BulkExportOpticResults {
5252
private int batchSize = 3;
5353
private int threadCount = 3;
5454
private String schemaDB = "Schemas";
55-
private String outputFile = "/tmp/opticExampleOutput.txt";
5655
private String customerTemplateFile = "/opticExample/customer.tdex";
5756
private String orderTemplateFile = "/opticExample/order.tdex";
5857
private DatabaseClient schemaDBclient = DatabaseClientSingleton.getAdmin(schemaDB);
@@ -192,7 +191,6 @@ private void exportWithOptic(int productID) throws IOException {
192191
// Create a Row manager to construct plans and query on
193192
// rows projected from the documents
194193
RowManager rowMgr = client.newRowManager();
195-
try (FileWriter writer = new FileWriter(outputFile)) {
196194
// Create a QueryBatcher to get the uris from the database which match the
197195
// query definition and pass it to the OpticExportToWriterListener for
198196
// creating the Optic API plans and writing the results of the plan to the
@@ -237,7 +235,6 @@ private void exportWithOptic(int productID) throws IOException {
237235
// Wait till the batch completes
238236
queryBatcher.awaitCompletion();
239237
moveMgr.stopJob(queryBatcher);
240-
}
241238
}
242239

243240
private void tearDown() {

src/main/java/com/marklogic/client/example/cookbook/datamovement/BulkExportOpticResultsToWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class BulkExportOpticResultsToWriter {
5959
private int batchSize = 3;
6060
private int threadCount = 3;
6161
private String schemaDB = "Schemas";
62-
private String outputFile = "/tmp/opticExampleOutput.txt";
62+
private String outputFile = System.getProperty("java.io.tmpdir") + "opticExampleOutput.txt";
6363
private String customerTemplateFile = "/opticExample/customer.tdex";
6464
private String orderTemplateFile = "/opticExample/order.tdex";
6565
private DatabaseClient schemaDBclient = DatabaseClientSingleton.getAdmin(schemaDB);
@@ -199,6 +199,7 @@ private void exportWithOptic(int productID) throws IOException {
199199
// Create a Row manager to construct plans and query on
200200
// rows projected from the documents
201201
RowManager rowMgr = client.newRowManager();
202+
System.out.println("Writing the results to " + outputFile);
202203
try (FileWriter writer = new FileWriter(outputFile)) {
203204
// Create a Function to pass to the OpticExportToWriterListener which
204205
// would take each batch and do the necessary optic operations and return

src/main/java/com/marklogic/client/example/cookbook/datamovement/BulkExportToJdbc.java

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.slf4j.LoggerFactory;
4444

4545
import java.io.IOException;
46+
import java.sql.Connection;
4647
import java.sql.ResultSet;
4748
import java.sql.SQLException;
4849
import java.util.Calendar;
@@ -70,12 +71,6 @@ public void run() throws IOException, SQLException {
7071
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
7172
final boolean isMySQLDB;
7273
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
73-
if (jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName().toLowerCase()
74-
.contains("hsql")) {
75-
isMySQLDB = false;
76-
} else {
77-
isMySQLDB = true;
78-
}
7974
StructuredQueryDefinition query = new StructuredQueryBuilder().directory(true, "/employees/");
8075
QueryBatcher qb = moveMgr.newQueryBatcher(query)
8176
.withBatchSize(batchSize)
@@ -85,72 +80,28 @@ public void run() throws IOException, SQLException {
8580
.withConsistentSnapshot()
8681
.onDocumentReady(record -> {
8782
Employee employee = record.getContentAs(Employee.class);
88-
if(isMySQLDB) {
89-
jdbcTemplate.update(
90-
"INSERT INTO employees (emp_no, hire_date, first_name, last_name, birth_date) " +
91-
"VALUES (?, ?, ?, ?, ?) " +
92-
"ON DUPLICATE KEY UPDATE emp_no=?, hire_date=?, first_name=?, last_name=?, birth_date=?",
93-
employee.getEmployeeId(), employee.getHireDate(), employee.getFirstName(),
94-
employee.getLastName(), employee.getBirthDate(),
95-
employee.getEmployeeId(), employee.getHireDate(), employee.getFirstName(),
96-
employee.getLastName(), employee.getBirthDate());
97-
} else {
98-
jdbcTemplate.update(
99-
"MERGE INTO employees USING (VALUES ?, ?, ?, ?, ?, ?) " +
100-
"temp (emp_no, hire_date, first_name, last_name, gender, birth_date) " +
101-
"ON employees.emp_no = temp.emp_no " +
102-
"WHEN MATCHED THEN UPDATE SET employees.hire_date=temp.hire_date, " +
103-
"employees.first_name=temp.first_name, employees.last_name=temp.last_name, " +
104-
"employees.gender = temp.gender, employees.birth_date=temp.birth_date " +
105-
"WHEN NOT MATCHED THEN INSERT (emp_no, hire_date, first_name, last_name, gender, birth_date) "
106-
+ "VALUES (temp.emp_no, temp.hire_date, temp.first_name, temp.last_name, temp.gender, temp.birth_date) ",
107-
employee.getEmployeeId(), dateFormat.format(employee.getHireDate().getTime()), employee.getFirstName(),
108-
employee.getLastName(), employee.getGender() == Gender.MALE ? "M" : "F",
109-
dateFormat.format(employee.getBirthDate().getTime()));
110-
}
83+
jdbcTemplate.update(
84+
"INSERT INTO employees_export (emp_no, hire_date, first_name, last_name, gender, birth_date) " +
85+
"VALUES (?, ?, ?, ?, ?, ?) ",
86+
employee.getEmployeeId(), dateFormat.format(employee.getHireDate().getTime()), employee.getFirstName(),
87+
employee.getLastName(), employee.getGender() == Gender.MALE ? "M" : "F",
88+
dateFormat.format(employee.getBirthDate().getTime()));
11189
if ( employee.getSalaries() != null ) {
11290
for ( Salary salary : employee.getSalaries() ) {
113-
if(isMySQLDB) {
114-
jdbcTemplate.update(
115-
"INSERT INTO salaries (emp_no, salary, from_date, to_date) " +
116-
"VALUES(?, ?, ?, ?)" +
117-
"ON DUPLICATE KEY UPDATE emp_no=?, salary=?, from_date=?, to_date=?",
118-
employee.getEmployeeId(), salary.getSalary(), salary.getFromDate(), salary.getToDate(),
119-
employee.getEmployeeId(), salary.getSalary(), salary.getFromDate(), salary.getToDate());
120-
} else {
121-
jdbcTemplate.update(
122-
"MERGE INTO salaries USING (VALUES ?, ?, ?, ?) " +
123-
"temp (emp_no, salary, from_date, to_date) " +
124-
"ON salaries.emp_no = temp.emp_no AND salaries.from_date = temp.from_date " +
125-
"WHEN MATCHED THEN UPDATE SET salaries.salary=temp.salary, " +
126-
"salaries.to_date=temp.to_date " +
127-
"WHEN NOT MATCHED THEN INSERT (emp_no, salary, from_date, to_date) " +
128-
"VALUES (temp.emp_no, temp.salary, temp.from_date, temp.to_date)",
129-
employee.getEmployeeId(), salary.getSalary(), dateFormat.format(salary.getFromDate().getTime()),
130-
dateFormat.format(salary.getToDate().getTime()));
131-
}
91+
jdbcTemplate.update(
92+
"INSERT INTO salaries_export (emp_no, salary, from_date, to_date) " +
93+
"VALUES(?, ?, ?, ?)",
94+
employee.getEmployeeId(), salary.getSalary(), dateFormat.format(salary.getFromDate().getTime()),
95+
dateFormat.format(salary.getToDate().getTime()));
13296
}
13397
}
13498
if ( employee.getTitles() != null ) {
13599
for ( Title title : employee.getTitles() ) {
136-
if(isMySQLDB) {
137-
jdbcTemplate.update(
138-
"INSERT INTO titles (emp_no, title, from_date, to_date) " +
139-
"VALUES(?, ?, ?, ?)" +
140-
"ON DUPLICATE KEY UPDATE emp_no=?, title=?, from_date=?, to_date=?",
141-
employee.getEmployeeId(), title.getTitle(), title.getFromDate(), title.getToDate(),
142-
employee.getEmployeeId(), title.getTitle(), title.getFromDate(), title.getToDate());
143-
} else {
144-
jdbcTemplate.update(
145-
"MERGE INTO titles USING (VALUES ?, ?, ?, ?) " +
146-
"temp (emp_no, title, from_date, to_date) " +
147-
"ON titles.emp_no = temp.emp_no AND titles.title = temp.title AND titles.from_date=temp.from_date " +
148-
"WHEN MATCHED THEN UPDATE SET titles.to_date=temp.to_date " +
149-
"WHEN NOT MATCHED THEN INSERT (emp_no, title, from_date, to_date) " +
150-
"VALUES (temp.emp_no, temp.title, temp.from_date, temp.to_date)",
151-
employee.getEmployeeId(), title.getTitle(), dateFormat.format(title.getFromDate().getTime()),
152-
dateFormat.format(title.getToDate().getTime()));
153-
}
100+
jdbcTemplate.update(
101+
"INSERT INTO titles_export (emp_no, title, from_date, to_date) " +
102+
"VALUES(?, ?, ?, ?)",
103+
employee.getEmployeeId(), title.getTitle(), dateFormat.format(title.getFromDate().getTime()),
104+
dateFormat.format(title.getToDate().getTime()));
154105
}
155106
}
156107
})

src/main/java/com/marklogic/client/example/cookbook/datamovement/BulkLoadFromJdbcWithJoins.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.slf4j.LoggerFactory;
3636

3737
import java.io.IOException;
38+
import java.sql.Connection;
3839
import java.sql.ResultSet;
3940
import java.sql.SQLException;
4041
import java.util.Calendar;
@@ -69,9 +70,11 @@ public Employee parseEmployee(ResultSet row) throws SQLException {
6970

7071
public void run() throws IOException, SQLException {
7172
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
72-
if(jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName().toLowerCase().contains("mysql"))
73+
Connection conn = jdbcTemplate.getDataSource().getConnection();
74+
if(conn.getMetaData().getDatabaseProductName().toLowerCase().contains("mysql"))
7375
// the following is required because GROUP_CONCAT calls in following SQL can generate long strings
7476
jdbcTemplate.execute("SET GLOBAL group_concat_max_len = 1000000");
77+
conn.close();
7578
WriteBatcher wb = moveMgr.newWriteBatcher()
7679
.withBatchSize(batchSize)
7780
.withThreadCount(threadCount)

src/test/java/com/marklogic/client/test/example/cookbook/DMSDKJdbcCookbookTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,31 @@ private void populateDataset(Database hsqlDB) {
9292
"FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE," +
9393
"PRIMARY KEY (emp_no, from_date));");
9494

95+
hsqlDB.execute("CREATE TABLE employees_export (" +
96+
"emp_no INTEGER NOT NULL," +
97+
"birth_date DATE NOT NULL," +
98+
"first_name VARCHAR(14) NOT NULL," +
99+
"last_name VARCHAR(16) NOT NULL," +
100+
"gender VARCHAR(2) NOT NULL," +
101+
"hire_date DATE NOT NULL," +
102+
"PRIMARY KEY (emp_no));");
103+
104+
hsqlDB.execute("CREATE TABLE titles_export (" +
105+
"emp_no INTEGER NOT NULL," +
106+
"title VARCHAR(50) NOT NULL," +
107+
"from_date DATE NOT NULL," +
108+
"to_date DATE," +
109+
"FOREIGN KEY (emp_no) REFERENCES employees_export (emp_no) ON DELETE CASCADE," +
110+
"PRIMARY KEY (emp_no,title, from_date));");
111+
112+
hsqlDB.execute("CREATE TABLE salaries_export (" +
113+
"emp_no INTEGER NOT NULL," +
114+
"salary INTEGER NOT NULL," +
115+
"from_date DATE NOT NULL," +
116+
"to_date DATE NOT NULL," +
117+
"FOREIGN KEY (emp_no) REFERENCES employees_export (emp_no) ON DELETE CASCADE," +
118+
"PRIMARY KEY (emp_no, from_date));");
119+
95120
hsqlDB.execute("INSERT INTO employees VALUES (1, '1990-10-04', 'Alice', 'Edward', 'F', '2012-04-05');");
96121
hsqlDB.execute("INSERT INTO employees VALUES (2, '1992-12-23', 'Bob', 'Miller', 'M', '2010-06-01');");
97122
hsqlDB.execute("INSERT INTO employees VALUES (3, '1985-11-30', 'Gerard', 'Steven', 'M', '2011-07-29');");

0 commit comments

Comments
 (0)