首页 > 其他分享 >Hadoop学习之hdfs的操作

Hadoop学习之hdfs的操作

时间:2024-05-28 21:29:06浏览次数:8  
标签:hdfs import entries Hadoop 学习 fileSystem new Configuration FileSystem

Hadoop学习之hdfs的操作

1.将HDFS中的文件复制到本地

package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
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.io.IOException;

public class Demo02Download {
    FileSystem fileSystem;

    // 创建执行对象
    // @Before: 前置通知, 在方法执行之前执行
    @Before
    public void getFileSystem() throws IOException {
        Configuration entries = new Configuration();
        entries.set("fs.defaultFS", "hdfs://master:9000");
        fileSystem = FileSystem.get(entries);
    }

    // 实现文件复制到本地
    // @Test的作用,省略了public static void main(String[] args) {,表示测试类的方法
    @Test
    public void getData() throws IOException {
        String hdfsPath = "/NOTICE.txt";
        String localPath = "data/";
        // 将HDFS中的文件复制到本地
        fileSystem.copyToLocalFile(new Path(hdfsPath),new Path(localPath));
    }


    // @After: 后置通知, 在方法执行之后执行 。
    @After
    public void close() throws IOException {
        fileSystem.close();
    }

}

2.上传数据到HDFS中

package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Demo04PutData {
    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
        /*
            上传数据到HDFS中
         */
        putData();
        putData2();
    }

    public static void putData() throws IOException {
        // 没有设置用户信息
        Configuration entries = new Configuration();
        entries.set("fs.defaultFS","hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(entries);
        // 从本地上传文件到HDFS上
        fileSystem.copyFromLocalFile(new Path("hadoop/data/students.txt"),new Path("/data/"));
        fileSystem.close();
    }

    public static void putData2() throws IOException, URISyntaxException, InterruptedException {
        // 设置了用户信息
        Configuration entries = new Configuration();
        entries.set("fs.defaultFS","hdfs://master:9000");
        /*
            FileSystem get(final URI uri, final Configuration conf,final String user)
         */
        URI uri = new URI("hdfs://master:9000");
        // 获取FileSystem的实体类对象(传递uri到get函数中吗,会更改上传到HDFS中文件的用户信息)
        FileSystem fileSystem = FileSystem.get(uri,entries,"root");
        fileSystem.copyFromLocalFile(new Path("hadoop/data/students.txt"),new Path("/data/"));
        fileSystem.close();
    }
}

3.在HDFS上创建文件目录

package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Demo05MakeDir {
    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
        /*
            上传数据到HDFS中
         */
        mkdir();
    }


    public static void mkdir() throws IOException, URISyntaxException, InterruptedException {
        // 设置了用户信息
        Configuration entries = new Configuration();
        entries.set("fs.defaultFS","hdfs://master:9000");

        URI uri = new URI("hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(uri,entries,"root");

        fileSystem.mkdirs(new Path("/api"));
//        fileSystem.mkdirs(new Path("/api/1/2"));

        fileSystem.close();
    }
}

4.删除HDFS上的文件目录

package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Demo06Delete {
    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
        delete();
    }
    public static void delete() throws IOException, URISyntaxException, InterruptedException {
        // 设置了用户信息
        Configuration entries = new Configuration();
        entries.set("fs.defaultFS","hdfs://master:9000");

        URI uri = new URI("hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(uri,entries,"root");

//        fileSystem.delete(new Path("/api/1/2"));
        //TODO 参数recursive:如果path是一个目录并设置为true,则删除该目录,否则抛出异常。
        //               在文件的情况下,递归可以设置为true或false。
        fileSystem.delete(new Path("/api"),true);
        fileSystem.close();
    }
}

5.查看HDFS文件系统中文件和目录的元数据

package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Demo07Liststatus {
    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
        getBlockLocation();
    }

    public static void getBlockLocation() throws IOException, URISyntaxException, InterruptedException {

        Configuration entries = new Configuration();
        entries.set("fs.defaultFS","hdfs://master:9000");

        URI uri = new URI("hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(uri,entries,"root");

        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hadoop-3.1.3.tar.gz"));
        System.out.println("路径:"+fileStatus.getPath());
        System.out.println("长度:"+fileStatus.getLen());
        System.out.println("副本数:"+fileStatus.getReplication());
        /*
            获取一个文件的文件指定开始和结束的部分数据所在的Block块位置
            BlockLocation[] getFileBlockLocations(FileStatus file,
                    long start, long len)
         */
        BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
        for (BlockLocation fileBlockLocation : fileBlockLocations) {
            System.out.println("整个长度:"+fileBlockLocation.getLength());
            System.out.println("偏移量,从文件的什么位置开始:"+fileBlockLocation.getOffset());
            System.out.println("整个主机:"+fileBlockLocation.getHosts());
            System.out.println("整个名称:"+fileBlockLocation.getNames());
        }
        fileSystem.close();
    }


    public static void getFileStatus() throws IOException, URISyntaxException, InterruptedException {

        Configuration entries = new Configuration();
        entries.set("fs.defaultFS","hdfs://master:9000");

        URI uri = new URI("hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(uri,entries,"root");

        // getFileStatus()获取FileStatus对象
        // FileStatus对象封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及权限等信息。
        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hadoop-3.1.3.tar.gz"));
        System.out.println("路径:"+fileStatus.getPath());
        System.out.println("长度:"+fileStatus.getLen());
        System.out.println("副本数:"+fileStatus.getReplication());
        fileSystem.close();
    }



    public static void listStatus() throws IOException, URISyntaxException, InterruptedException {
        // 没有设置用户信息
        Configuration entries = new Configuration();
        entries.set("fs.defaultFS","hdfs://master:9000");

        URI uri = new URI("hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(uri,entries,"root");


        // listStatus()获取FileStatus对象数组,遍历根目录下的所有文件和目录的元数据
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            // 判断其是否为文件(检查这个抽象路径名表示的文件是否是普通文件),若为目录则输出其路径
            if (fileStatus.isFile()) {
                long blockSize = fileStatus.getBlockSize();
                System.out.println(fileStatus.getPath());
                System.out.println("Block块大小:"+blockSize);
                System.out.println("长度:"+fileStatus.getLen());
            }else {
                System.out.println(fileStatus.getPath());
            }
        }
        fileSystem.close();
    }
}

标签:hdfs,import,entries,Hadoop,学习,fileSystem,new,Configuration,FileSystem
From: https://blog.csdn.net/m0_58050808/article/details/139222311

相关文章

  • 《基于物理一致性的全息成像自监督学习》精读笔记
    基于物理一致性的全息成像自监督学习原文链接:https://www.nature.com/articles/s42256-023-00704-7三句话评价为计算成像与显微学中的逆问题的求解提供了新的方法;根据物理一致性(也即物理规律)构造自监督损失函数,实现模型的训练;在构造合理的情况下,所述方法可以实现基于超声波......
  • 深度学习概念
    一、前言在大四的时候,同专业的同学都开始找工作,听到他们说自己的工资的时候说实话挺羡慕的,可能我比较现实,我认为现在读书其实就是为了以后能够找个好工作,在我眼里,好工作就是工资高,上班累点也没关系。人生就是这样,面临多个选择的时候,无论选择哪条路以后都有可能会后悔。我时常在......
  • 盒模型,百分比设置元素的大小学习
    1.盒模型<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>盒模型/框模型</title>......
  • 深度学习-nlp-微调BERT--82
    目录importtorchimporttorch.nnasnnfromtorch.utils.dataimportTensorDataset,DataLoader,RandomSampler,SequentialSamplerfromsklearn.model_selectionimporttrain_test_splitfromtransformersimportBertTokenizer,BertConfigfromtransformersimpo......
  • 深度学习入门:3.手写数字分类
    这一章将基于Pytorch在只用全连接网络情况下实现手写数字识别,让大家基本了解怎么实现模型训练和预测使用。完整代码在最下面。一.MNIST数据集1.数据集介绍MNIST数据集包含60,000个训练样本和10,000个测试样本,每个样本都是28x28像素的灰度图像,表示一个0到9的手写......
  • Keras深度学习框架第三十一讲:KerasTuner定制搜索空间
    1、绪论在本文中我们将深入探讨如何在不直接修改HyperModel代码的情况下,定制KerasTuner的搜索空间。在深度学习的超参数优化过程中,搜索空间的定制是一个关键的步骤,因为它决定了Tuner将尝试哪些不同的配置组合。通过定制搜索空间,我们可以更有效地探索那些可能对模型性能产......
  • 深度学习——自己的训练集——测试模型(CNN)
    测试模型1.导入新图片名称2.加载新的图片3.加载图片4.使用模型进行预测5.获取最可能的类别6.显示图片和预测的标签名称7.图像加载失败输出导入新的图像,显示图像和预测的类别标签。1.导入新图片名称new_image_path='456.jpg'2.加载新的图片new_image=cv2.i......
  • DC-5靶场渗透实战过程(个人学习)
    环境安装DC-5下载地址:https://www.vulnhub.com/entry/dc-4,313/下载完成安装后,将kali和DC-5的靶场网络环境设置一致kali的IP:192.168.37.129一、信息收集寻找靶机的IP方法一:利用nmap命令查找靶机IP方法二:利用arp-scan-l查看局域网内其他IP方式查找靶机IP发现靶机的IP为......
  • 普通程序员深度学习教程(fastai及PyTorch)1深度学习快速入门-1简介
    1深度学习快速入门本章介绍深度学习背后的关键概念,并在不同的任务中训练我们的第一个模型。如果你不是技术或数学专业出身,也没有关系,我们从工程应用的角度入手,而不是数学科学。1.1深度学习没那么难多数深度学习不需要:高深的数据基础,实际高中数学已经够用大量数据:实际最低小......
  • 机器学习:逻辑回归
    目录一、引言1.1回归问题和分类问题的区别1.2 逻辑回归与线性回归的区别二、 逻辑回归算法原理2.1Sigmoid函数2.2 基于最优化方法的最佳系数确定三、LogisticRegression算法实现3.1导入库包3.2Sigmoid函数3.3 创建逻辑回归模型3.3.1初始化参数3.3.2正向......