Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.marklogic;
- import com.marklogic.client.DatabaseClient;
- import com.marklogic.client.DatabaseClientFactory;
- import com.marklogic.client.Transaction;
- import com.marklogic.client.document.DocumentWriteSet;
- import com.marklogic.client.document.XMLDocumentManager;
- import com.marklogic.client.io.StringHandle;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- import freemarker.template.TemplateExceptionHandler;
- import java.io.StringWriter;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by mwarnes on 20/12/2016.
- */
- public class XCCInsert {
- public static void main(String[] args) {
- // Configure FreeMArker templates to build sample data files
- Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
- cfg.setClassForTemplateLoading(XIncludeBuilder.class, "/");
- cfg.setDefaultEncoding("UTF-8");
- cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
- cfg.setLogTemplateExceptions(false);
- // Create MarkLogic connection
- System.out.println("Open MarkLogic connection ");
- String HOST = "192.168.66.160";
- Integer PORT = 8000;
- String USER = "admin";
- String PASSWORD = "admin";
- DatabaseClient client = DatabaseClientFactory.newClient(HOST, PORT, USER, PASSWORD,
- DatabaseClientFactory.Authentication.DIGEST);
- XMLDocumentManager xdm = client.newXMLDocumentManager();
- // Main outer loop
- for (int loop = 1; loop <= 100; loop++) {
- //Create transaction
- Transaction transaction = client.openTransaction();
- // Inner loop creates batch of 3000 customer and info data files
- try {
- DocumentWriteSet batch = xdm.newWriteSet();
- for (int counter = 1; counter <= 500; counter++) {
- Map customer = new HashMap();
- String count = String.format("%08d", counter);
- customer.put("user", "Test" + count);
- customer.put("counter", count);
- Template customerTmpl = cfg.getTemplate("Customer.ftlh");
- StringWriter customerWriter = new StringWriter();
- customerTmpl.process(customer, customerWriter);
- customerWriter.close();
- Map info = new HashMap();
- info.put("user", "Test" + count);
- info.put("counter", count);
- Template infoTmpl = cfg.getTemplate("Info.ftlh");
- StringWriter infoWriter = new StringWriter();
- infoTmpl.process(info, infoWriter);
- infoWriter.close();
- String infoUri = "/tmp/info/INFO_" + count + ".xml";
- StringHandle infoDoc = new StringHandle(infoWriter.getBuffer().toString());
- batch.add(infoUri, infoDoc);
- String customerUri = "/customer/CUSTOMER_" + count + ".xml";
- StringHandle customerDoc = new StringHandle(customerWriter.getBuffer().toString());
- batch.add(customerUri, customerDoc);
- }
- // Write batch of 3000 documents
- xdm.write(batch);
- // Commit transaction
- transaction.commit();
- } catch (Exception e) {
- // In event of failure roll back transaction
- e.printStackTrace();
- transaction.rollback();
- }
- //Loop around and repeat
- }
- // Release the MarkLogic client
- System.out.println("Release MarkLogic client");
- client.release();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement