从 Spark 访问 OpenStack Swift

Spark 对 Hadoop InputFormat 的支持使其能够以与 Hadoop 相同的 URI 格式处理 OpenStack Swift 中的数据。您可以通过形如 swift://container.PROVIDER/path 的 URI 将 Swift 中的路径指定为输入。您还需要通过 core-site.xmlSparkContext.hadoopConfiguration 设置您的 Swift 安全凭据。当前的 Swift 驱动程序要求 Swift 使用 Keystone 认证方法或其 Rackspace 特定的前身。

配置 Swift 以获得更好的数据本地性

虽然不是强制性的,但建议使用 list_endpoints 配置 Swift 的代理服务器以获得更好的数据本地性。更多信息可在此处获取。

依赖项

Spark 应用程序应包含 hadoop-openstack 依赖项,这可以通过包含用于特定 Spark 版本的 hadoop-cloud 模块来完成。例如,对于 Maven 支持,请将以下内容添加到 pom.xml 文件中

<dependencyManagement>
  ...
  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>hadoop-cloud_2.13</artifactId>
    <version>${spark.version}</version>
  </dependency>
  ...
</dependencyManagement>

配置参数

创建 core-site.xml 并将其放置在 Spark 的 conf 目录下。应配置的主要参数类别是 Keystone 所需的认证参数。

下表包含 Keystone 强制参数的列表。PROVIDER 可以是任何(字母数字)名称。

属性名称含义必需
fs.swift.service.PROVIDER.auth.url Keystone 认证 URL 强制
fs.swift.service.PROVIDER.auth.endpoint.prefix Keystone 端点前缀 可选
fs.swift.service.PROVIDER.tenant 租户 强制
fs.swift.service.PROVIDER.username 用户名 强制
fs.swift.service.PROVIDER.password 密码 强制
fs.swift.service.PROVIDER.http.port HTTP 端口 强制
fs.swift.service.PROVIDER.region Keystone 区域 强制
fs.swift.service.PROVIDER.public 指示是使用公共(云外)还是私有(云内;无传输费用)端点 强制

例如,假设 PROVIDER=SparkTest,并且 Keystone 包含为租户 test 定义的用户 tester (密码为 testing)。那么 core-site.xml 应包含

<configuration>
  <property>
    <name>fs.swift.service.SparkTest.auth.url</name>
    <value>http://127.0.0.1:5000/v2.0/tokens</value>
  </property>
  <property>
    <name>fs.swift.service.SparkTest.auth.endpoint.prefix</name>
    <value>endpoints</value>
  </property>
    <name>fs.swift.service.SparkTest.http.port</name>
    <value>8080</value>
  </property>
  <property>
    <name>fs.swift.service.SparkTest.region</name>
    <value>RegionOne</value>
  </property>
  <property>
    <name>fs.swift.service.SparkTest.public</name>
    <value>true</value>
  </property>
  <property>
    <name>fs.swift.service.SparkTest.tenant</name>
    <value>test</value>
  </property>
  <property>
    <name>fs.swift.service.SparkTest.username</name>
    <value>tester</value>
  </property>
  <property>
    <name>fs.swift.service.SparkTest.password</name>
    <value>testing</value>
  </property>
</configuration>

请注意,fs.swift.service.PROVIDER.tenantfs.swift.service.PROVIDER.usernamefs.swift.service.PROVIDER.password 包含敏感信息,将它们保留在 core-site.xml 中并非总是最佳方法。我们建议在通过 spark-shell 运行 Spark 进行测试时,将这些参数保留在 core-site.xml 中。对于作业提交,应通过 sparkContext.hadoopConfiguration 提供它们。