簡體   English   中英

使用 Java 客戶端 API 在 MarkLogic 中獲取所有文檔 URI

[英]Fetching all the document URI's in MarkLogic Using Java Client API

我試圖在不知道確切 url 的情況下從數據庫中獲取所有文檔。 我收到一個查詢

DocumentPage documents =docMgr.read();
while (documents.hasNext()) {
    DocumentRecord document = documents.next();
    System.out.println(document.getUri());
}

但我沒有特定的網址,我想要所有的文件

第一步是在數據庫上啟用您的 uris 詞典。

您可以評估一些 XQuery 並運行 cts:uris()(或服務器端 JS 並運行 cts.uris()):

    ServerEvaluationCall call = client.newServerEval()
        .xquery("cts:uris()");
    for ( EvalResult result : call.eval() ) {
        String uri = result.getString();
        System.out.println(uri);
    }

兩個缺點是:(1) 您需要一個具有特權的用戶;(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();
    }

僅當您需要與此過程同時發生的添加/刪除操作隔離的有保證的“快照”列表時,才需要該事務。 由於它增加了一些開銷,如果您不需要這樣的精確性,可以隨意刪除它。

找出頁面長度,然后在 queryMgr 中指定要訪問的起點。 繼續增加起點並循環遍歷所有 URL。 我能夠獲取所有 URI。 這可能不是很好的方法,但有效。

List<String> uriList = new ArrayList<>();       
        QueryManager queryMgr = client.newQueryManager();
        StructuredQueryBuilder qb = new StructuredQueryBuilder();
        StructuredQueryDefinition querydef = qb.and(qb.collection("xxxx"), qb.collection("whatever"), qb.collection("whatever"));//outputs 241152
        SearchHandle results = queryMgr.search(querydef, new SearchHandle(), 10);
        long pageLength = results.getPageLength();
        long totalResults = results.getTotalResults();
        System.out.println("Total Reuslts: " + totalResults);
        long timesToLoop = totalResults / pageLength;
        for (int i = 0; i < timesToLoop; i = (int) (i + pageLength)) {
            System.out.println("Printing Results from: " + (i) + " to: " + (i + pageLength));
            results = queryMgr.search(querydef, new SearchHandle(), i);
            MatchDocumentSummary[] summaries = results.getMatchResults();//10 results because page length is 10
            for (MatchDocumentSummary summary : summaries) {
//                System.out.println("Extracted friom URI-> " + summary.getUri());
                uriList.add(summary.getUri());
            }
            if (i >= 1000) {//number of URI to store/retreive. plus 10
                break;
            }
        }
         uriList= uriList.stream().distinct().collect(Collectors.toList());
        return uriList;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM