How Spark Read Sftp Files from Hadoop SFTP FileSystem

Gradle Dependencies

gradle 复制代码
        implementation('org.apache.spark:spark-sql_2.13:3.5.3')
        implementation 'org.apache.hadoop:hadoop-common:3.3.4'

        testImplementation "org.springframework.boot:spring-boot-starter-test"
        testImplementation "org.apache.sshd:sshd-core:2.8.0"
        testImplementation "org.apache.sshd:sshd-sftp:2.8.0"

Setup a Fake SFTP server

java 复制代码
        // GIVEN
        // SETUP Fake SFTP Server
        String host = "127.0.0.1";
        String user = "username";
        String passwd = "password";
        int port = 9188;

        SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(port);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
        sshd.setPasswordAuthenticator((username, password, session) -> user.equals(username) && passwd.equals(password) );
        sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
        sshd.setFileSystemFactory(new VirtualFileSystemFactory(rootPath));

        sshd.start();
        System.out.println("Fake SFTP server started at port " + port);

Generate A tested CSV file based on Hadoop SFTP FileSystem api

java 复制代码
        String sftpURL = String.format("sftp://%s:%s@%s:%d", user, passwd, host, port);
        String testedCsvFile = "test.csv";
        // WHEN
        // Create a CSV file by Hadoop FileSystem api
        Configuration conf = new Configuration();
        conf.set("fs.sftp.impl", "org.apache.hadoop.fs.sftp.SFTPFileSystem");
        conf.set("fs.defaultFS", sftpURL);

        // get FileSystem instance by a root Path
        Path path = new Path("/");
        FileSystem sftpFileSystem = FileSystem.get(path.toUri(),conf);
        Assertions.assertTrue(sftpFileSystem instanceof SFTPFileSystem);

        // Create a test csv file and write text contents to it
        try (BufferedWriter br = new BufferedWriter(new OutputStreamWriter(sftpFileSystem.create(new Path(testedCsvFile), true)))) {
            br.write("A|B|C|D");
            br.newLine();
            br.write("1|2|3|4");
        }

        // check the tested file
        FileStatus[] statuses = sftpFileSystem.listStatus(new Path("/"));
        Assertions.assertEquals(1, statuses.length);
        Assertions.assertTrue(statuses[0].isFile());
        Assertions.assertEquals(testedCsvFile, statuses[0].getPath().getName());

Finally, Read the tested data from SFTP Server

java 复制代码
    // THEN
    // Read the test csv file by Spark
    SparkConf sparkConf = new SparkConf()
            .setAppName("spark-test")
            .setMaster("local[2]")
            .set("spark.ui.enabled","false")
            .set("spark.hadoop.fs.sftp.impl","org.apache.hadoop.fs.sftp.SFTPFileSystem")
            .set("spark.hadoop.fs.defaultFS",sftpURL)
            ;
    SparkSession sparkSession = SparkSession.builder().config(sparkConf).getOrCreate();

    // read csv file by the sftp connection
    Dataset<Row> dataset = sparkSession.read()
            .option("header","true").option("delimiter","|")
            .csv(testedCsvFile);
    dataset.printSchema();
    dataset.show();
        
text 复制代码
root
    |-- A: string (nullable = true)
    |-- B: string (nullable = true)
    |-- C: string (nullable = true)
    |-- D: string (nullable = true)

+---+---+---+---+
|  A|  B|  C|  D|
+---+---+---+---+
|  1|  2|  3|  4|
+---+---+---+---+
相关推荐
煤烦恼2 小时前
Spark-SQL核心编程(二)
大数据·sql·spark
小名叫咸菜4 小时前
Spark-SQL核心编程语言
spark
精神内耗中的钙奶饼干5 小时前
Windows 系统上安装和使用 Apache Kafka记录
大数据·kafka
观无5 小时前
基于AOP+Log4Net+AutoFac日志框架
java·大数据·数据库
EasyDSS5 小时前
安防监控视频管理平台EasyCVR助力建筑工地施工4G/5G远程视频监管方案
大数据·网络·网络协议·音视频
F36_9_6 小时前
质量问题频发,如何提升源头把控
大数据·运维
2401_890665866 小时前
免费送源码:Java+ssm+MySQL 基于PHP在线考试系统的设计与实现 计算机毕业设计原创定制
java·hadoop·spring boot·python·mysql·spring cloud·php
lqg_zone6 小时前
Elasticvue-轻量级Elasticsearch可视化管理工具
大数据·elasticsearch·搜索引擎
youka1507 小时前
大数据学习栈记——MongoDB编程
大数据·学习·mongodb
星辰瑞云7 小时前
Spark-SQL核心编程2
大数据·分布式·spark