大数据挖掘分析与应用 (五) HDFS操作

启动hdfs:在sbin目录下./start-dfs.sh,然后jps检测。
在这里插入图片描述
HDFS的shell命令
首先追加hadoop环境变量
在这里插入图片描述
在这里插入图片描述
重启使之生效
在这里插入图片描述
在这里插入图片描述
1…创建一个文件:hadoop fs -mkdir (-p )hello.txt
在这里插入图片描述
2…查看文件
hadoop fs -ls (-R )目录 (-R是查看全部文件)
hadoop fs -cat 文件名
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3…移动文件
hadoop fs -put 文件 hdfs路径
就是把本地文件上传到hdfs’系统中
在这里插入图片描述
在这里插入图片描述
4…复制文件
hadoop fs -cp 文件 复制的位置
在这里插入图片描述
在这里插入图片描述
5…删除文件
hadoop fs -rm -r 文件名(/hello.txt)
在这里插入图片描述
6…重命名文件
hadoop fs -mv 源文件 改的名字文件
在这里插入图片描述
在这里插入图片描述
7…get 命令:从hdfs系统中取文件下来到本地
hadoop fs -get hdfs中文件 本地目录
在这里插入图片描述
三 Java API操作HDFS文件
1.IDEA+Maven创建Java工程
new projects然后选maven,配置maven(详细可以查看csdn的其他人写的配置maven。非常详细)直到idea显示sucess为止。在这里插入图片描述
在里面添加hadoop版本,这样下面添加依赖时就可以直接用了
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

porm.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>0410</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>0410</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <hadoop.version>2.6.0-cdh5.15.0</hadoop.version>
  </properties>

  <repositories>
    <repository>
      <id>cloudera</id>
      <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>${
    
    hadoop.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>RELEASE</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

2.通过JavaAPI对HDFS系统进行操作

package org.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import java.net.URI;

public class HDFSapp {
    
    
    public static final String HDFS_PATH="hdfs://192.168.1.65:8020";

    //定义一个HDFS文件系统的对象
    FileSystem fileSystem = null;
    //HDFS配置对象
    Configuration configuration= null;

    ///准备工作(构造个函数)
    @Before  //单元测试
    public void setUp() throws Exception{
    
    
        System.out.println("HDFSapp.SetUp()");
        configuration=new Configuration();
        fileSystem=FileSystem.get(new URI(HDFS_PATH),configuration,"lqz");


    }

    //创建文件目录
    @Test
    public void mkdir()throws Exception{
    
    
        fileSystem.mkdirs(new Path("/hdfstest0410/test"));
    }

    //创建文件
    @Test
    public void create()throws Exception{
    
    
        FSDataOutputStream output =fileSystem.create(new Path("/hdfstest0410/test/lqz0410.txt")); //FSData是返回值
        output.write("hello world!".getBytes());  //追加hello world 以字节码的形式写进文件
        output.flush();//刷新
        output.close();//关闭文件
    }

    //结束工作
    @After
    public void tearDown() throws Exception{
    
    
        configuration=null;
        fileSystem=null;

        System.out.println("结束工作");
    }
    //查看文件内容
    @Test
    public void cat()throws Exception{
    
    
        FSDataInputStream inputStream = fileSystem.open(new Path("/hdfstest0410/test/lqz0410.txt"));
        IOUtils.copyBytes(inputStream,System.out,1024);
        inputStream.close();
    }


}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
上面是主要的代码,下面是测试代码
在这里插入图片描述
在这里插入图片描述

    //删除文件
    @Test
    public void delete()throws Exception{
    
    
        fileSystem.delete(new Path("/1234"),true);
        //recursive 相当于rm -r,如果是true将整个一个子目录甚至文件夹删除,默认目录是删除不掉的,ture才可以删除
    }

在这里插入图片描述
在这里插入图片描述

    //重命名文件
    @Test
    public void rename()throws Exception{
    
    
        Path oldPath=new Path("/hdfstest0410/test/lqz0410.txt");
        Path newPath=new Path("/hdfstest0410/test/0410.txt");
        fileSystem.rename(oldPath,newPath);//旧名字,新名字
    }

在这里插入图片描述
在这里插入图片描述

    //复制文件,从本地上传到HDFS
    //copyFromLocalFile从本地上传到hdfs,如果hdfs是一个文件那么就把文件内容复制到hdfs中的文件中去
    //如果hdfs是一个目录,默认把文件拷贝到该目录下 
    @Test
    public void copyFileFromLocalToHDFS()throws Exception{
    
    
        Path localPath=new Path("D:\\a.txt");
        Path hdfsPath=new Path("/hdfstest0410/test/0410.txt");
        fileSystem.copyFromLocalFile(localPath,hdfsPath);//从本地拷贝到hdfs

    }

此时就是把内容复制过去
在这里插入图片描述

    //下载文件从hdfs拷贝到本地
    @Test
    public void copyToLocalFile()throws Exception{
    
    
        Path localPath=new Path("D:\\b.txt");
        Path hdfsPath=new Path("/hdfstest0410/test/0410.txt");
        fileSystem.copyToLocalFile(false,hdfsPath,localPath,true);//从hdfs拷贝到本地
        //false表示不在原来的文件中(hdfs)删除内容,true是按照源文件方法将内容拷贝下来
        //是否删除dfs中的目录,是否用本地fs系统
    }

在这里插入图片描述

    @Test
    public void listFiles()throws Exception{
    
    
        FileStatus[] fileStatus = fileSystem.listStatus(new Path("/"));
        //输出
        for (FileStatus fileStatuse:fileStatus){
    
    
            String isdir=fileStatuse.isDirectory()?"文件夹":"文件";//判断是否是目录,是就执行文件夹,不是执行文件
            short replication=fileStatuse.getReplication();//文件的副本数
            long len=fileStatuse.getLen();//长度
            String path=fileStatuse.getPath().toString();//路径
            System.out.println(isdir+"\t"+replication+"\t"+len+"\t"+path);
        }
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43872169/article/details/104841892