大数据实验作业(一)

177 阅读3分钟

Hdooooooooooooop!我要迪士尼!

1. 环境

操作系统:MacOS

Hadoop版本:3.3.6

Scala版本:2.11.8

JDK版本:1.8 (一定要是1.8!!!)

ide:IntelliJ IDEA

2. 程序任务

2.1 要求

在HDFS中,创建以自己名字及学号最后两位为命名的文件夹。

在刚才创建的目录中,利用给定jar包,将自己的学号、姓名(拼音)与jar包生成的多位随机生成的字符串写入到以自己姓名(拼音)为命名的文本文件中。

2.2 步骤

  1. 在HDFS中,创建以自己名字及学号最后两位为命名的文件夹

命令行中输入

hadoop fs -mkdir /姓名+学号后2位

之后可以在Web Gui中看到刚刚自己创建的文件夹

image.png

  1. 代码部分

(1)调用MyExport jar包生成字符串

private static String generateRandomStr() {
    MyExport me = new MyExport();
    me.SetSelfInfo(stuId, name);
    return me.output();
}

(2)创建以自己的名字命名的文本文件,并写入字符串

private static void writeIntoFile() throws IOException {
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create("hdfs://127.0.0.1:9000"), conf);
    Path file = new Path("/" + name + stuId + "/" + name);
    FSDataOutputStream outStream = fs.create(file);
    outStream.writeUTF(generateRandomStr());
    outStream.close();
}

(3)读取文件测试一下

private static void readFile() throws IOException {
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create("hdfs://127.0.0.1:9000"), conf);
    Path file = new Path("/" + name + stuId + "/" + name);
    FSDataInputStream inStream = fs.open(file);
    String data = inStream.readUTF();
    System.out.println(data);
}

(4)完整代码

public class Main {
    final static String stuId = "24";
    final static String name = "zhoumeitong";

    public static void main(String[] args) throws IOException {
        writeIntoFile();
        readFile();
    }

    /**
     * 写入内容
     */
    private static void writeIntoFile() throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create("hdfs://127.0.0.1:9000"), conf);
        Path file = new Path("/" + name + stuId + "/" + name);
        FSDataOutputStream outStream = fs.create(file);
        outStream.writeUTF(generateRandomStr());
        outStream.close();
    }

    /**
     * 读取内容
     */
    private static void readFile() throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create("hdfs://127.0.0.1:9000"), conf);
        Path file = new Path("/" + name + stuId + "/" + name);
        FSDataInputStream inStream = fs.open(file);
        String data = inStream.readUTF();
        System.out.println(data);
    }

    /**
     * 根据姓名和学号后两位生成随机字符串
     */
    private static String generateRandomStr() {
        MyExport me = new MyExport();
        me.SetSelfInfo(stuId, name);
        return me.output();
    }
}

2.3 可能遇到的坑

如果运行时抛出一堆Exception,绝对是没有导入相关的jar包

常见的:

image.png

最后还要导入 share/hadoop/client/ 里面的 hadoop-client-api-3.3.6.jar 和 hadoop-client-runtime-3.3.6.jar

3. 命令任务

3.1 要求

  1. 将自己刚才在编程题中的项目生成以自己名字为命名的jar包,并采用向hadoop集群提交jar包的方式,提交自己的jar包

  2. 用命令方式将HDFS中刚创建的文件内容显示出来

  3. 将HDFS中刚创建的文件内拷贝到本地用户目录中

  4. 将scala安装目录下的README文件追加到HDFS刚创建的文件中并显示文件内容

  5. 上传一份自己的简介到HADOOP集群的用户目录

  6. 将刚才在HDFS上所创建的目录删除

3.2 步骤

  1. 生成jar包(作业要求jar包是自己名字的拼音)

网上非常多的教程,直接使用你自己的ide去生成jar包即可。

这里附上一个IntelliJ IDEA的教程: blog.csdn.net/weixin_4367…

image.png

  1. 向hadoop集群提交jar包

输入如下命令

hdfs dfs -put /user/zhoumeitong24/zhoumeitong.jar(本地jar包路径) /zhoumeitong24(hadoop集群路径)

执行完成后去Web Gui查看,如果能看到我们刚刚提交的jar包就是成功了。

image.png

  1. 用命令方式将HDFS中刚创建的文件内容显示出来

hdfs dfs -cat /zhoumeitong24/zhoumeitong

  1. 将HDFS中刚创建的文件内拷贝到本地用户目录中

hadoop fs -get /zhoumeitong24/zhoumeitong(hdfs中的路径) /Users(本地路径)

  1. 将scala安装目录下的README文件追加到HDFS刚创建的文件中并显示文件内容

追加:

hdfs dfs -appendToFile /Users/zhoumeitong/scala-2.11.8/doc/README(本地路径) /zhoumeitong24/zhoumeitong(hdfs中的路径)

  1. 上传一份自己的简介到HADOOP集群的用户目录

先在本地写好一份自己的简介文件,然后:

hdfs dfs -put /user/zhoumeitong24/zhoumeitong.txt(本地文件路径) /user(hadoop集群路径)

  1. 删除刚才在HDFS所创建的目录

需要先删除目录中的文件:

hdfs dfs -rm /zhoumeitong24/zhoumeitong

hdfs dfs -rm /zhoumeitong24/zhoumeitong.jar

再删除目录:

hdfs dfs -rmdir /zhoumeitong24

4. 参考文章

[1] HDFS常用文件操作指令

[2]HDFS追加文件内容