marklogic - Marklogic Java 客户端 API 中的 exportListener 是否有压缩选项?

标签 marklogic java

我想使用数据移动 SDK 从我的 marklogic 数据库导出所有文档。我已成功导出为文件,但我想通过 DMSDK 将它们压缩为 zip 文件。我在有关 compress 选项的文档中进行了搜索,但没有找到任何内容。

更新代码

public class Extract {
    static // replace with your MarkLogic Server connection information

    DatabaseClient client =
              DatabaseClientFactory.newClient("x", x,
                                              "x", "x",
                                              Authentication.DIGEST);

    private static String EX_DIR = "F:/JavaExtract";

    // Loading files into the database asynchronously
    public static void exportByQuery() {  
         DataMovementManager dmm = client.newDataMovementManager();
        // Construct a directory query with which to drive the job.
        QueryManager qm = client.newQueryManager();
        StringQueryDefinition query = qm.newStringDefinition();
        query.setCollections("GOT");


        // Create and configure the batcher
        QueryBatcher batcher = dmm.newQueryBatcher(query);
        batcher.withBatchSize(1000)
        .withThreadCount(10)
        .onUrisReady(
            new ExportListener()
                .onDocumentReady(doc-> {
                    String uriParts[] = doc.getUri().split("/");
                    try {
                       FileOutputStream dest = new 
                             FileOutputStream("F:/Json/file.zip");
                           ZipOutputStream out = new ZipOutputStream(new 
                             BufferedOutputStream(dest));
                           ZipEntry e = new ZipEntry(uriParts[uriParts.length - 1]);
                           out.putNextEntry(e);

                           byte[] data = doc.getContent(
                                   new StringHandle()).toBuffer();
                           doc.getFormat();
                           out.write(data, 0, data.length);
                          out.closeEntry();

                          out.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }))
               .onQueryFailure( exception -> exception.printStackTrace() );

        dmm.startJob(batcher);

        // Wait for the job to complete, and then stop it.
        batcher.awaitCompletion();
        dmm.stopJob(batcher);
    }

    public static void main(String[] args) {
        exportByQuery();
    }
}

当我运行时,它只获取 GOT 集合中的最后一个文档并保存在 zip 中,而不是全部获取。

感谢任何帮助

谢谢

最佳答案

你们真的很接近。只需使用标准 Java zip 写入而不是 Files.write。这里最上面的两个答案看起来非常好:How to create a zip file in Java

另一个选项是 WriteToZipConsumer 。这将替换 onDocumentReady 调用中的所有代码。

[根据更新的问题进行更新] 您的 onDocumentReady 监听器针对每个文档运行,因此我猜测为每个文档创建一个 new FileOutputStream("F:/Json/file.zip"); 是没有意义的。这就是为什么您在完成后只能看到最后一个文档。尝试将这两行移到初始化批处理程序之前:

                       final FileOutputStream dest = new 
                         FileOutputStream("F:/Json/file.zip");
                       final ZipOutputStream out = new ZipOutputStream(new 
                         BufferedOutputStream(dest));

这样他们只会运行一次。

另外,将其移至 dmm.stopJob(batcher); 之后:

                      out.close();

此外,将监听器代码放在 synchronized(out) {...} block 中,以便线程在写入流时不会相互覆盖。请记住,您的监听器代码将在 10 个线程中并行运行,因此监听器中的代码需要是线程安全的。

关于marklogic - Marklogic Java 客户端 API 中的 exportListener 是否有压缩选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48636279/

相关文章:

constraints - 如何根据 Marklogic 中的用户输入创建自定义日期范围?

java - 从 json 对象生成 pdf 文档

linux - 如何在 curl 命令中提示输入用户名和密码?

java - 无法使用 apache poi 从 Excel 文件读取

java - Derby Db NoClassDefFoundError 无法在 Netbeans 之外工作

java - Logger.getLogger(class_name.class.getName()) 导致 NullPointerException

java - Hibernate Validator @URL 不接受没有 schema 的 URL

xml - 如何从数据库 marklogic 获取文档 uri 名称列表?

xpath - Marklogic:Xpath使用移除处理指令标签

node.js - Marklogic : "highlight" seem not work withe Node. js 和 QueryBuilder