Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.

Commit 3e84af6

Browse files
author
Scott Stafford
committed
Verify tests and update documentation
1 parent 90f8cfb commit 3e84af6

File tree

5 files changed

+91
-34
lines changed

5 files changed

+91
-34
lines changed

examples/rowToDoc/README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
# SQL Example #1 - Import Invoice Documents
22

3-
This is an batch processing job example that migrates the tabular rows of a SQL Query, transforms them into either JSON or XML and inserts the data into MarkLogic.
3+
This is an batch processing job example that migrates the tabular rows of a SQL Query, transforms them into either JSON or XML and inserts the data into MarkLogic.
44

55
The RDBMS data set is using the [Invoice data](invoices-sql-diagram.jpg) from the [HSQL relational database](http://www.hsqldb.org/).
66

7+
This job can take two tables and create child elements from the many relationship. For this example, we will examine the
8+
customer and invoice tables. These two tables are joined via a one (customer) to
9+
many (invoice) relationship. We have decided that the root element is going to be based on the customer
10+
and many invoice children will be created. By renaming the column names using a
11+
**[parent element]/[child element]** naming convention in your SQL query, this batch processing job
12+
will create the document accordingly.
13+
14+
SELECT customer.*, invoice.id as \"invoice/id\", invoice.total as \"invoice/total\"
15+
FROM invoice LEFT JOIN customer on invoice.customerId = customer.id
16+
ORDER BY customer.id
17+
18+
In this example, this would generate the following sample document.
19+
20+
<invoice>
21+
<ID>13</ID>
22+
<FIRSTNAME>Laura</FIRSTNAME>
23+
<LASTNAME>Ringer</LASTNAME>
24+
<STREET>38 College Av.</STREET>
25+
<CITY>New York</CITY>
26+
<invoice>
27+
<id>43</id>
28+
<total>3215</total>
29+
</invoice>
30+
<invoice>
31+
<id>30</id>
32+
<total>1376</total>
33+
</invoice>
34+
</invoice>
35+
36+
Please note that any child elements beyond the second level are not yet supported.
37+
738
## Create RowToDoc Distribution Program
839
1) gradle installDist
940
2) Verify the program that was installed under ./build/install/rowToDoc
@@ -21,11 +52,8 @@ This command uses the [gradle application plugin](https://docs.gradle.org/curren
2152
1) Open rowToDoc.bat and check the host, port, username, and password parameters to make sure that they point to a valid MarkLogic application server
2253
2) Execute: rowToDoc.bat
2354

24-
For Unix based systems, execute build\install\rowToJson\bin\sql
25-
26-
The program can also be executed via a Gradle JavaExec task
55+
For Unix based systems, execute build\install\rowToJson\bin\rowToDoc
2756

28-
gradle importInvoices
2957

3058

3159

examples/rowToDoc/build.gradle

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,3 @@ task runManager(type: JavaExec) {
2929
"--user", "SA"
3030
]
3131
}
32-
33-
task importInvoices(type: JavaExec) {
34-
classpath = sourceSets.main.runtimeClasspath
35-
main = "com.marklogic.spring.batch.Main"
36-
args = [
37-
"--host", "localhost",
38-
"--port", "8200",
39-
"--username", "admin",
40-
"--password", "admin",
41-
"--config", "com.marklogic.spring.batch.config.MigrateColumnMapsConfig",
42-
"--jdbc_driver", "org.hsqldb.jdbc.JDBCDriver",
43-
"--jdbc_url", "jdbc:hsqldb:file:data\\invoices",
44-
"--sql", "SELECT * FROM invoice LEFT JOIN customer on invoice.customerId=customer.id LEFT JOIN item on invoice.id=item.invoiceId LEFT JOIN product on product.id=item.productId ORDER BY invoice.id asc",
45-
"--jdbc_username", "sa",
46-
"--format", "xml",
47-
"--root_local_name", "invoice",
48-
"--collections", "invoice"
49-
]
50-
}

examples/rowToDoc/src/main/java/com/marklogic/spring/batch/config/RowToDocConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
1717
import org.springframework.batch.core.configuration.annotation.JobScope;
1818
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
19-
import org.springframework.batch.item.ItemProcessor;
2019
import org.springframework.batch.item.database.JdbcCursorItemReader;
2120
import org.springframework.beans.factory.annotation.Autowired;
2221
import org.springframework.beans.factory.annotation.Qualifier;

examples/rowToDoc/src/test/java/com/marklogic/spring/batch/config/sql/RowToDocConfigTest.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.marklogic.spring.batch.config.sql;
22

3+
import com.marklogic.client.admin.TransformExtensionsManager;
4+
import com.marklogic.client.document.XMLDocumentManager;
5+
import com.marklogic.client.io.FileHandle;
6+
import com.marklogic.client.io.Format;
7+
import com.marklogic.client.io.StringHandle;
8+
import com.marklogic.junit.Fragment;
39
import com.marklogic.spring.batch.config.PathAwareColumnMapRowMapper;
410
import com.marklogic.spring.batch.config.RowToDocConfig;
511
import com.marklogic.spring.batch.test.AbstractJobTest;
@@ -11,11 +17,13 @@
1117
import org.springframework.batch.item.ExecutionContext;
1218
import org.springframework.batch.item.database.JdbcCursorItemReader;
1319
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.core.io.Resource;
1421
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
1522
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
1623
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
1724

1825
import javax.sql.DataSource;
26+
import java.io.IOException;
1927
import java.util.Map;
2028

2129

@@ -36,20 +44,20 @@ public class RowToDocConfigTest extends AbstractJobTest {
3644

3745
protected static EmbeddedDatabase embeddedDatabase;
3846
protected final Logger logger = LoggerFactory.getLogger(getClass());
47+
private TransformExtensionsManager transMgr;
48+
private String sql = "SELECT customer.*, invoice.id as \"invoice/id\", invoice.total as \"invoice/total\" FROM invoice LEFT JOIN customer on invoice.customerId = customer.id ORDER BY customer.id";
3949

4050
@Before
41-
public void createDb() {
51+
public void createDb() throws IOException {
4252
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL);
4353
builder.addScripts("db/sampledata_ddl.sql", "db/sampledata_insert.sql");
4454
embeddedDatabase = builder.build();
45-
}
4655

47-
@After
48-
public void teardown() {
49-
if (embeddedDatabase != null) {
50-
embeddedDatabase.shutdown();
51-
}
52-
embeddedDatabase = null;
56+
Resource transform = getApplicationContext().getResource("classpath:/transforms/simple.xqy");
57+
transMgr = getClient().newServerConfigManager().newTransformExtensionsManager();
58+
FileHandle fileHandle = new FileHandle(transform.getFile());
59+
fileHandle.setFormat(Format.XML);
60+
transMgr.writeXQueryTransform("simple", fileHandle);
5361
}
5462

5563
@Test
@@ -69,7 +77,7 @@ public void jdbcCursorItemReaderTest() throws Exception {
6977
}
7078

7179
@Test
72-
public void runRowToDocJobTest() {
80+
public void runRowToDocJobWithTransformTest() {
7381
runJob(RowToDocTestConfig.class,
7482
"--sql", "SELECT customer.*, invoice.id as \"invoice/id\", invoice.total as \"invoice/total\" FROM invoice LEFT JOIN customer on invoice.customerId = customer.id ORDER BY customer.id",
7583
"--jdbc_username", "sa",
@@ -78,7 +86,29 @@ public void runRowToDocJobTest() {
7886
"--collections", "invoice",
7987
"--transform_name", "simple",
8088
"--transform_parameters", "monster,grover,trash,oscar");
89+
Fragment f = loadInvoice();
90+
f.assertElementValue("/invoice/invoice/ID", "13");
91+
f.assertElementValue("/invoice/invoice/LASTNAME", "Ringer");
92+
f.assertElementExists("/invoice/invoice/invoice[1]/total[. = '3215']");
93+
f.assertElementExists("/invoice/invoice/invoice[2]/total[. = '1376']");
94+
f.assertElementExists("/invoice/transform");
95+
f.assertElementExists("/invoice/monster[. = 'grover']");
96+
f.assertElementExists("/invoice/trash[. = 'oscar']");
97+
}
8198

99+
private Fragment loadInvoice() {
100+
XMLDocumentManager mgr = getClient().newXMLDocumentManager();
101+
String xml = mgr.read("/invoice/13.xml", new StringHandle()).get();
102+
return parse(xml);
103+
}
104+
105+
@After
106+
public void teardown() {
107+
if (embeddedDatabase != null) {
108+
embeddedDatabase.shutdown();
109+
}
110+
embeddedDatabase = null;
111+
transMgr.deleteTransform("simple");
82112
}
83113

84114
@Configuration
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
xquery version "1.0-ml";
2+
3+
module namespace x = "http://marklogic.com/rest-api/transform/simple";
4+
5+
declare function x:transform(
6+
$context as map:map,
7+
$params as map:map,
8+
$content as document-node())
9+
as document-node() {
10+
document {
11+
element { fn:local-name($content/element()) } {
12+
$content/element(),
13+
element transform {},
14+
for $param in map:keys($params)
15+
return element { $param } { map:get($params, $param) }
16+
}
17+
}
18+
19+
};

0 commit comments

Comments
 (0)