Marklogic Java 客户端 API 中是否有 exportListener 的压缩选项?
Is there compress option for exportListener in Marklogic Java Client API?
我想使用 Data Movement 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) {...}
块中,这样线程在写入流时不会相互覆盖。请记住,您的侦听器代码将在 10 个线程中并行执行 运行,因此您在侦听器中的代码需要是线程安全的。
我想使用 Data Movement 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) {...}
块中,这样线程在写入流时不会相互覆盖。请记住,您的侦听器代码将在 10 个线程中并行执行 运行,因此您在侦听器中的代码需要是线程安全的。