分别用Shell命令和JAVA代码对HDFS进行操作

HDFS-Shell命令操作

进入/usr/local/hadoop:

cd /usr/local/hadoop

首次使用hdfs时,需要使用format命令进行格式化:

./bin/hdfs namenode -format

使用start-dfs.sh命令启动hdfs,可以使用 jps 命令查看是否成功启动:

./sbin/start-dfs.sh

创建hdfs的文件目录(虚拟)

./bin/hdfs dfs -mkdir -p /user/hadoop/input

查看hdfs下的文件目录:

./bin/hdfs dfs -ls

在桌面创建一个测试文本文件:

touch /home/hadoop/桌面/testinput.txt

打开桌面的testinput文本,输入一些信息,如:hello world!什么的····

使用hdfs的put命令将本地文件考到hdfs的input目录中:

./bin/hdfs dfs -put /home/hadoop/桌面/testinput.txt input 

使用-ls查看input目录下的文件:

./bin/hdfs dfs -ls input

使用-cat命令查看input目录下testinput文件内容:

./bin/hdfs dfs -cat input/testinput.txt 

使用-get命令将input目录下的某一文件拷到本地磁盘中:

rm /home/hadoop/桌面/testinput.txt # 先删除桌面已有的testinput.txt
./bin/hdfs dfs -get input/testinput.txt /home/hadoop/桌面 #执行拷贝

使用-rm命令将input目录下某一文件删除:

./bin/hdfs dfs -rm /user/hadoop/input/testinput.txt

HDFS-Java程序设计

首先选择IDE,像:eclipse,idea等等

摸鱼怪选择的是idea,Ubuntu下安装idea具体步骤请参考这篇博文

在idea新建一个Java Project,并import需要的Hadoop JAR包,具体步骤请参考这篇博文

  • 判断HDFS目录中对应文件是否存在

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    
    public class HDFSFileIfExist {
          
          
        public static void main(String[] args){
          
          
            try {
          
          
                String fileName = "hdfs://localhost:9000/user/hadoop/input/testinput.txt";
                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(fileName))){
          
          
                    System.out.println("exists!");
                }
                else{
          
          
                    System.out.println("not exists!");
                }
                fs.close();
            }
            catch (Exception e){
          
          
                e.printStackTrace();
            }
        }
    }
    

    如果存在testinput.txt文件存在,则返回 exists! ;否则返回 not exists!

    如果出现一下情况:

    解决方法:

    添加hdfs下的hadoop-hdfs-client-3.2.1.jar(可能版本号不一样)

  • 读取HDFS目录中testinput.txt文件内容

    import java.io.InputStream;
    import java.net.URL;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
    import org.apache.hadoop.io.IOUtils;
    
    
    public class ReadHDFSFileContents {
          
          
        public static void main(String[] args){
          
          
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://localhost:9000");
            conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
            try{
          
          
                URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
                InputStream in = new URL("hdfs://localhost:9000/user/hadoop/input/testinput.txt").openStream();
                IOUtils.copyBytes(in, System.out, 4096, true);
            }
            catch(Exception e){
          
          
                e.printStackTrace();
            }
        }
    }
    

    返回结果应为testinput.txt文本里的内容

  • 读取HDFS相应文件的BLOCK信息

    import java.net.URI;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.BlockLocation;
    
    
    public class ReadHDFSBlockContents {
          
          
        public static void main(String[] args){
          
          
            String uri = "hdfs://localhost:9000/user/hadoop/input/testinput.txt";
            Configuration conf = new Configuration();
            try{
          
          
                FileSystem fs = FileSystem.get(new URI(uri), conf);
                Path path = new Path(uri);
                FileStatus fileStatus = fs.getFileStatus(path);
                BlockLocation blkLocation[] = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
                for (int i=0; i<blkLocation.length; i++)
                {
          
          
                    String[] hosts = blkLocation[i].getHosts();
                    System.out.println("block_" + i + "_location: "+hosts[0]);
                }
            }
            catch (Exception e){
          
          
                e.printStackTrace();
            }
        }
    }
    
  • 删除HDFS目录中相应文件

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    public class DeleteHDFSFile {
          
          
        public static void main(String[] args){
          
          
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://localhost:9000");
            try{
          
          
                FileSystem fs = FileSystem.get(conf);
                boolean deleteOnExit = fs.deleteOnExit(new Path("/user/hadoop/input/testinput.txt"));
                System.out.println(deleteOnExit);
            }
            catch (Exception e)
            {
          
          
                e.printStackTrace();
            }
        }
    }
    
    

    返回结果为 true 或者 false

猜你喜欢

转载自blog.csdn.net/xwmrqqq/article/details/105876385