读写 HDFS 文件

在hadoop hdfs上传下载文件

什么是hdfs

HDFS(Hadoop Distributed File System)为大数据平台其它所有组件提供了基本的存 储功能。它具有高容错、高可靠、可扩展、高吞吐率等特征,为大数据存储和处理提供 了强大的底层存储架构。 HDFS 是一个主/从(master/slave)体系结构,从最终用户的角度来看,它就像传统 的文件系统,可通过目录路径对文件执行 CRUD 操作。由于其分布式存储的性质,HDFS 集群拥有一个 NameNode 和一些 DataNodes,NameNode 管理文件系统的元数据,DataNode 存储实际的数据。 HDFS 开放文件系统的命名空间以便用户以文件形式存储数据,秉承“一次写入、 多次读取”的原则。客户端通过 NameNode 和 DataNodes 的交互访问文件系统,联系 NameNode 以获取文件的元数据,而真正的文件 I/O 操作是直接和 DataNode 进行交互的。

本试验所用的所有 jar 包都在集群的/home/hadoop/lib.zip 中,大家可以自行下载.
在这里插入图片描述
使用 idea 创建一个项目,创建一个包为 hdfs,里面一个类 Operator,引用需要的 Jar 包(jar 包在集群的/home/hadoop/lib.zip 中可以自行下载并解压,引用到项目中), 编写一个可以交互的 HDFS 文件操作的程序,Operator 类的完整代码如下:

package hdfs;  
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

 public class Operator {

/***
 * 读取 HDFS 文件到控制台
 *@param url
 *@throws IOException
 */
public static void readHadoopFile(String url) throws IOException {
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(url), conf);
    InputStream in = null;
    try {
        in = fs.open(new Path(url));
        IOUtils.copyBytes(in, System.out, 4096, false);
    } finally {
        IOUtils.closeStream(in);
    }
    in.close();
    fs.close();
}

/***
 * 读取文件到字符串中
 *@param url
 *@return
 *@throws IOException
 */

public static String readHadoopFileToString(String url) throws IOException {
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(url), conf);
    InputStream in = null;
    StringBuilder sb = new StringBuilder();
    ByteArrayOutputStream outByte = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(outByte);
    try {
        in = fs.open(new Path(url));
        IOUtils.copyBytes(in, out, 4096, false);
    } finally {
        IOUtils.closeStream(in);
    }
    sb.append(outByte.toString("UTF-8"));
    in.close();
    out.close();
    fs.close();
    outByte.close();
    return sb.toString();
}

/***
 * 上传 HDFS 文件
 大数据实验手册 Hadoop
 12
 *@param source
 *@param target
 *@throws IOException
 */
public static void writeHadoopFile(String source, String target) throws IOException {
    InputStream in = new BufferedInputStream(new FileInputStream(source));
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(target), conf);
    OutputStream out = null;
    Path path = new Path(target);
    if (fs.exists(path)) {
        out = fs.append(path);
    } else {
        out = fs.create(new Path(target));
    }
    IOUtils.copyBytes(in, out, 4096, true);
    in.close();
    out.close();
    fs.close();
}


/***
 * 获取文件列表
 *@param url
 *@throws IOException
 */
public static void getFileList(String url) throws IOException {
    Configuration conf = new Configuration();
    Path path = new Path(url);
    FileSystem fs = FileSystem.get(URI.create(url), conf);
    FileStatus[] filestatus = fs.listStatus(path);
    for (FileStatus f : filestatus) {
        System.out.println(f.getPath().toString());
    }
    fs.close();
}


/***
 * 删除 HDFS 文件
 *@param url
 *@throws IOException
 */
public static void deleteFile(String url) throws IOException {
    Configuration conf = new Configuration();
    Path path = new Path(url);
    FileSystem fs = FileSystem.get(URI.create(url), conf);
    fs.delete(path, true);
    fs.close();
}


/***
 * 下载 HDFS 文件到本地系统
 *@param localFileName
 *@param hadoopFilePath
 *@throws FileNotFoundException
 *@throws IOException
 */
public static void downFromCloud(String localFileName, String hadoopFilePath)
        throws FileNotFoundException, IOException {
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(hadoopFilePath), conf);
    FSDataInputStream hdfs_in = fs.open(new Path(hadoopFilePath));
    OutputStream outToLOCAL = new FileOutputStream(localFileName);
    IOUtils.copyBytes(hdfs_in, outToLOCAL, 1024, true);
    fs.close();
    hdfs_in.close();
    outToLOCAL.close();
}


public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.err.println("参数错误,参数使用方法:");
        System.err.println("读取 HDFS 文件:readsource");
        System.err.println("上传 HDFS 文件:uploadsourcetarget");
        System.err.println("获取 HDFS 列表:listsource");
        System.err.println("删除 HDFS 文件:delsource");
        System.err.println(" 下 载 HDFS 文 件 : download localFileName hadoopFilePath");
        System.exit(2);
    }
    try {
        switch (args[0].toLowerCase().trim()) {
            case "read":
                readHadoopFile(args[1]);
                break;
            case "upload":
                writeHadoopFile(args[1], args[2]);
                break;
            case "list":
                getFileList(args[1]);
                break;
            case "del":
                deleteFile(args[1]);
                break;
            case "download":
                downFromCloud(args[1], args[2]);
                break;
            default:
                System.err.println("参数错误,参数使用方法:");
                System.err.println("读取 HDFS 文件:read source");
                System.err.println("上传 HDFS 文件:upload source target");
                System.err.println("获取 HDFS 列表:list source");
                System.err.println("删除 HDFS 文件:del source");
                System.err.println(" 下 载 HDFS 文 件 : download localFileName hadoopFilePath");
                System.exit(2);
                break;
        }
    } catch (Exception ex) {
        System.err.println(ex.toString());
    }
}
    }

导出hdfs.jar

1.file->Project Structure->Atifacts
在这里插入图片描述
2.点击 + 号
在这里插入图片描述
3. 创建一个jar的包点击ok
在这里插入图片描述
勾线lnude in porrojecyt build_>APPly ->ok在这里插入图片描述
5.找到这个包后,放置桌面用xftp6放入Ubuntu18.04中
在这里插入图片描述

登录Ubuntu18.04 启动hadoop (start-all.sh)

输入:

   cd /usr/local/hadoop
   hadoop jar hdfs.jar 

会出现:在这里插入图片描述
这里就说明你成功了 启动成功 hafs.jar 没有问题 这个也是你要执行的命令。

这里就在windows上创建一个a.txt 用xftp6放到/home/hadoop/文件下

把a.txt 上传到hdfs中

hadoop jar hdfs.jar upload /home/hadoop/a.txt /b.txt

查询是否上传

hadoop  jar hdfs.jar list /

在这里插入图片描述
删除文件

hadoop jar hdfs.jar del /b.txt

然后就在查询一遍就没了
hdfs 也就成功了
希望这个小案例如果能帮到你,加油!

发布了16 篇原创文章 · 获赞 0 · 访问量 638

猜你喜欢

转载自blog.csdn.net/qq_43388040/article/details/103215045