的第一步是使數據庫的URI的詞彙。
你可以EVAL一些XQuery和運行CTS:URI的()(或服務器端JS和運行cts.uris()):
ServerEvaluationCall call = client.newServerEval()
.xquery("cts:uris()");
for (EvalResult result : call.eval()) {
String uri = result.getString();
System.out.println(uri);
}
兩個缺點是:(1)你需要一個用戶privileges和(2)沒有分頁。
如果您有少量文件,則不需要分頁。但是對於大量的文檔分頁建議。下面是使用搜索API和分頁一些代碼:如果您需要添加/刪除在這個過程中發生的同時分離出一種保證「快照」列表
// do the next eight lines just once
String options =
"<options xmlns='http://marklogic.com/appservices/search'>" +
" <values name='uris'>" +
" <uri/>" +
" </values>" +
"</options>";
QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager();
optionsMgr.writeOptions("uriOptions", new StringHandle(options));
// run the following each time you need to list all uris
QueryManager queryMgr = client.newQueryManager();
long pageLength = 10000;
queryMgr.setPageLength(pageLength);
ValuesDefinition query = queryMgr.newValuesDefinition("uris", "uriOptions");
// the following "and" query just matches all documents
query.setQueryDefinition(new StructuredQueryBuilder().and());
int start = 1;
boolean hasMore = true;
Transaction transaction = client.openTransaction();
try {
while (hasMore) {
CountedDistinctValue[] uriValues =
queryMgr.values(query, new ValuesHandle(), start, transaction).getValues();
for (CountedDistinctValue uriValue : uriValues) {
String uri = uriValue.get("string", String.class);
//System.out.println(uri);
}
start += uriValues.length;
// this is the last page if uriValues is smaller than pageLength
hasMore = uriValues.length == pageLength;
}
} finally {
transaction.commit();
}
交易纔是必需的。由於它增加了一些開銷,如果你不需要這樣的精確度,可以隨意刪除它。
你究竟在努力完成什麼?如果你想導出內容,MLCP會更容易。如果您想要進行一些數字運算,那麼在MarkLogic內部進行操作可能會更容易。 –