尝试通过java代码来控制hdfs的文件创建删除上传下载等操作

第一步(总重要的一环节如果你只是想看相关方法直接跳掉最后)

保证自己提前有一个已经装好hadoop环境的linux虚拟机,并且该虚拟机需要和本机进行通信一般就是本机通过cmd命令ping 你的虚拟机ip,然后是设置linux防火墙(这一步主要是针对hadoop需要使用好几个端口,而这些端口需要被外界ip访问到,保证java代码正常对hdfs的文件操作)下面是对防火墙设置的一些整理

查询指定端口是否已经开放
firewall-cmd --query-port=9000/tcp
返回yes/no。此时也有可能返回firewalld is not running,此时需要打开防火墙在开放端口。
开放端口号命令:
添加指定需要开放的端口:
firewall-cmd --add-port=9000/tcp --permanent
重载入添加的端口:
firewall-cmd --reload

搭建一个maven项目引入关于hdfs操做的依赖

<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.7.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.9.2</version>
    </dependency>

将你linux系统中关于hadoop的配置文件core-site.xml和hdfs-site.xml复制到到你目录的resurces资源文件夹中

在这里插入地方图片描述
这个文件在linux你安装hadoop的目录
我的是 /root/opt/hadoop-2.9.2/etc/hadoop
将core-site.xml和hdfs-site.xml复制到资源文件里
注意:core-site.xml里你写的是linux的ip地址还是别名
如果是像我这种给ip起别名的需要在本地计算机找到你的hosts文件
追加内容 192.168.0.103 bigdata 这个是按照我的ip写的,你自己的需要写你的运行hadoop的linux的ip还有你自己编的别名

在这里插入图片描述

启动hadoop集群(不启动集群你的java对hdfs的操作是搞不了的)

进入hadoop下的sbin目录下启动 start-all.sh
查看hadoop是否启动成功在linux命令中输入 jps
显示如下内容:
在这里插入图片描述

访问你的分布式文件系统hdfs的根目录

hadoop fs -ls /

在这里插入图片描述

开始编造java代码来控制hdfs

这个代码只是对大概方法的实验,粘贴后需要改动路径(这些的都是我的路径)你可以先做一个查询hdfs目录功能,然后依次进行对文件或者文件夹的增删查操作,建议手敲一遍功能点收获多多.

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;


import java.io.*;

public class MyHadoop {
    static Configuration configuration = new Configuration();//创建hdfs的配置信息对象,该对象会自动搜索包路径下的hadoop配置文件
    static FileSystem fileSystem =null; //基于Configuration的信息创建FileSystem对象 用于对hdfs文件的创建删除等操作
    public static void main(String[] args) throws IOException {
        Path path = new Path("/test");
        Path src = new Path("C:\\Users\\xxx\\Desktop\\日程.txt");
        Path dfs = new Path("/2.txt");
        Path old = new Path("/2.txt");
        Path ne = new Path("/xxxx");
        Path dfsFile = new Path("/日程.txt");
        listfile(path);
        //createFile(path);
        //delectFile(path);
        //upload(src,dfs);
        //mv(old,ne);
        //downlocal(dfsFile,"C:\\Users\\xxx\\Desktop\\从hdfs下载下来的文件.txt");
        //appendTOFile(new Path("/日程.txt"),"我追加一句话");
        //readFile(new Path("/日程.txt"));
    }

    /**
     * 根据输入路径遍历hdfs目录
     * @param path
     */
    public static void listfile(Path path){
        FileStatus[] fileStatuses = new FileStatus[0];
        try {
            fileSystem=FileSystem.get(configuration);
            fileStatuses = fileSystem.listStatus(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for(FileStatus f:fileStatuses){
            System.out.println(f.getPath());
        }
    }

    /**
     * 在hdfs创建目录
     * @param path
     * @throws IOException
     */
    public static void createFile(Path path) throws IOException {
        fileSystem=FileSystem.get(configuration);
        fileSystem.mkdirs(path);
    }

    /**
     * 删除文件或者目录
     * @param path
     * @throws IOException
     */
    public static void delectFile(Path path) throws IOException {
        fileSystem=FileSystem.get(configuration);
        fileSystem.delete(path,true);
    }

    /**
     * 上传本地文件到hdfs
     * @param src
     * @param dfs
     * @throws IOException
     */
    public static void upload(Path src,Path dfs) throws IOException {
        fileSystem=FileSystem.get(configuration);
        fileSystem.copyFromLocalFile(src,dfs);
        fileSystem.close();
    }

    /**
     * 在hdfs中移动文件或者文件夹
     * @param old
     * @param ne
     * @throws IOException
     */
    public static void mv(Path old,Path ne) throws IOException {
        fileSystem=FileSystem.get(configuration);
        fileSystem.rename(old,ne);
        fileSystem.close();
    }

    /**
     * 将hdfs中的文件下载到本地
     * @param dfs
     * @param local
     * @throws IOException
     */
    public static void downlocal(Path dfs,String local) throws IOException {
        fileSystem = FileSystem.get(configuration);
        InputStream inputStream = fileSystem.open(dfs);
        OutputStream outputStream = new FileOutputStream(new File(local));
        int data=0;
        while((data=inputStream.read())!=-1){
            outputStream.write(data);
        }
        outputStream.close();
        inputStream.close();
    }

    /**
     * 给hdfs文件追加内容
     * @param path
     * @param text
     * @throws IOException
     */
    public static void appendTOFile(Path path,String text) throws IOException {
        fileSystem =FileSystem.get(configuration);
        OutputStream outputStream = fileSystem.append(path);
        outputStream.write(text.getBytes("UTF-8"));
        outputStream.close();
    }

    /**
     * 读取hdfs中的文件内容
     * @param path
     * @throws IOException
     */
    public static void readFile(Path path) throws IOException {
        fileSystem = FileSystem.get(configuration);
        InputStream inputStream = fileSystem.open(path);
        Reader reader = new InputStreamReader(inputStream);
        int data =0;
        while((data=reader.read())!=-1){
            System.out.print((char)data);
        }
        inputStream.close();
    }

}

本人第一次写博客,如果那里写的不对和遗漏请大家联系我改正

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值