首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对WebHDFS的弹簧支持

对WebHDFS的弹簧支持
EN

Stack Overflow用户
提问于 2014-08-06 05:04:37
回答 1查看 1.7K关注 0票数 5

有什么Spring支持的楔子吗?我在google上没有找到任何有用的链接。

我想通过webhdfs通过普通身份验证和kerberos身份验证连接到hadoop。这个在春天支持吗?

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-06 06:31:59

是的,Spring数据支持这一点。根据本文档,可以配置任何支持的Hadoop文件系统:

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

SHDP不强制使用任何特定的协议--事实上,正如本节所述,可以使用任何FileSystem实现,甚至允许使用HDFS以外的其他实现。

有关演示将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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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);
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25152585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档