首页 > 其他分享 >12月13每日打卡

12月13每日打卡

时间:2023-12-13 22:57:08浏览次数:28  
标签:HDFSApi 13 12 String fs localFilePath remoteFilePath conf 打卡

 

 

HDFSApi

package HDFSApi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;

import HDFSApi.SCP;

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) throws IOException {

SCP.get();//通过SCP将linux上的文件下载到windows上

Configuration conf = new Configuration();
conf.set("fs.default.name","hdfs://192.168.88.101:8020");
String localFilePath = "D:\\local.txt"; // 本地路径
String remoteFilePath = "/user/hadoop/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();
}
}
}

 

标签:HDFSApi,13,12,String,fs,localFilePath,remoteFilePath,conf,打卡
From: https://www.cnblogs.com/yunbianshangdadun/p/17900122.html

相关文章

  • P4463 [集训队互测 2012] calc 题解
    Description一个序列\(a_1,a_2,\dots,a_n\)是合法的,当且仅当:\(a_1,a_2,\dots,a_n\)都是\([1,k]\)中的整数。\(a_1,a_2,\dots,a_n\)互不相等。一个序列的值定义为它里面所有数的乘积,即\(a_1\timesa_2\times\dots\timesa_n\)。求所有不同合法序列的值的和对\(p\)......
  • 2023.12.12 校赛解题报告
    7-1点菜题面Congruent和ComistryMo两个人正在饭店吃饭,饭店里面一共有\(n\)道菜,每道菜有一个价格\(a_i\)​\((1≤i≤n)\)。他俩会在饭店按顺序点\(k\)道菜(可以不连续,保证相对顺序即可),假设点的菜序列为\(S_1−S_k\)。他们约定:一个人付所有奇数下标已点菜品价格的最大......
  • 12.13每日总结
    packagetuxiang;importokhttp3.*;importorg.json.JSONObject;importjavax.imageio.ImageIO;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.image.BufferedImage;importja......
  • B3912 [语言月赛 202312] 打表过样例
    [语言月赛202312]打表过样例题目背景众所周知,一名负责人的出题人,不应当让如下的打表程序获得过多的分数。#include<iostream>intmain(){std::cout<<"SpecificValue\n";}这个程序的功能是,输出一个特定的内容,以尝试通过一些测试点而获得分数。经典的例子是:http......
  • 12.13 闲话
    昨天\(12.12\)今年是西安事变\(87\)周年所以\(\mathrm{CCF}\)送给\(\mathrm{HE}\)省队一个\(\mathrm{HE}\)事变,现在看起来大家都已经快似了(悲)今天打了个Hash水题???企鹅DescriptionPenguinQQ是中国最大、最具影响力的SNS(SocialNetworkingServices)网站,以实名制为基础,为用户......
  • (13)不运行程序,立即预览报表
    前提是要在设计的时候,数据库已正确连接和显示 双击frxReport1 ......
  • 2023-12-13
    packagecom.example.chatroom.api;importcom.example.chatroom.component.OnlineUserManger;importcom.example.chatroom.model.*;importcom.fasterxml.jackson.core.JsonProcessingException;importcom.fasterxml.jackson.databind.ObjectMapper;importorg.sprin......
  • 闲话12.13
    摆。上午ds,vjudge题单刚开就切了七道,妈的树点涂色啥时候加的hack,吃了两发罚时下午才改掉这题。上午喜提rk2。刚开始讲20min就开始肚子难受,窜了。感觉宿舍又热又冷的,还没睡觉的时候热的要死,睡着了又能着凉,难受。最近两天还上火了,不爽。但是ds有一半都听懂了,赢。于是后......
  • [20231213]tmux与环境变量PTAH.txt
    [20231213]tmux与环境变量PTAH.txt--//昨天给一台机器安装配置tmux,发现登陆tmux后环境变量PATH特别长,问题在于tmux登陆后要重复执行.bash_profile的内容.--//以前遇到过,主要问题在于.bash_profile在配置PATH时写法不合理.exportPATH=$PATH;...exportPATH=$PATH;...exportPAT......
  • 每日总结12.13
    今天完成软件构造的大作业中,一直报这个错误,目前不知道是什么原因,还没解决。在解决的过程中发现可能和下面有关,但都没有问题,之后再换个办法解决吧。确认Maven或Gradle依赖配置是否正确:如果你使用Maven或Gradle进行依赖管理,确保在你的配置文件中添加了OkHttp依赖。你......