HDFS文件的基本操作

HDFS文件的基本操作:

package wjn;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Scanner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
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.apache.hadoop.io.IOUtils;
import org.apache.log4j.BasicConfigurator;

public class ww {

    public static Scanner sc = new Scanner(System.in);
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        main9();
    }

    public static void main10() throws IOException{
        String p1 = "/user/hadoop/text/text1";
        String p2 = "/user/hadoop";
        if(mv(p1,p2)){
            System.out.println("文件移动成功");
        }else{
            System.out.println("文件移动失败");
        }
    }

    public static void main9() throws IOException{
        String hdfspath = "/user/hadoop/text2";

        if(deleteFileFromHDFS(hdfspath)){
        System.out.println("已删除文件"+hdfspath);
        }else{
            System.out.println("删除文件失败");
        }
    }

    public static void main7() throws IOException{

        String hdfspath1 = "/user/hadoop";
        boolean forcedelete = false;
        if(!ifex(hdfspath1)){
            mkdir(hdfspath1);
            System.out.println("创建目录"+hdfspath1);
        }else{
            if(isempty(hdfspath1)||forcedelete){
                rmDir(hdfspath1);
                System.out.println("删除目录"+hdfspath1);
            }else{
                System.out.println("目录不为空,不删除");
            }
        }

    }

public static void main6() throws IOException{

    String hdfspath = "/user/hadoop/text2";
    String hdfspath1 = "/user/hadoop";
    if(ifex(hdfspath)){
        deleteFileFromHDFS(hdfspath);
        System.out.println("该路径存在,删除路径"+hdfspath);
    }else{
        if(!ifex(hdfspath1)){
            mkdir(hdfspath1);
            System.out.println("创建文件夹"+hdfspath1);
        }
        touchz(hdfspath);
        System.out.println("创建路径"+hdfspath);
    }
    }

    public static void main5() throws IOException{

        String hdfspath = "/user/hadoop";
        System.out.println("所有文件信息如下");
        lsDir(hdfspath);
    }

    public static void main4() throws IOException{
        String hdfspath = "/user/hadoop/text2";
        System.out.println("文件信息如下");
        ls(hdfspath);
    }

    public static void main3() throws IOException{
        String hdfspath = "/user/hadoop/text2";
        cat(hdfspath);
        System.out.println("读取完成");
    }

    public static void main2() throws IOException{
        String localpath = "/home/hadoop/1234.txt";
        String hdfspath = "/user/hadoop/text2";
        download(hdfspath,localpath);
        System.out.println("文件下载成功");
    }

    public static void main1() throws IOException{
        String localpath = "/home/hadoop/123.txt";
        String hdfspath = "/user/hadoop/text2";
        if(ifex(hdfspath)){
            System.out.println("文件存在,请选择追加(1)还是覆盖(2)");
            int i = sc.nextInt();
            if(i==1){
                appendFileToHDFS(hdfspath,localpath);
                System.out.println("文件追加成功");
            }else if(i==2){
                deleteFileFromHDFS(hdfspath);
                update(localpath,hdfspath);
                System.out.println("文件覆盖成功");
            }else{
                System.out.println("输入有误");
            }

        }else{
            update(localpath,hdfspath);
            System.out.println("文件不存在,上传成功");
        }
    }

    public static void update(String localpath , String hdfspath) throws IOException{

        InputStream in = new BufferedInputStream(new FileInputStream(localpath));
        FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
        OutputStream out = fileSystem.create(new Path(hdfspath));

        IOUtils.copyBytes(in, out, 4096, true);
        fileSystem.close();

    }

    //判断hdfs中文件是否存在
    public static boolean  ifex(String hdfspath) throws IOException{

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://localhost:9000");
        conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(new Path(hdfspath))){
            fs.close();
            return true;
            }else{
                fs.close();
                return false;
            }
    }

    //创建目录
    public static void mkdir(String hdfspath) throws IOException{

        FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
        Path dirPath = new Path(hdfspath);
        fileSystem.mkdirs(dirPath);
        fileSystem.close();
    }

    //创建文件
public static void touchz(String hdfspath) throws IOException{

        FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
        Path dirPath = new Path(hdfspath);
        FSDataOutputStream outputStream = fileSystem.create(dirPath);
        outputStream.close();
        fileSystem.close();
    }

    public static void appendFileToHDFS(String hdfsPath, String localFilePath) throws IOException {
        Configuration config = new Configuration();
        config.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
        config.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");

        FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), config);

        InputStream in = new BufferedInputStream(new FileInputStream(localFilePath));
        FSDataOutputStream out = fileSystem.append(new Path(hdfsPath));

        IOUtils.copyBytes(in, out, 4096, true);
        fileSystem.close();
    }

    //删除文件
    public static boolean deleteFileFromHDFS(String hdfsPath) throws IOException {
        FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration());
        boolean result = fileSystem.deleteOnExit(new Path(hdfsPath));
        fileSystem.close();
        return result;
    }

    //删除目录
    public static void rmDir(String hdfspath) throws IOException{

        FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
        fileSystem.delete(new Path(hdfspath),true);
        fileSystem.close();
    }

    //判断目录是否为空
    public static boolean isempty(String hdfspath) throws IOException{

        FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
        RemoteIterator<LocatedFileStatus> remoteIterator = fileSystem.listFiles(new Path(hdfspath), true);
        return !remoteIterator.hasNext();
    }

    public static void download (String hdfsPath, String localPath) throws IOException {
        FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration());

        FSDataInputStream in = fileSystem.open(new Path(hdfsPath));
        OutputStream out = new FileOutputStream(localPath);

        IOUtils.copyBytes(in, out, 4096, true);
        fileSystem.close();
    }

    //根据hdfs路径输出其内容到终端
    public static void cat(String hdfspath) throws IOException{

           FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());

            FSDataInputStream in = fileSystem.open(new Path(hdfspath));

            BufferedReader d = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while((line = d.readLine())!=null){
                System.out.println(line);
            }
            d.close();
            in.close();
    }

    //显示hdfs中指定文件的信息
    public static void ls(String hdfspath) throws IOException{

        FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
        Path path = new Path(hdfspath);
        FileStatus[] fileStatus = fileSystem.listStatus(path);
        for (FileStatus s : fileStatus){
            System.out.println("路径:"+s.getPath().toString());
            System.out.println("权限:"+s.getPermission().toString());
            System.out.println("大小:"+s.getLen());
            Long time = s.getModificationTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = format.format(time);
            System.out.println("时间:"+date);
        }
    }

    //显示文件夹下所有文件的信息
    public static void lsDir(String hdfspath) throws IOException{

        FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
        Path dirPath = new Path(hdfspath);
        RemoteIterator<LocatedFileStatus> remoteIterator = fileSystem.listFiles(dirPath, true);
        while(remoteIterator.hasNext()){

            FileStatus s = remoteIterator.next();
            System.out.println("路径:"+s.getPath().toString());
            System.out.println("权限:"+s.getPermission().toString());
            System.out.println("大小:"+s.getLen());
            Long time = s.getModificationTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = format.format(time);
            System.out.println("时间:"+date);
            System.out.println();
        }
        fileSystem.close();
    }

    //移动文件
    public static boolean mv(String path1,String path2) throws IOException{

        FileSystem fileSystem = FileSystem.get(URI.create(path1), new Configuration());

        Path p1 = new Path(path1);
        Path p2 = new Path(path2);
        boolean result = fileSystem.rename(p1, p2);
        fileSystem.close();
        return result;
    }

}

原文地址:https://www.cnblogs.com/123456www/p/11525298.html

时间: 2024-08-30 06:30:59

HDFS文件的基本操作的相关文章

HDFS文件操作

hadoop装好后,文件系统中没有任何目录与文件 1. 创建文件夹 hadoop fs -mkdir -p /hkx/learn 参数-p表示递归创建文件夹 2. 浏览文件 hadoop fs -ls / 3. 上传本地文件到HDFS hadoop fs -put word.txt /hkx/learn 4. 查看文件 hadoop fs -cat /hkx/learn/word.txt HDFS Shell命令介绍文章很多,这里就不一一列举了,引用一篇文章来介绍:https://segment

Oracle Bigdata Connector实战1: 使用Oracle Loader加载HDFS文件到Oracle数据库

部署jdk/Hadoop/OraLoader软件包 将准备好的软件包,逐一解压到hadoop用户home目录下: hadoop-2.6.2.tar.gz jdk-8u65-linux-x64.gz oraloader-3.4.0.x86_64.zip Hadoop软件部署如下: ├── hadoop-2.6.2 ├── jdk1.8.0_65 ├── oraloader-3.4.0-h2 设置环境变量 export JAVA_HOME=/home/hadoop/jdk1.8.0_65 expor

【大数据系列】HDFS文件权限和安全模式、安装

HDFS文件权限 1.与linux文件权限类型 r:read w:write x:execute权限x对于文件忽略,对于文件夹表示是否允许访问其内容 2.如果linux系统用户sanglp使用hadoop命令创建一个文件,那么这个文件在HDFS中owner就是sanglp 3.HDFS的权限目的:阻止好人做错事,而不是阻止坏人做坏事. 安全模式 1. namenode启动的时候,首先将映像文件(fsimage)载入内存,并执行编辑日志(edits)中的各项操作. 2. 一旦在内存中成功建立文件系

Linux下文件的基本操作

文件的基本操作 新建和删除文件夹 命令#mkdir /file 在当前目录创建file文件夹 命令#rmdir /file 删除当前目录下file文件夹 复制和移动文件 命令#cp text/file.txt /text1/file1.txt  将text目录下file.txt 复制到text1并更名为file1.txt 命令#mv text/file.txt /text1 将text目录下file.txt复制到text1目录下 查看和创建文件 命令#cat text/file.txt 查看te

Hadoop之HDFS文件操作

摘要:Hadoop之HDFS文件操作常有两种方式,命令行方式和JavaAPI方式.本文介绍如何利用这两种方式对HDFS文件进行操作. 关键词:HDFS文件    命令行     Java API HDFS是一种分布式文件系统,为MapReduce这种框架下的海量数据分布式处理而设计. Hadoop之HDFS文件操作常有两种方式,一种是命令行方式,即Hadoop提供了一套与Linux文件命令类似的命令行工具:另一种是JavaAPI,即利用Hadoop的Java库,采用编程的方式操作HDFS的文件.

目录与文件的基本操作一 (44)

解析目录路径 使用PHP脚本可以方便对目录进行操作,如创建目录.遍历目录.复值目录与删除目录等操作.??常用的文件目录路径格式:??$unixPath="/var/www/html/index.php";//在UNIX系统中的绝对路径,必须使用"/"分隔??$winPath="C:\\Appserv\\www\\index.php";//在Windows系统的绝对路径,默认使用"\"分隔??$winPath2="C:

Windows PowerShell--目录文件的基本操作

Windows PowerShell(一) 目录与文件的基本操作 你可以通过一些命令浏览系统里的目录,创建,移动,删除目录还有文件.目录与文件的操作是使用命令行工具的基础.Windows 用户打开 Powershell ,然后试一下接下来的这些命令. (1)查看你当前的位置  pwd Powershell,还有 终端,在光标的左边默认会显示你当前所在的目录.你也可以使用 pwd ( print working directory )这个命令,查看自己当前所在的位置. C:\Users\Admin

Hadoop学习笔记0002——HDFS文件操作

  说明:Hadoop之HDFS文件操作常有两种方式,命令行方式和JavaAPI方式. 方式一:命令行方式 Hadoop文件操作命令形式为:hadoop fs -cmd <args> 说明:cmd是具体的文件操作命令,<args>是一组数目可变的参数. Hadoop最常用的文件操作命令,包括添加文件和目录.获取文件.删除文件等. 1 添加文件和目录 HDFS有一个默认工作目录/usr/$USER,其中$USER是你的登录用户名,作者的用户名是root.该目录不能自动创建,需要执行m

HDFS文件上传:8020端口拒绝连接问题解决!

HDFS文件上传:8020端口拒绝连接问题解决! CopyFromLocal: Call to localhost/127.0.0.1:8020 failed on connection exception:      java.net.ConnectException 问题提示本机的8020端口无法连接. 网络上面找到一篇文章是将core-site.xml里面的配置端口修改成8020,但是我们现在依然用他默认的9000端口,只需要在配置eclipse的时候端口修改为9000就可以了. 我的问题