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|
+---+---+---+---+
相关推荐
2501_9248785931 分钟前
强光干扰下漏检率↓78%!陌讯动态决策算法在智慧交通违停检测的实战优化
大数据·深度学习·算法·目标检测·视觉检测
做科研的周师兄1 小时前
【机器学习入门】1.2 初识机器学习:从数据到智能的认知之旅
大数据·数据库·人工智能·python·机器学习·数据分析·机器人
小白不想白a2 小时前
【Hadoop】HDFS 分布式存储系统
hadoop·分布式·hdfs
IT毕设梦工厂2 小时前
大数据毕业设计选题推荐-基于大数据的丙型肝炎患者数据可视化分析系统-Hadoop-Spark-数据可视化-BigData
大数据·hadoop·spark·毕业设计·源码·bigdata
阿里云大数据AI技术3 小时前
【跨国数仓迁移最佳实践7】基于MaxCompute多租的大数据平台架构
大数据
阿里云大数据AI技术3 小时前
ODPS 十五周年实录 | Data + AI,MaxCompute 下一个15年的新增长引擎
大数据·python·sql
SelectDB3 小时前
2-5 倍性能提升,30% 成本降低,阿里云 SelectDB 存算分离架构助力波司登集团实现降本增效
大数据·数据库·数据分析
随心............4 小时前
Spark面试题
大数据·分布式·spark
君不见,青丝成雪5 小时前
Flink的CheckPoint与SavePoint
大数据·flink
mask哥5 小时前
详解flink SQL基础(四)
java·大数据·数据库·sql·微服务·flink