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

Marklogic Java客户端API中是否有exportListener的压缩选项?,marklogic,java,Marklogic,Java,我想使用数据移动SDK从marklogic db导出所有文档。我成功导出为文件,但我想通过DMSDK将它们压缩为zip文件。我在文档中搜索了有关压缩选项的内容,但没有找到任何内容 更新代码 public class Extract { static // replace with your MarkLogic Server connection information DatabaseClient client = DatabaseClientFac

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

更新代码

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();
    }
}
当我运行时,它只获取
get
集合中的最后一个文档,并保存在zip中,而不是获取所有文档

谢谢你的帮助


谢谢

你真的很接近了。只需使用标准的Javazip编写,而不是Files.write。这里的前两个答案看起来非常好:

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

[根据更新的问题进行更新] 您的onDocumentReady侦听器是为每个文档运行的,所以我猜创建一个
新的FileOutputStream(“F:/Json/file.zip”)是没有意义的用于每个文档。这就是为什么您在完成后才看到最后一个文档。在初始化批处理程序之前,请尝试将这两行移到:

                       final FileOutputStream dest = new 
                         FileOutputStream("F:/Json/file.zip");
                       final ZipOutputStream out = new ZipOutputStream(new 
                         BufferedOutputStream(dest));
这样他们只会跑一次

另外,将其移动到
dmm.stopJob(批处理程序)之后


另外,将侦听器代码包围在
synchronized(out){…}
块中,这样线程在写入流时不会相互覆盖。请记住,您的侦听器代码将在10个线程中并行运行,因此侦听器中的代码需要是线程安全的。

我可以实现zip,但它只占用集合中的最后一个文件。我把我的代码保存在更新的部分。任何帮助都非常有用。
                      out.close();