【简单易懂版】使用IDEA操作Hadoop(增删改查)


前提:服务器中已经配置好了hadoop
本人亲测,以下代码已经跑通,基础功能都可以完成!!!希望对大家有用!!!在这里插入图片描述

一、引入hdfs依赖

  1. 创建一个maven项目cn.et
  2. 本地maven配置阿里镜像,用于快速下载依赖(重启加载)
  3. pow文件中引入hadoop依赖
		<!--	hdfs依赖	-->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>3.1.3</version>
		</dependency>

二、创建hdfs工具类

创建一个HdfsApiUtils 类,用于实现hdfs的增删改查:

  1. 获取hdfs的代码需要封装到静态代码块(先执行而且只执行一次)
  2. 创建文件或目录(mkdirs,建议写绝对路径hdfs://地址:9000/新目录
  3. 删除文件或目录(delete)
  4. 修改或移动文件或目录(rename)
  5. 查询当前路径下所有的文件或目录(显示时间和MB)
package com.example.springbootonline.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Print
 * @Date: 2023/07/17/10:07
 * @Description:idea集成hdfs;地址hdfs://地址:9000
 */

@Component
public class HdfsApiUtils {
    private static String hdfsUrl = "hdfs://地址:9000";


    private static String hdfsUsername = "root";

    private static FileSystem hdfs;

    static {
        Configuration conf = new Configuration();
        // 上传到云服务器需要配置下面这个句话
        conf.set("dfs.client.use.datanode.hostname","true");
        try {
            hdfs = FileSystem.get(URI.create(hdfsUrl), conf, hdfsUsername);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建文件或目录
     */
    public boolean mkdir(String path) {
        boolean res = false;
        try {
            hdfs.mkdirs(new Path(path));
            res = true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return res;
    }

    /**
     * 删除文件或目录
     */
    public boolean delete(String path) {
        boolean res = false;
        try {
            res = hdfs.delete(new Path(path), true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return res;
    }

    /**
     * 修改或移动文件或目录
     */
    public boolean rename(String oldFile, String newFlie) {
        boolean res = false;
        try {
            res = hdfs.rename(new Path(oldFile), new Path(newFlie));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return res;
    }

    /**
     * 查询当前路径下所有的文件或目录(只查当前目录下)
     */
    public FileStatus[] findCurrent(String path) {
        FileStatus[] res = null;
        try {
            res = hdfs.listStatus(new Path(path));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return res;
    }

    /**
     * 查询当前路径下所有的文件或目录(递归查下面所有)
     */
    public RemoteIterator<LocatedFileStatus> findAll(String path){
        RemoteIterator<LocatedFileStatus> iterator = null;
        try {
            iterator = hdfs.listFiles(new Path(path),true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return iterator;
    }

    /**
     * 上传
     */
    public boolean upload(String localPath, String path) {
        boolean res = false;
        try {
            hdfs.copyFromLocalFile(new Path(localPath), new Path(path));
            res = true;
        } catch (IOException e) {
            res = false;
            throw new RuntimeException(e);
        }
        return res;
    }

    /**
     * 下载
     */
    public boolean download(String hdfsPath, String localPath) {
        boolean res = false;
        try {
            hdfs.copyToLocalFile(new Path(hdfsPath), new Path(localPath));
            res = true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return res;
    }
}

三、测试hdfs工具类

import com.example.springbootonline.utils.HdfsApiUtils;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Print
 * @Date: 2023/07/17/10:59
 * @Description:
 */
@Component
public class HdfsApiUtilsTest {
    HdfsApiUtils hdfsApiUtils = new HdfsApiUtils();

    @Test
    public void mkdir(){
        String newFile = "/file";
        System.out.println(hdfsApiUtils.mkdir(newFile));
    }

    @Test
    public void delete(){
        String path = "/aaa";
        System.out.println(hdfsApiUtils.delete(path));
    }

    @Test
    public void rename(){
        String oldFile = "/aaa",newFile = "/newAAA";
        System.out.println(hdfsApiUtils.rename(oldFile,newFile));
    }

    @Test
    public void upload(){
        String localPath = "F:\\Users\\HP\\Videos\\Captures\\demo.mp4",path = "/abc/aaa";
        System.out.println(hdfsApiUtils.upload(localPath,path));
    }

    @Test
    public void findCurrent(){
        String path = "/file";
        FileStatus[] fss = hdfsApiUtils.findCurrent(path);
        for (FileStatus fs:fss) {
            System.out.println(fs.toString()+"\n");
        }
        System.out.println();
    }

    @Test
    public void findAll() throws IOException {
        String path = "/file";
        RemoteIterator<LocatedFileStatus> iterator = hdfsApiUtils.findAll(path);
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
    }
}

反思

好像应该再写一个服务器如何配置hadoop,后面再看有没有时间吧在这里插入图片描述

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
作为一种基于Hadoop的NoSQL数据库,Hbase的增删操作相对于传统的关系型数据库有所不同。在进行实验时,我发现以下几点心得: 1. 增加数据 Hbase的增加数据操作主要涉及到put命令。但需要注意的是,插入的每一行数据必须要有一个唯一的row key,而且这个key必须是字节类型的。因此,在插入数据时,需要将row key转化为字节数组。 2. 删除数据 Hbase的删除操作主要涉及到delete命令。需要注意的是,删除数据时不需要指定列族,只需要指定row key和列限定符即可。另外,使用delete命令只能删除单个cell,如果需要删除整行数据,需要使用deleteall命令。 3. 修数据 Hbase的修操作其实就是先删除原来的数据,再插入新的数据。因此,在修数据时需要先使用delete命令删除原来的数据,然后再使用put命令插入新的数据。需要注意的是,如果要修的数据不存在,那么使用delete命令删除数据时会报错,因此需要先判断数据是否存在。 4. 询数据 Hbase的操作主要涉及到get命令。需要注意的是,询数据时需要指定row key和列限定符。如果要询一整行数据,可以使用scan命令。另外,Hbase支持按照row key的范围进行询,这可以通过设置startrow和endrow参数来实现。 总的来说,Hbase的增删操作相对于传统的关系型数据库有所不同,需要注意的细节较多。但是,Hbase具有高可扩展性和高性能等优点,可以满足大规模数据存储和处理的需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PRINT!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值