分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

FTP文件上传到HDFS上

发布时间:2023-09-06 01:16责任编辑:胡小海关键词:文件上传

在做测试数据时,往往会有ftp数据上传到hdfs的需求,一般需要手动操作,这样做太费事,于是有了下边代码实现的方式:

ftp数据上传到hdfs函数:

import java.io.InputStream;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;/** * Created by Administrator on 11/10/2017. */public class FtpUtil { ???/** ????* loadFromFtpToHdfs:将数据从ftp上传到hdfs上. <br/> ????* ????* @param ip ????* @param username ????* @param password ????* @param filePath ????* @param outputPath ????* @param conf ????* @return ????* @author qiyongkang ????* @since JDK 1.8 ????*/ ???public static boolean loadFromFtpToHdfs(String ip, String username, String password, String filePath, String outputPath, Configuration conf) { ???????FTPClient ftp = new FTPClient(); ???????InputStream inputStream = null; ???????FSDataOutputStream outputStream = null; ???????boolean flag = true; ???????try { ???????????ftp.connect(ip); ???????????ftp.login(username, password); ???????????ftp.setFileType(FTP.BINARY_FILE_TYPE); ???????????ftp.setControlEncoding("UTF-8"); ???????????int reply = ftp.getReplyCode(); ???????????if (!FTPReply.isPositiveCompletion(reply)) { ???????????????ftp.disconnect(); ???????????} ???????????FTPFile[] files = ftp.listFiles(filePath); ???????????FileSystem hdfs = FileSystem.get(conf); ???????????for (FTPFile file : files) { ???????????????if (!(file.getName().equals(".") || file.getName().equals(".."))) { ???????????????????inputStream = ftp.retrieveFileStream(filePath + file.getName()); ???????????????????outputStream = hdfs.create(new Path(outputPath + file.getName())); ???????????????????IOUtils.copyBytes(inputStream, outputStream, conf, false); ???????????????????if (inputStream != null) { ???????????????????????inputStream.close(); ???????????????????????ftp.completePendingCommand(); ???????????????????} ???????????????} ???????????} ???????????ftp.disconnect(); ???????} catch (Exception e) { ???????????flag = false; ???????????e.printStackTrace(); ???????} ???????return flag; ???}}

main调用函数:

import org.apache.hadoop.conf.Configuration/** ?* Created by Administrator on 11/10/2017. ?*/object FtpDownToHdfsMain { ?def main(args: Array[String]): Unit = { ???val conf = new Configuration() ???FtpUtil.loadFromFtpToHdfs("192.168.1.23", "test", "abc123", "/www/input/", "/user/jr/dt/fblib/", conf) ?}}

使用yarn jar提交:

yarn jar myapp.jar

FTP文件上传到HDFS上

原文地址:http://www.cnblogs.com/yy3b2007com/p/7650409.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved