public classTestHadoop {private staticConfiguration conf;private staticFileSystem fs;/*** 初始化
*@throwsException*/
public static void init() throwsException {
conf= newConfiguration();
conf.set(“fs.defaultFS”, “hdfs://localhost:9527”); //对应 core-site.xml 中配置的端口//拿到操作HDFS的一个实例,并且设置其用户(由于windows权限问题”zwj”需替换为管理员账号)
fs = FileSystem.get(new URI(“hdfs://localhost:9527″),conf,”zwj”);
}/*** 文件上传
*@throwsException*/
public static void upload()throwsException{//后面的true,是指如果文件存在,则覆盖
FSDataOutputStream fout = fs.create(new Path(“/mydir/001.jpg”), true);
InputStream in= new FileInputStream(“E:/tmp/qrcode/123.jpg”);//复制流,并且完成之后关闭流
IOUtils.copyBytes(in, fout, 1024,true);
}/*** 在指定位置读取
*@throwsException*/
public static void random()throwsException{
FSDataInputStream fin= fs.open(new Path(“/mydir/001.jpg”));//从0起始位置的位置开始读
fin.seek(0);
OutputStream out= new FileOutputStream(“E:/tmp/qrcode/111.jpg”);
IOUtils.copyBytes(fin, out,1024,true);
}/*** 获取hadoop配置
*@throwsException*/
public static void conf()throwsException{
Iterator> iterator =conf.iterator();while(iterator.hasNext()){
Map.Entry entry =iterator.next();
System.out.println(entry);
}
}/*** 创建文件夹
*@throwsException*/
public static void mkdir()throwsException{boolean mkdirs = fs.mkdirs(new Path(“/mydir/dir1/dir2”));if(mkdirs){
System.out.println(“创建文件夹成功”);
}
fs.close();
}/*** 删除文件及文件夹
*@throwsException*/
public static void delete()throwsException{//递归删除
boolean delete = fs.delete(new Path(“/mydir”), true);if(delete){
System.out.println(“删除成功”);
}
fs.close();
}/*** 递归列出所有文件
*@throwsException*/
public static void listFile()throwsException{
RemoteIterator listFiles = fs.listFiles(new Path(“/”), true);while(listFiles.hasNext()){
LocatedFileStatus lfs=listFiles.next();
System.out.println(“块大小:” +lfs.getBlockSize());
System.out.println(“所属组:” +lfs.getOwner());
System.out.println(“大小:” +lfs.getLen());
System.out.println(“文件名:” +lfs.getPath().getName());
System.out.println(“是否目录:” +lfs.isDirectory());
System.out.println(“是否文件:” +lfs.isFile());
System.out.println();
BlockLocation[] blockLocations=lfs.getBlockLocations();for(BlockLocation blockLocation : blockLocations) {
System.out.println(“块偏移数:” +blockLocation.getOffset());
System.out.println(“块长度:” +blockLocation.getLength());
System.out.println(“块名称:” +Arrays.toString(blockLocation.getNames()));
System.out.println(“块名称:” +Arrays.toString(blockLocation.getHosts()));
}
System.out.println(“————————–“);
}
}/*** 列出指定目录下的文件
*@throwsException*/
public static void listFile2()throwsException{
FileStatus[] listStatus= fs.listStatus(new Path(“/”));for(FileStatus fileStatus : listStatus) {
System.out.println(“块大小:” +fileStatus.getBlockSize());
System.out.println(“所属组:” +fileStatus.getOwner());
System.out.println(“大小:” +fileStatus.getLen());
System.out.println(“文件名:” +fileStatus.getPath().getName());
System.out.println(“是否目录:” +fileStatus.isDirectory());
System.out.println(“是否文件:” +fileStatus.isFile());
}
}public static voidmain(String[] args) {try{
init();//upload();//random();//conf();//mkdir();//delete();
listFile();//listFile2();
} catch(Exception e){
e.printStackTrace();
}
}
}