Advertisement
Guest User

Untitled

a guest
Dec 21st, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. package com.marklogic;
  2.  
  3. import com.marklogic.client.DatabaseClient;
  4. import com.marklogic.client.DatabaseClientFactory;
  5. import com.marklogic.client.Transaction;
  6. import com.marklogic.client.document.DocumentWriteSet;
  7. import com.marklogic.client.document.XMLDocumentManager;
  8. import com.marklogic.client.io.StringHandle;
  9. import freemarker.template.Configuration;
  10. import freemarker.template.Template;
  11. import freemarker.template.TemplateExceptionHandler;
  12.  
  13. import java.io.StringWriter;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16.  
  17. /**
  18. * Created by mwarnes on 20/12/2016.
  19. */
  20. public class XCCInsert {
  21.  
  22. public static void main(String[] args) {
  23.  
  24. // Configure FreeMArker templates to build sample data files
  25. Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
  26. cfg.setClassForTemplateLoading(XIncludeBuilder.class, "/");
  27. cfg.setDefaultEncoding("UTF-8");
  28. cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  29. cfg.setLogTemplateExceptions(false);
  30.  
  31. // Create MarkLogic connection
  32. System.out.println("Open MarkLogic connection ");
  33. String HOST = "192.168.66.160";
  34. Integer PORT = 8000;
  35. String USER = "admin";
  36. String PASSWORD = "admin";
  37. DatabaseClient client = DatabaseClientFactory.newClient(HOST, PORT, USER, PASSWORD,
  38. DatabaseClientFactory.Authentication.DIGEST);
  39. XMLDocumentManager xdm = client.newXMLDocumentManager();
  40.  
  41.  
  42. // Main outer loop
  43. for (int loop = 1; loop <= 100; loop++) {
  44.  
  45. //Create transaction
  46. Transaction transaction = client.openTransaction();
  47.  
  48. // Inner loop creates batch of 3000 customer and info data files
  49. try {
  50. DocumentWriteSet batch = xdm.newWriteSet();
  51.  
  52. for (int counter = 1; counter <= 500; counter++) {
  53. Map customer = new HashMap();
  54. String count = String.format("%08d", counter);
  55. customer.put("user", "Test" + count);
  56. customer.put("counter", count);
  57.  
  58. Template customerTmpl = cfg.getTemplate("Customer.ftlh");
  59. StringWriter customerWriter = new StringWriter();
  60. customerTmpl.process(customer, customerWriter);
  61. customerWriter.close();
  62.  
  63. Map info = new HashMap();
  64. info.put("user", "Test" + count);
  65. info.put("counter", count);
  66.  
  67. Template infoTmpl = cfg.getTemplate("Info.ftlh");
  68. StringWriter infoWriter = new StringWriter();
  69. infoTmpl.process(info, infoWriter);
  70. infoWriter.close();
  71.  
  72. String infoUri = "/tmp/info/INFO_" + count + ".xml";
  73. StringHandle infoDoc = new StringHandle(infoWriter.getBuffer().toString());
  74. batch.add(infoUri, infoDoc);
  75.  
  76. String customerUri = "/customer/CUSTOMER_" + count + ".xml";
  77. StringHandle customerDoc = new StringHandle(customerWriter.getBuffer().toString());
  78. batch.add(customerUri, customerDoc);
  79. }
  80.  
  81. // Write batch of 3000 documents
  82. xdm.write(batch);
  83. // Commit transaction
  84. transaction.commit();
  85.  
  86. } catch (Exception e) {
  87. // In event of failure roll back transaction
  88. e.printStackTrace();
  89. transaction.rollback();
  90. }
  91.  
  92. //Loop around and repeat
  93.  
  94. }
  95.  
  96. // Release the MarkLogic client
  97. System.out.println("Release MarkLogic client");
  98. client.release();
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement