在Marklogic Java Client API中是否有exportListener的compress选项?

时间:2018-02-06 05:50:32

标签: marklogic java-api

我想使用Data Movement SDK从我的marklogic db导出所有文档。我成功导出为文件,但我想通过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而不是全部。

感谢任何帮助

由于

1 个答案:

答案 0 :(得分:2)

你真的很亲近。只需使用标准的Java zip文件而不是Files.write。这里的前两个答案看起来非常好:How to create a zip file in Java

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

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

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

这样他们只会跑一次。

此外,请将其移至dmm.stopJob(batcher);

之后
                      out.close();

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