HDFS编程实践-从HDFS中下载指定文件到本地

news/2024/5/20 4:29:12 标签: hdfs, hadoop, 大数据

前言:Hadoop采用java语言开发,提供了Java Api与HDFS进行交互

先要把hadoop的jar包导入到idea中去

为了能编写一个与hdfs交互的java应用程序,一般需要向java工程中添加以下jar包

1)/usr/local/hadoop/share/hadoop/common目录下的所有jar包

2)/usr/local/hadoop/share/hadoop/common/lib下的所有jar包

3)/usr/local/hadoop/share/hadoop/hdfs目录下的所有jar包

4)/usr/local/hadoop/share/hadoop/hdfs/lib中的所有jar包

1、先从本地上传个文件到HDFS中去

命令

hdfs dfs -cp -f file:///usr/local/hadoop/a b

2、在idea中创建项目

HDFSAPI.java


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

import java.io.File;
import java.io.IOException;

//从HDFS中下载指定文件,如果本地文件与要下在文件名相同,则自动对下载的文件重命名
public class HDFSAPI {
    public static void copyToLocal(Configuration conf , String remoteFilePath, String localFilePath)throws IOException{
        FileSystem fs = FileSystem.get(conf);
        Path remotePath = new Path(remoteFilePath);
        File f = new File(localFilePath);
        //如果文件名存在,自动重命名(在文件后面加上_0,_1
        if (f.exists()){
            System.out.println(localFilePath+"已存在!");
            Integer i = 0;
            while (true){
                f=new File(localFilePath+"_"+i.toString());
                if (!f.exists()){
                    localFilePath=localFilePath+"_"+i.toString();
                }
                i++;
                System.out.println("将文件重命名"+localFilePath);

                break;
            }


        }


        //下载到本地
        Path localPath=new Path(localFilePath);
        fs.copyToLocalFile(remotePath,localPath);
        fs.close();
    }


}

Main.java

import org.apache.hadoop.conf.Configuration;

public class Main{
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.set("fs.default.name","hdfs://localhost:9000");
        //本地路径
        String localFilePath="/home/hadoop/text.txt";
        //hdfs路径
        String remoteFilePath="/user/hadoop/b";
        try {
            HDFSAPI.copyToLocal(configuration,remoteFilePath,localFilePath);
            System.out.println("下载完成!");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

3、将该项目打包成jar包

File->Project Structure

打包的文件在idea当前项目的out文件夹里面

4、将打包好的jar包移动到hadoop的安装目录下

我这里在hadoop的安装目录下新建了个myapp的文件夹

5、运行

./bin/hadoop jar ./myapp/HDFS_API.jar


http://www.niftyadmin.cn/n/5053435.html

相关文章

cmdb运维管理平台,IT资源配置管理

CMDB运维管理平台是IT资产管理的工具,可以帮助企业实现IT资产的综合管理和监督。CMDB运维管理平台的主要功能包括:配置管理、变更管理、问题管理、事件管理等。  配置管理是CMDB运维管理平台的主要功能之一,可以帮助企业实现IT资源的综合管…

唤醒手腕 2023年 B 站课程 Golang 语言详细教程笔记(更新中)

0001、1000集GO语言Flag毒誓 唤醒手腕UP猪Pig目标花费1000集进行讲解Go语言视频学习教程(有趣的灵魂,适合小白,不适合巨佬),从2023年3月19日开始,将会一直每天更新,准备在2024年5月1日之前更新…

使用 frp 进行内网穿透

使用 frp 进行内网穿透 1.frp 概念2.服务端搭建3.客户端搭建1.frp 概念 frp 主要由 客户端(frpc) 和 服务端(frps) 组成,服务端通常部署在具有公网 IP 的机器上,客户端通常部署在需要穿透的内网服务所在的机器上。 内网服务由于没有公网 IP,不能被非局域网内的其他用户访问…

按键检测|中断检测

一.按键检测 1.硬件原理 当未按下按键时,GPIO_5为低电平,按下按键GPIO_5变为高电平。 根据引脚编号找到引脚名称 根据引脚名称找到引脚编号 裸机程序控制外设 特点:读数据手册、设寄存器值 找出外设有哪些相关寄存器找出外设相关寄存器如何…

Android ConstraintLayout app:layout_constraintHorizontal_weight

Android ConstraintLayout app:layout_constraintHorizontal_weight <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:…

如何使用Langchain-ChatGLM快速搭建个人知识库

引言 随着人工智能技术的不断发展&#xff0c;语言模型在各个领域中的应用越来越广泛。其中&#xff0c;Langchain-ChatGLM是一款使用了GPT-2语言模型的聊天机器人&#xff0c;它可以帮助用户快速搭建个人知识库&#xff0c;实现自动化问答和知识管理。下面&#xff0c;我们将…

PhpExcel导出Excel表格中数字类型自动转文本类型的解决办法

使用setCellValueExplicit属性 将->setCellValue改为->setCellValueExplicit即可 ->setCellValue(C4, 位置) ->setCellValue(D4, 名称) ->setCellValueExplicit(E4, date("Y-m"), PHPExcel_Cell_DataType::TYPE_STRING) ->setCellValueExplicit(…

linux脚本笔记

目录 1.增加环境变量 2.自定义命令快捷键 3.关闭selinux和防火墙 4.增加别名快捷键 5.Linux链接 1.增加环境变量 新建add_env.sh #!/bin/bashapp_dir"/root/docker"# 检查配置文件中是否已存在相同的环境变量 if grep -q -E "^export APP_HOME.*" ~…