gpt4 book ai didi

Spring 对 WebHDFS 的支持

转载 作者:可可西里 更新时间:2023-11-01 14:18:57 26 4
gpt4 key购买 nike

是否有任何 Spring 支持 wedhdfs?我没有在谷歌上找到任何有用的链接。

我想通过 webhdfs 连接到具有正常身份验证和 kerberos 身份验证的 hadoop。 spring 支持吗?

任何有用的链接都会有帮助。

谢谢

最佳答案

是的,Spring Data 支持这个。根据此文档,可以配置任何受支持的 Hadoop 文件系统:

http://docs.spring.io/spring-hadoop/docs/current/reference/html/fs.html

SHDP does not enforce any specific protocol to be used - in fact, as described in this section any FileSystem implementation can be used, allowing even other implementations than HDFS to be used.

有关演示自动连接 WebHDFS 的代码示例,请参见下文 FileSystem实例到命令行应用程序中。要运行它,将文件路径作为命令行参数传递,它将通过调用 FileSystem.listStatus 列出该路径中存在的每个文件。 .

代码示例配置为使用“简单”身份验证连接到不安全的 WebHDFS 实例。要连接到受 Kerberos 保护的 WebHDFS 实例,您需要在 <hdp:configuration id="hadoopConfiguration" /> 中设置相关的配置属性。 bean 。 Hadoop 安全配置是一个很大的话题。我不再重复这些信息,而是指向 Apache 中的文档:

http://hadoop.apache.org/docs/r2.4.1/hadoop-project-dist/hadoop-common/SecureMode.html

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-spring-hadoop</groupId>
<artifactId>test-webhdfs</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Test Spring Hadoop with WebHDFS</name>
<description>Test Spring Hadoop with WebHDFS</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.0.RELEASE</version>
</parent>

<repositories>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>

<properties>
<start-class>testwebhdfs.Main</start-class>
<java.version>1.6</java.version>
<hadoop.version>2.4.1</hadoop.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
</project>

src/main/resources/hadoop-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">

<hdp:configuration id="hadoopConfiguration" />
<hdp:file-system uri="webhdfs://localhost:50070" />
</beans>

src/main/java/testwebhdfs/Main.java

package testwebhdfs;

import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("hadoop-context.xml")
public class Main implements CommandLineRunner {

@Autowired
private FileSystem fs;

@Override
public void run(String... strings) throws Exception {
Path[] paths = new Path[strings.length];
for (int i = 0; i < strings.length; ++i) {
paths[i] = new Path(strings[i]);
}
for (FileStatus stat: fs.listStatus(paths)) {
System.out.println(stat.getPath());
}
}

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}

关于Spring 对 WebHDFS 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25152585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com