簡體   English   中英

將元數據存儲到 Jackrabbit 存儲庫中

[英]Store metadata into Jackrabbit repository

任何人都可以向我解釋,如何在以下情況下進行?

  1. 接收文件(MS 文檔、ODS、PDF)

  2. 通過 Apache Tika 提取雙核心元數據 + 通過 jackrabbit-content-extractors 提取內容

  3. 使用 Jackrabbit 將文檔(內容)與其元數據一起存儲到存儲庫中

  4. 檢索文檔 + 元數據

我對第 3 點和第 4 點感興趣...

詳細信息:應用程序以交互方式處理文檔(一些分析 - 語言檢測、字數等 + 收集盡可能多的細節 - Dublin 核心 + 解析內容/事件處理),以便將處理結果返回給用戶,然后提取的內容和元數據(提取的和自定義的用戶元數據)存儲到 JCR 存儲庫中

感謝任何幫助,謝謝

JCR 2.0 和 JCR 1.0 的上傳文件基本相同。 但是,JCR 2.0 添加了一些額外的有用的內置屬性定義。

“nt:file”節點類型旨在表示一個文件,並且在 JCR 2.0 中有兩個內置屬性定義(這兩個都是在創建節點時由存儲庫自動創建的):

  • jcr:創建(日期)
  • jcr:createdBy (STRING)

並定義了一個名為“jcr:content”的孩子。 這個“jcr:content”節點可以是任何節點類型,但一般來說,與內容本身有關的所有信息都存儲在這個子節點上。 事實上的標准是使用“nt:resource”節點類型,它定義了以下屬性:

  • jcr:data (BINARY) 強制
  • jcr:lastModified (DATE) 自動創建
  • jcr:lastModifiedBy (STRING) 自動創建
  • jcr:mimeType (STRING) 受保護?
  • jcr:編碼(字符串)保護?

請注意,“jcr:mimeType”和“jcr:encoding”是在 JCR 2.0 中添加的。

特別是,“jcr:mimeType”屬性的目的是完全按照您的要求執行 - 捕獲內容的“類型”。 但是,“jcr:mimeType”和“jcr:encoding”屬性定義可以(由 JCR 實現)定義為受保護的(意味着 JCR 實現會自動設置它們) - 如果是這種情況,您將無法手動設置這些屬性。 我相信JackrabbitModeShape不會將這些視為受保護的。

下面是一些代碼,展示了如何使用這些內置節點類型將文件上傳到 JCR 2.0 存儲庫:

// Get an input stream for the file ...
File file = ...
InputStream stream = new BufferedInputStream(new FileInputStream(file));

Node folder = session.getNode("/absolute/path/to/folder/node");
Node file = folder.addNode("Article.pdf","nt:file");
Node content = file.addNode("jcr:content","nt:resource");
Binary binary = session.getValueFactory().createBinary(stream);
content.setProperty("jcr:data",binary);

如果 JCR 實現未將“jcr:mimeType”屬性視為受保護(即 Jackrabbit 和 ModeShape),則必須手動設置此屬性:

content.setProperty("jcr:mimeType","application/pdf");

元數據可以很容易地存儲在“nt:file”和“jcr:content”節點上,但開箱即用的“nt:file”和“nt:resource”節點類型不允許額外的屬性. 因此,在添加其他屬性之前,首先需要添加一個 mixin(或多個 mixin),其中包含要存儲的屬性類型的屬性定義。 你甚至可以定義一個允許任何屬性的 mixin。 這是一個定義這樣一個 mixin 的 CND 文件:

<custom = 'http://example.com/mydomain'>
[custom:extensible] mixin
- * (undefined) multiple 
- * (undefined) 

注冊此節點類型定義后,您可以在您的節點上使用它:

content.addMixin("custom:extensible");
content.setProperty("anyProp","some value");
content.setProperty("custom:otherProp","some other value");

您還可以定義和使用允許任何Dublin Core 元素的 mixin:

<dc = 'http://purl.org/dc/elements/1.1/'>
[dc:metadata] mixin
- dc:contributor (STRING)
- dc:coverage (STRING)
- dc:creator (STRING)
- dc:date (DATE)
- dc:description (STRING)
- dc:format (STRING)
- dc:identifier (STRING)
- dc:language (STRING)
- dc:publisher (STRING)
- dc:relation (STRING)
- dc:right (STRING)
- dc:source (STRING)
- dc:subject (STRING)
- dc:title (STRING)
- dc:type (STRING)

所有這些屬性都是可選的,並且這個 mixin 不允許任何名稱或類型的屬性。 我也沒有真正解決這個“dc:metadata”混合的問題,因為其中一些已經用內置屬性表示(例如,“jcr:createBy”、“jcr:lastModifiedBy”、“jcr:created” , "jcr:lastModified", "jcr:mimeType") 並且其中一些可能與內容更相關,而另一些可能與文件更相關。

您當然可以定義更適合您的元數據需求的其他 mixin,在需要的地方使用繼承。 但是在使用 mixin 時要小心——因為 JCR 允許一個節點有多個 mixin,通常最好將你的 mixin 設計為緊密作用域和面向方面的(例如,“ex:taggable”、“ex:describable”等)然后根據需要簡單地將適當的混合應用到節點。

(甚至可以定義一個mixin,允許在“nt:file”節點下有更多子節點,並在那里存儲一些元數據。)

Mixins 非常棒,為您的 JCR 內容提供了極大的靈活性和功能。

哦,當你創建了你想要的所有節點時,一定要保存會話:

session.save();

我是 Jackrabbit 的新手,正在開發 2.4.2。 至於您的解決方案,您可以使用核心 java 邏輯檢查類型並放置定義操作中任何變化的案例。

您無需擔心保存不同 .txt 或 .pdf 內容的問題,因為它們的內容會被轉換為二進制文件並保存。 這是一個小示例,我在其中上傳和下載了 jackrabbit 存儲庫中的 pdf 文件。

    // Import the pdf file unless already imported 
            // This program is for sample purpose only so everything is hard coded.
        if (!root.hasNode("Alfresco_E0_Training.pdf"))
        { 
            System.out.print("Importing PDF... "); 

            // Create an unstructured node under which to import the XML 
            //Node node = root.addNode("importxml", "nt:unstructured"); 
            Node file = root.addNode("Alfresco_E0_Training.pdf","nt:file");

            // Import the file "Alfresco_E0_Training.pdf" under the created node 
            FileInputStream stream = new FileInputStream("<path of file>\\Alfresco_E0_Training.pdf");
            Node content = file.addNode("jcr:content","nt:resource");
            Binary binary = session.getValueFactory().createBinary(stream);
            content.setProperty("jcr:data",binary);
            stream.close();
            session.save(); 
            //System.out.println("done."); 
            System.out.println("::::::::::::::::::::Checking content of the node:::::::::::::::::::::::::");
            System.out.println("File Node Name : "+file.getName());
            System.out.println("File Node Identifier : "+file.getIdentifier());
            System.out.println("File Node child : "+file.JCR_CHILD_NODE_DEFINITION);
            System.out.println("Content Node Name : "+content.getName());
            System.out.println("Content Node Identifier : "+content.getIdentifier());
            System.out.println("Content Node Content : "+content.getProperty("jcr:data"));
            System.out.println(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");

        }else
        {
            session.save();
            Node file = root.getNode("Alfresco_E0_Training.pdf");
            Node content = file.getNode("jcr:content");
            String path = content.getPath();
            Binary bin = session.getNode(path).getProperty("jcr:data").getBinary();
            InputStream stream = bin.getStream();
             File f=new File("C:<path of the output file>\\Alfresco_E0_Training.pdf");

              OutputStream out=new FileOutputStream(f);
              byte buf[]=new byte[1024];
              int len;
              while((len=stream.read(buf))>0)
              out.write(buf,0,len);
              out.close();
              stream.close();
              System.out.println("\nFile is created...................................");


            System.out.println("done."); 
            System.out.println("::::::::::::::::::::Checking content of the node:::::::::::::::::::::::::");
            System.out.println("File Node Name : "+file.getName());
            System.out.println("File Node Identifier : "+file.getIdentifier());
            //System.out.println("File Node child : "+file.JCR_CHILD_NODE_DEFINITION);
            System.out.println("Content Node Name : "+content.getName());
            System.out.println("Content Node Identifier : "+content.getIdentifier());
            System.out.println("Content Node Content : "+content.getProperty("jcr:data"));
            System.out.println(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
        } 

        //output the repository content
        } 
    catch (IOException e){
        System.out.println("Exception: "+e);
    }
    finally { 
        session.logout(); 
        } 
        } 
}

希望這可以幫助

我對 JCR 有點生疏,我從未使用過 2.0,但這應該能讓你開始。

請參閱此鏈接 你會想打開第二條評論。

您只需將文件存儲在節點中並向節點添加其他元數據。 以下是存儲文件的方法:

Node folder = session.getRootNode().getNode("path/to/file/uploads"); 
Node file = folder.addNode(fileName, "nt:file"); 
Node fileContent = file.addNode("jcr:content"); 
fileContent.setProperty("jcr:data", fileStream);
// Add other metadata
session.save();

您如何存儲元數據取決於您。 一個簡單的方法是只存儲鍵值對:

fileContent.setProperty(key, value, PropertyType.STRING);

要讀取數據,您只需調用getProperty()

fileStream = fileContent.getProperty("jcr:data");
value = fileContent.getProperty(key);

暫無
暫無

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

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