头歌大数据技术第二关 大数据从入门到实战 - 第2章 分布式文件系统HDFS

第1关 HDFS的基本操作

pwd

回车

cd /

回车

mkdir /develop

回车

mkdir /develop/input

回车

mkdir /develop/output

回车

start-dfs.sh

回车

hadoop fs -mkdir /usr

回车

hadoop fs -mkdir /usr/input

回车

hadoop fs -mkdir /usr/output

回车

hadoop fs -ls /

回车

hadoop fs -ls /usr

回车

cd /develop/input

回车

touch helloworld.txt

回车

vim helloworld.txt

回车

hello hadoop

退出 :wq

hadoop fs -put helloworld.txt /usr/output

回车

hadoop fs -cat /usr/output/helloworld.txt

回车

hadoop fs -ls /user/hadoop

回车

hadoop fs -ls /

回车

hadoop fs -ls /usr

回车

hadoop fs -ls /user

回车

hadoop fs -mv /usr/output/helloworld.txt /

回车

hadoop fs -ls /

回车

hadoop fs -rm /helloworld.txt

回车

hadoop fs -mkdir /usr/output

回车

touch hello.txt

回车

vim hello.txt

回车

HDFS 的块比磁盘的块大,其目的是为了最小化寻址开销。

退出 wq

hadoop fs -put hello.txt /usr/output

回车

hadoop fs -ls /usr/output

回车

hadoop fs -rm -r /user/hadoop

回车

hadoop fs -get /usr/output/hello.txt /usr/local

回车

ls /usr/local

 

第2关 HDFS-JAVA接口之读取文件 

命令行:

start-dfs.sh

回车

touch test.txt

回车

vim test.txt

独坐池塘如虎踞,绿荫树下养精神。

春来我不先开口,哪个虫儿敢作声。

exit

: wq

代码文件:

package step2;

import java.io.IOException;

import java.io.InputStream;

import java.net.URI;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IOUtils;

 

public class FileSystemCat {

   

    public static void main(String[] args) throws IOException {

        //请在Begin-End之间添加你的代码,完成任务要求。

        //请按照左侧的编程要求进行编写代码

        //文件地址为 "hdfs://localhost:9000/user/hadoop/task.txt"

        /********* Begin *********/

    URI uri = URI.create("hdfs://localhost:9000/user/hadoop/task.txt");

    Configuration config = new Configuration();

    FileSystem fs = FileSystem.get(uri, config);

    InputStream in = null;

    try {

        in = fs.open(new Path(uri));

        IOUtils.copyBytes(in, System.out, 2048, false);

    } catch (Exception e) {

        IOUtils.closeStream(in);

    }

        /********* End *********/

    }

}

第3关 HDFS-JAVA接口之上传文件

命令行:

mkdir /develop

回车

mkdir /develop/input

回车

cd /develop/input

回车

touch hello.txt

回车

vim hello.txt

回车

迢迢牵牛星,皎皎河汉女。

纤纤擢素手,札札弄机杼。

终日不成章,泣涕零如雨。

河汉清且浅,相去复几许?

盈盈一水间,脉脉不得语。

《迢迢牵牛星》

exit

:wq  

start-dfs.sh

代码文件:

package step3;

 

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.net.URI;

import java.io.File;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IOUtils;

import org.apache.hadoop.util.Progressable;

 

public class FileSystemUpload {

   

    public static void main(String[] args) throws IOException {

        //请在 Begin-End 之间添加代码,完成任务要求。

        /********* Begin *********/

File localPath = new File("/develop/input/hello.txt");

 String hdfsPath = "hdfs://localhost:9000/user/tmp/hello.txt";

 InputStream in = new BufferedInputStream(new FileInputStream(localPath));// 获取输入流对象

 Configuration config = new Configuration();

 FileSystem fs = FileSystem.get(URI.create(hdfsPath),config);

 long fileSize = localPath.length() > 65536 ? localPath.length() / 65536 : 1;// 待上传文件大小

 FSDataOutputStream out = fs.create(new Path(hdfsPath), new Progressable() {

 //方法在每次上传了64KB字节大小的文件之后会自动调用一次

 long fileCount = 0;

 public void progress() {

 System.out.println("总进度"+ (fileCount / fileSize) * 100 + "%");

 fileCount++;

 }

 });

 IOUtils.copyBytes(in, out, 2048, true);//最后一个参数的意思是使用完之后是否关闭流

 }  

           

        /********* End *********/

    }

第4关 HDFS-JAVA接口之删除文件

命令行:

start-dfs.sh

代码文件 

package step4;

import java.io.IOException;

import java.net.URI;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileStatus;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.FileUtil;

import org.apache.hadoop.fs.Path;

public class FileSystemDelete {

    public static void main(String[] args) throws IOException {

        //请在 Begin-End 之间添加代码,完成本关任务。

        /********* Begin *********/

    String hadoop = "hdfs://localhost:9000/user/hadoop";

    String test = "hdfs://localhost:9000/tmp/test";

    String root = "hdfs://localhost:9000/";

    String tmp = "hdfs://localhost:9000/tmp";

    Configuration config = new Configuration();

    FileSystem fs = FileSystem.get(URI.create(root), config);

    fs.delete(new Path(hadoop), true);

    fs.delete(new Path(test), true);

    Path[] paths = {new Path(root), new Path(tmp)};

    FileStatus[] status = fs.listStatus(paths);

    Path[] listPaths = FileUtil.stat2Paths(status);

    for (Path path: listPaths) {

        System.out.println(path);

    }

        /********* End *********/

    }

}