組裝和編譯自訂連接器
將若干元件檔案包裝在一起以建立自訂連接器。
IBM Cloud Pak for Data IBM Software Hub
此資訊僅適用於已安裝的部署。
自訂連接器元件
自訂連接器套件是包含下列元件的壓縮檔案:
| 路徑 | 說明 |
|---|---|
config/template.xml |
配置範本 |
config/messages.properties |
使用者介面訊息的內容檔 |
lib/*.jar |
自訂連接器需要的 JAR 檔,不包括您撰寫的連接器程式碼 |
配置範本
配置範本是劃分為區段的 XML 檔案。 每個區段會包含相關設定。 XML Snippet 從範例 template.xml 檔案中取得,該檔案位置列在瞭解 custom-crawler-docs.zip 檔案中。
宣告設定
已宣告的設定由 <declare /> 元素表示。 這個元素具有下列屬性:
| 屬性名稱 | 說明 |
|---|---|
type |
資料類型;為下列其中一個值:string、long、boolean、list(字串清單)或 enum |
name |
設定的名稱 |
initial-value |
設定的起始值 |
enum-value |
enum 值的清單,以垂直條分隔 ( | ) |
required |
指出設定為必要 |
hidden |
指出是否在使用者介面隱藏中設定。 指定 true 值以隱藏設定。 |
在目前的版本中,required 和 hidden 屬性不應用於 Discovery 產品使用者介面。
宣告設定範例
若要宣告 enum 類型,請使用類似以下片段的程式碼:
<declare type="enum" name="type" enum-values="PROXY|BASIC|NTLM" initial-value="BASIC"/>
若要宣告具有初始值的隱藏 string,請使用類似以下片段的程式碼:
<declare type="string" name="custom_config_class" hidden="true" initial-value="com.example.ExampleCrawlerConfig" />
若要宣告必填 long,請使用類似以下片段的程式碼:
<declare type="long" name="port" required="required" initial-value="22"/>
條件式設定
條件設定由 <condition /> 元素表示。 唯有滿足條件時,才會顯示條件式設定。 這個元素具有下列屬性:
| 屬性名稱 | 說明 |
|---|---|
name |
設定的名稱 |
enable |
如果 name 屬性的值等於 enable 屬性的值,將啟用設定 |
in |
如果在指定的值清單中包含 name 屬性的值,將啟用設定 |
在現行版本中,Discovery 產品使用者介面中未套用條件式設定。
條件式設定範例
若要使用 boolean 條件啟用區段,請使用類似以下片段的程式碼:
<declare type="boolean" name="use_key" initial-value="true" />
<condition name="use_key" enabled="true">
<declare type="string" name="key" hidden="false" />
</condition>
若要使用 enum 條件啟用區段,請使用類似以下片段的程式碼:
<declare type="enum" name="type" enum-values="PROXY|BASIC|NTLM" initial-value="BASIC"/>
<condition name="type" in="BASIC|NTLM|PROXY">
</condition>
<condition name="type" in="PROXY">
範本區段
每個區段包含一個 <declare /> 元件,用於其每個設定。
| XPath 表示式 | 說明 |
|---|---|
/function/@name |
搜索器的名稱(類型)。 這不是使用者介面的顯示名稱。 不能包含空格。 |
/function/prototype/proto-section |
配置的區段。 |
區段:general_settings
XPath 表示式為 /function/prototype/proto-section[@section="general_settings"]。 它包括適用於所有爬蟲的共通設定,包括以下設定:
<declare type="string" name="crawler_name" />
<declare type="string" name="description" />
<declare type="long" name="fetch_interval" initial-value="0" />
<declare type="long" name="number_of_max_threads" initial-value="10" />
<declare type="long" name="number_of_max_documents" initial-value="2000000000" />
<declare type="long" name="max_page_length" initial-value="32768" />
自訂搜索器使用 general_settings 區段中的下列設定進行起始設定。 如需介面的相關資訊,請參閱開發自訂連接器程式碼。
| 名稱 | 值 |
|---|---|
custom_config_class |
實作 com.ibm.es.ama.custom.crawler.CustomCrawlerConfiguration 介面的類別名稱 |
custom_crawler_class |
實作 com.ibm.es.ama.custom.crawler.CustomCrawler 介面的類別名稱 |
custom_security_class |
實作 com.ibm.es.ama.custom.crawler.CustomCrawlerSecurityHandler 介面的類別名稱 |
document_level_security_supported |
指定是已啟用 (true) 還是停用 (false) 文件層次安全 |
若要指定介面,請使用類似以下片段的程式碼:
<declare type="string" name="custom_config_class" hidden="true" initial-value="com.ibm.es.ama.custom.crwler.sample.sftp.SftpCrawler" />
<declare type="string" name="custom_crawler_class" hidden="true" initial-value="com.ibm.es.ama.custom.crwler.sample.sftp.SftpCrawler" />
<declare type="string" name="custom_security_class" hidden="true" initial-value="com.ibm.es.ama.custom.crwler.sample.sftp.SftpCrawler" />
<declare type="boolean" name="document_level_security_supported" initial-value="true" hidden="true"/>
如果您建置的自訂連接器具有與 2.2.1 版或更早版本組合在一起的 SDK 套件,則必須停用 document_level_security_supported (設為 false)。 2.2.1 及更早版本不支援文件層次安全。 不過,即使不支援文件層次安全,Discovery 中仍會顯示 啟用文件層次安全 選項。 當您建立新的集合時,請勿選取此選項。
若要在 Discovery 中隱藏 啟用文件層次安全 選項 (如果使用與 2.2.1 版或更早版本組合在一起的 SDK 套件建置自訂連接器),請完成下列步驟:
-
將
config/template.xml檔案中的document_level_security_supported參數變更為如下所示:<declare type="boolean" name="document_level_security_supported" hidden="true" initial-value="false"/> -
重建連接器套件,然後重新上傳。
區段:datasource_settings
XPath 表示式為 /function/prototype/proto-section[@section="datasource_settings"]。 它包含資料來源特有的設定。
<proto-section section="datasource_settings">
<declare type="string" name="host" required="required" initial-value="localhost"/>
<declare type="long" name="port" required="required" initial-value="22"/>
<declare type="string" name="user" required="required" />
<declare type="boolean" name="use_key" initial-value="true" />
<condition name="use_key" enabled="true">
<declare type="string" name="key" hidden="false" />
<declare type="password" name="passphrase" hidden="false" />
</condition>
<condition name="use_key" enabled="false">
<declare type="password" name="secret_key" hidden="false" />
</condition>
</proto-section>
區段:crawlspace_settings
XPath 表示式為 /function/prototype/proto-section[@section="crawlspace_settings"]。 本節只包含一個 <declare /> 元素來指定路徑。 路徑的值由連接器程式碼提供。
<proto-section section="crawlspace_settings" cardinality="multiple">
<declare type="string" name="path" hidden="true" />
</proto-section>
內容檔
如需內容檔的範例,請參閱範例 messages.properties 檔案,檔案位置列在瞭解 custom-crawler-docs.zip 檔案。
Jar 檔
自訂連接器程式碼使用之任何介面的 JAR 檔,包括在ama-zing-custom-crawler-{version_numbers}.jar瞭解 檔案custom-crawler-docs.zip中列出其位置的 檔案。 ama-zing-custom-crawler-{version_numbers}.jar 檔案包含在com.ibm.es.ama.custom.crawler開發自訂連接器程式碼中說明的 Java 套件。
編譯和包裝自訂連接器
在撰寫自訂連接器的原始碼和配置檔後,需要對其進行編譯和包裝。
必要條件
若要編譯自訂連接器,您需要在本機系統上具備下列項目。 如需詳細資料,請參閱自訂連接器範例。
-
Java SDK 1.8 或更新版本
-
已安裝之 Discovery 實例中的
custom-crawler-docs.zip檔案 -
JSch 套件
-
範例自訂連接器的下列檔案:
- Java 原始碼 (
SftpCrawler.java和SftpSecurityHandler.java) - XML 定義檔 (
template.xml) - 內容檔 (
messages.properties)
請勿變更範例自訂連接器檔案的名稱或路徑。 這樣做可能會導致問題,包括建立失敗。
- Java 原始碼 (
編譯和包裝原始碼
-
確保您在本機系統的自訂連接器開發目錄中:
cd {local_directory} -
使用 Gradle 來編譯您的 Java 原始碼,並建立包含自訂連接器所有必要元件的壓縮檔案:
gradle build packageCustomCrawler
Gradle 會在 {local_directory}/build/distributions/{built_connector_zip_file} 中建立一個檔案,其中 {built_connector_zip_file} 的名稱基於 settings.gradle 的 rootProject.name 值。 例如,如果行文如下,Gradle 生成的文件名為 {local_directory}/build/distributions/my-sftp-connector.zip。
rootProject.name = 'my-sftp-connector'
下一步
繼續安裝和解除安裝自訂連接器,以將自訂連接器安裝到 Discovery 實例。