IBM Cloud Docs
组装和编译自定义连接器

组装和编译自定义连接器

将若干组件文件打包在一起可创建定制连接器。

IBM Cloud Pak for Data IBM Software Hub

此信息仅适用于已安装的部署。

定制连接器组件

自定义连接器包是一个压缩文件,包含以下组件:

连接器组件
路径 描述
config/template.xml 配置模板
config/messages.properties 用于 UI 消息的属性文件
lib/*.jar 定制连接器需要的 JAR 文件,不包括您编写的连接器代码

配置模板

配置模板是划分为节的 XML 文件。 每个节包含相关设置。 XML 片段从示例 template.xml 文件中获取,该文件的位置在了解 custom-crawler-docs.zip 文件中列出。

声明设置

声明的设置由 <declare /> 元素表示。 此元素具有以下属性:

声明元素属性
属性名称 描述
type 数据类型;为下列其中一个值:stringlongbooleanlist(字符串列表)或 enum
name 设置的名称
initial-value 设置的初始值
enum-value enum 值列表,以竖线( | )分隔
required 指示设置是必需的
hidden 指示是否在 UI 隐藏中设置。 指定值 true 以隐藏设置。

在当前版本中,requiredhidden 属性不适用于 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 搜寻器的名称(类型)。 这不是 UI 的显示名称。 不能包含空格。
/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"/>

如果使用与 V 2.2.1 或更低版本捆绑的 SDK 软件包构建了定制连接器,那么必须禁用 document_level_security_supported (设置为 false)。 在 2.2.1 和更低发行版中不支持文档级别安全性。 但是,即使不支持文档级安全性,Discovery 中也会显示 启用文档级安全性 选项。 创建新集合时,请勿选择此选项。

要隐藏 Discovery 中的 启用文档级别安全性 选项 (如果定制连接器是使用与 V 2.2.1 或更低版本捆绑的 SDK 包构建的),请完成以下步骤:

  1. config/template.xml 文件中的 document_level_security_supported 参数更改为如下所示:

    <declare type="boolean" name="document_level_security_supported" hidden="true" initial-value="false"/>
    
  2. 重新构建连接器软件包,然后重新上载。

节: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 或更高版本

  • Gradle

  • 已安装的 Discovery 实例中的 custom-crawler-docs.zip 文件

  • JSch 包

  • 示例定制连接器的以下文件:

    • Java 源代码 (SftpCrawler.javaSftpSecurityHandler.java)
    • XML 定义文件 (template.xml)
    • 属性文件 (messages.properties)

    请勿更改示例定制连接器文件的名称或路径。 这样做可能会导致问题,包括制造失败。

编译和打包源代码

  1. 确保您位于本地系统自定义连接器开发目录中:

    cd {local_directory}
    
  2. 使用 Gradle 编译您的 Java 源代码,并创建一个包含自定义连接器所需所有组件的压缩文件:

    gradle build packageCustomCrawler
    

Gradle 在 {local_directory}/build/distributions/{built_connector_zip_file} 中创建文件,其中 {built_connector_zip_file} 的名称基于 settings.gradlerootProject.name 值。 例如,如果行如下所示,Gradle 将生成一个名为 {local_directory}/build/distributions/my-sftp-connector.zip 的文件。

rootProject.name = 'my-sftp-connector'

下一步

继续安装和卸载定制连接器,以将定制连接器安装到 Discovery 实例。