首页 > 编程语言 >java操作hdfs

java操作hdfs

时间:2023-01-09 20:07:06浏览次数:29  
标签:hdfs fs java org new IOException Path import 操作

package cagy.hap;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test; public class HDFSClientS {

private FileSystem fs=null; public void getFS() throws IOException
{
Configuration conf=new Configuration();
conf.set("fs.defaultFS","hdfs://192.168.0.109:9000/");
conf.set("dfs.replication","2");
fs=FileSystem.get(conf);
}

public void upload() throws IllegalArgumentException, IOException
{
getFS();
Path p=new Path("666.exe");
fs.copyFromLocalFile(p, new Path("/cagy/"));
}

public void deleteFile() throws IllegalArgumentException, IOException
{
getFS();
boolean res= fs.delete(new Path("/cagy"), true);
System.out.println(res?"ok":"failed");

}
//@Test
public void createMkdir() throws IllegalArgumentException, IOException
{
getFS();
fs.mkdirs(new Path("/cagy"));
System.out.println("end!");
}

public void rename() throws IllegalArgumentException, IOException
{
getFS();
fs.rename(new Path("/q.txt"), new Path("/qq.txt"));
System.out.println("end!");
}
@Test
public void ls() throws FileNotFoundException, IllegalArgumentException, IOException
{
getFS();
RemoteIterator<LocatedFileStatus> ls=fs.listFiles(new Path("/"), true);
while(ls.hasNext())
{
//列出所有文件
LocatedFileStatus file= ls.next();
System.out.println(file.getPath().getName());
}
System.out.println("*********************");
FileStatus[] stat=fs.listStatus(new Path("/"));
for(FileStatus file:stat)
{
//列出根目录
System.out.println(file.getPath().getName()+" "+(file.isDirectory()?"文件夹":"文件"));
}

}

}

标签:hdfs,fs,java,org,new,IOException,Path,import,操作
From: https://blog.51cto.com/u_14650780/5998734

相关文章

  • 用Java写一个PDF,Word文件转换工具
    前言前段时间一直使用到word文档转pdf或者pdf转word,寻思着用Java应该是可以实现的,于是花了点时间写了个文件转换工具源码weloe/FileConversion(github.com)主要功能就......
  • 【java基础】创建不可变集合
    创建不可变集合List<Integer>list=List.of(1,2,3,4);//[1,2,3,4]Set<Integer>set=Set.of(1,2,3,4);//[1,2,3,4]Map<Integer,Integer>map=Map.of(1,2,3,4);//{1......
  • C++实现顺序栈相关操作代码
    #include<iostream>#include<cstdlib>usingnamespacestd;#defineMAXSIZE100#defineOK1#defineERROR0#defineOVERFLOW-2typedefintStatus;typedefintElemtype......
  • 从主机往虚拟机拷贝文件提示“虚拟机无法扫描本地目录,您可能没有执行本操作的权限
    下面的内容我在新浪博客发表过,地址是从主机往虚拟机拷贝文件提示“虚拟机无法扫描本地目录,您可能没有执行本操作的权限”_来自金沙江的小鱼_新浪博客(sina.com.cn)在这......
  • 【java基础】如何创建20元素以上的不可变集合?(Map.of()无法创建20个以上)
    背景由于Map.of()(jdk-9出现)创建的不可变集合无法超过20个参数,所以可以使用下面的办法创建Map<Object,Object>map=Map.ofEntries(hm.entrySet().toArray(newMap.Entry......
  • Java String类
    String类一、String类的理解和创建对象结构剖析String对象用于保存字符串,也就是一组字符序列;字符串常量对象是用双引号括起来的字符序列。例如:jack"字符串常量;......
  • HDFS常用基础命令
    hadoopfs-cat/wc/output1/part-r-00000hadoopfs-ls/wc/output1hadoopfs-rm-r/wc/output1删除目录以及下面的文件hadoopfs-puthl.txt/wc/data//当前目录......
  • mapreduce基础JOB操作
    packagecagy.mapreduce.wordcount;importjava.io.IOException;importorg.apache.hadoop.io.LongWritable;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.m......
  • HIVE简单操作命令
    beelinebeeline>!connectjdbc:hive2://192.168.2.2:10000hdfs回车回车直接用默认表,不需要配置权限createtablest(idint,namestring)rowformatdelimitedfieldster......
  • Java07 异常
    一、什么是异常实际工作中,遇到的情况不可能是非常完美的。比如:你写的某个模块,用户输入不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在或者文件格式不对......