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

Getting Started

Scott Stafford edited this page Jan 26, 2018 · 17 revisions

The goal of this tutorial is to create a simple program which generates random JSON documents and ingests those documents into MarkLogic. This tutorial will demonstrate how to build this program using Spring Batch and the MarkLogic Spring Batch extensions.

Prerequisites

Make sure that both Java and Gradle commands are in your path variable. Gradle is a requirement for the purposes of this getting started exercise. It is not a requirement when building a MarkLogic Spring Batch application.

Set up project

Create a project folder and name it 'sb-sample'. Open a command prompt and change to this new directory. This will serve as your PROJECT_ROOT.

mkdir sb-sample
cd sb-sample

From PROJECT_ROOT, initialize the project using the gradle init plugin.

gradle init --type java-application    

This gradle command produces a command-line application implemented using Java.
Delete the following files/directories

  • ./src/main/java/App.java
  • ./test/

Add Project Dependencies

Next, add the MarkLogic Spring Batch dependencies in your gradle file. Open build.gradle and replace the dependencies section with the content below.

dependencies {
    compile 'com.marklogic:marklogic-spring-batch-core:1.+'
}

The jar files from the MarkLogic Spring Batch project are hosted in a Maven site called JCenter.

Create Job Configuration Class

The main code for a Spring Batch program is the JobConfig class. This wires up the necessary piece parts of your job. For this example, we are going to copy an existing JobConfig class and use that for a starting point. The Job will generate 100 json files and write that to MarkLogic.

Please copy the YourJobConfig.java file and save under the ./src/main/java directory.

Create job properties

The JobConfig declares that its PropertySource is a file on the classpath called job.properties. Create a file under ./src/main/resources called job.properties and copy the following contents into it.

marklogic.host=localhost
marklogic.port=8200
marklogic.username=admin
marklogic.password=admin

Modify the property values for your local environment.

Create logback.xml properties

Logback is used for logging and requires a configuration file. Create the directory ./src/main/resources and create a file called logback.xml into this directory with the following content.

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Execute

To execute your Spring Batch program, a gradle task will be added that executes the main program. Open PROJECT_ROOT/build.gradle and add the following task.

task runYourJob(type: JavaExec) {
     main = 'com.marklogic.spring.batch.core.launch.support.CommandLineJobRunner'
     classpath = sourceSets.test.runtimeClasspath
     args = ["--job_path", "YourJobConfig",
        "--job_id", "yourJob",
        "--output_collections", "yourJob",
        "--chunk_size", "50",
        "--thread_count", "1",
        "--next"]
 }

From the command line under PROJECT_ROOT, execute the following command.

 gradle runYourJob

Check your database referenced from your JobProperties and you should now 100 JSON documents.

Congratulations on creating a simple Spring Batch program.

Clone this wiki locally