import java.io._
import java.net.URI
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.io.IOUtils;
object HdfsFileOperate {
def Test(args: Array[String]) {
appendHdfs("hdfs://somepath", "test")
}
/**
* HDFS文件追加
* @param hdfs_path
* @param context
*/
def appendHdfs(hdfs_path:String, context: String): Unit = {
val conf:Configuration = new Configuration();
conf.setBoolean("dfs.support.append", true);
//var inpath = "/home/wyp/append.txt";
var fs:FileSystem = null;
try {
fs = FileSystem.get(URI.create(hdfs_path), conf);
if(!fs.exists(new Path(hdfs_path))) {
createFile(hdfs_path)
}
fs = FileSystem.get(URI.create(hdfs_path), conf);
var in = new ByteArrayInputStream(context.getBytes());
var out = fs.append(new Path(hdfs_path));
IOUtils.copyBytes(in, out, 4096, true);
} catch {
case e: Exception => println("HDFS文件追加:" + e)
} finally {
try{
fs.close()
}catch{
case e: Exception => println("HDFS文件追加[关闭FileSystem]:" + e)
}
}
}
def createFile(hdfs_file: String): Unit ={
val conf_f:Configuration = new Configuration();
var fs_f:FileSystem = null;
try {
fs_f = FileSystem.get(URI.create(hdfs_file), conf_f);
if(!fs_f.exists(new Path(hdfs_file))){
fs_f.create(new Path(hdfs_file))
}
} catch {
case e: Exception => println("创建HDFS文件:" + e)
} finally {
try{
fs_f.close()
}catch {
case e: Exception => println("创建HDFS文件[关闭FileSystem]:" + e)
}
}
}
def createPath(hdfs_path: String): Unit ={
val conf_p:Configuration = new Configuration();
var fs_p:FileSystem = null;
try {
fs_p = FileSystem.get(URI.create(hdfs_path), conf_p);
if(!fs_p.exists(new Path(hdfs_path))){
println("create hdfs path .......")
fs_p.mkdirs(new Path(hdfs_path));
}
} catch {
case e: Exception => println("创建HDFS目录:" + e)
} finally {
try{
fs_p.close()
}catch {
case e: Exception => println("创建HDFS目录[关闭FileSystem]:" + e)
}
}
}
def exists(path:String): Boolean = {
var fs_e:FileSystem = null;
val conf_e:Configuration = new Configuration();
var isExsits = false
try {
fs_e = FileSystem.get(URI.create(path), conf_e);
if(fs_e.exists(new Path(path))) {
isExsits = true
}
} catch {
case e: Exception => println("判断HDFS上【文件】或【文件夹】是否存在:" + e)
} finally {
try{
fs_e.close()
}catch {
case e: Exception => println("判断HDFS上【文件】或【文件夹】是否存在[关闭FileSystem]:" + e)
}
}
isExsits
}
}
HDFS 记录日志
最近热门
- Straight-Through Estimator(STE, 直推估计器)
- 流匹配(Flow Matching,FM)
- 模型证据下界(Evidence Lower Bound,ELBO)
- LLM | Chain of Thought(CoT,思维链)
- Zero Redundancy Optimizer(ZeRO)内存优化技术
- 面向任意目标的全库向量召回技术PDM
- 多模态对齐(Multimodal Alignment)
- STT模型(Speech-to-Text)
- 论文:HoME - Hierarchy of Multi - Gate Experts for Multi - Task Learning at Kuaishou
- RQ-VAE(Residual-Quantized VAE)变分自编码器和残差量化
最常浏览
- 016 推荐系统 | 排序学习(LTR - Learning To Rank)
- 偏微分符号
- i.i.d(又称IID)
- 利普希茨连续条件(Lipschitz continuity)
- (error) MOVED 原因和解决方案
- TextCNN详解
- 找不到com.google.protobuf.GeneratedMessageV3的类文件
- Deployment failed: repository element was not specified in the POM inside distributionManagement
- cannot access com.google.protobuf.GeneratedMessageV3 解决方案
- CLUSTERDOWN Hash slot not served 问题原因和解决办法
×