1.17总结

Java代码
package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.;
import java.io.;
public class HDFSApi {
/**

  • 判断路径是否存在
    /
    public static boolean test(Configuration conf, String path) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    return fs.exists(new Path(path));
    }
    /*
  • 复制文件到指定路径
  • 若路径已存在,则进行覆盖
    /
    public static void copyFromLocalFile(Configuration conf, String localFilePath, String
    remoteFilePath) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path localPath = new Path(localFilePath);
    Path remotePath = new Path(remoteFilePath);
    / fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆
    盖 /
    fs.copyFromLocalFile(false, true, localPath, remotePath);
    fs.close();
    }
    /*
  • 追加文件内容
    /
    public static void appendToFile(Configuration conf, String localFilePath, String
    remoteFilePath) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path remotePath = new Path(remoteFilePath);
    / 创建一个文件读入流 /
    FileInputStream in = new FileInputStream(localFilePath);
    / 创建一个文件输出流,输出的内容将追加到文件末尾 /
    FSDataOutputStream out = fs.append(remotePath);
    / 读写文件内容 */
    byte[] data = new byte[1024];
    int read = -1;
    while ( (read = in.read(data)) > 0 ) {
    out.write(data, 0, read);
    }
    out.close();
    in.close();
    fs.close();
    }

/**

  • 主函数
    /
    public static void main(String[] args) {
    Configuration conf = new Configuration();
    conf.set("fs.default.name","hdfs://node1:8020");
    String localFilePath = "E:\local.txt"; // 本地路径
    String remoteFilePath = "/user/root/text.txt"; // HDFS 路径
    String choice = "append"; // 若文件存在则追加到文件末尾
    // String choice = "overwrite"; // 若文件存在则覆盖
    try {
    /
    判断文件是否存在 /
    Boolean fileExists = false;
    if (HDFSApi.test(conf, remoteFilePath)) {
    fileExists = true;
    System.out.println(remoteFilePath + " 已存在.");
    } else {
    System.out.println(remoteFilePath + " 不存在.");
    }
    / 进行处理 */
    if ( !fileExists) { // 文件不存在,则上传
    HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath);
    System.out.println(localFilePath + " 已上传至 " + remoteFilePath);
    } else if ( choice.equals("overwrite") ) { // 选择覆盖
    HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath);
    System.out.println(localFilePath + " 已覆盖 " + remoteFilePath);
    } else if ( choice.equals("append") ) { // 选择追加
    HDFSApi.appendToFile(conf, localFilePath, remoteFilePath);
    System.out.println(localFilePath + " 已追加至 " + remoteFilePath);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    log4j.properties
    log4j.rootLogger=info,consolePrint,errorFile,logFile

log4j.appender.consolePrint.Encoding = UTF-8
log4j.appender.consolePrint = org.apache.log4j.ConsoleAppender
log4j.appender.consolePrint.Target = System.out
log4j.appender.consolePrint.layout = org.apache.log4j.PatternLayout
log4j.appender.consolePrint.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logFile.Encoding = UTF-8
log4j.appender.logFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logFile.File = D:/eclipse/log4j_properties_runlog/WorldCount_demo_run.log
log4j.appender.logFile.Append = true
log4j.appender.logFile.Threshold = info
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

log4j.appender.errorFile.Encoding = UTF-8
log4j.appender.errorFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorFile.File = D:/eclipse/log4j_properties_runlog/WorldCount_demo_error.log
log4j.appender.errorFile.Append = true
log4j.appender.errorFile.Threshold = ERROR
log4j.appender.errorFile.layout = org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
· 运行截图

(2)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;
shell命令
if (hdfsdfstestefile:///root/text.txt);then(hdfs dfs -copyToLocal text.txt ./text2.txt);
else $(hdfs dfs -copyToLocal text.txt ./text.txt);
Fi

posted @   奉禾  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示
主题色彩