HDFS文件创建与写入

实验环境
Linux Ubuntu 16.04
前提条件:
1)Java 运行环境部署完成
2)Hadoop 的单点部署完成
 

实验内容
在上述前提条件下,学习HDFS文件创建、写入、追加与合并等操作

实验步骤

1.点击桌面的"命令行终端",打开新的命令行窗口

2.启动HDFS

启动HDFS,在命令行窗口输入下面的命令:

/apps/hadoop/sbin/start-dfs.sh

运行后显示如下,根据日志显示,分别启动了NameNode、DataNode、Secondary NameNode:

dolphin@tools:~$ /apps/hadoop/sbin/start-dfs.sh 
Starting namenodes on [localhost]
localhost: Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Starting datanodes
Starting secondary namenodes [tools.hadoop.fs.init]
tools.hadoop.fs.init: Warning: Permanently added 'tools.hadoop.fs.init,172.22.0.2' (ECDSA) to the list of known hosts.

3.查看HDFS相关进程

在命令行窗口输入下面的命令:

jps

运行后显示如下,表明NameNode、DataNode、Secondary NameNode已经成功启动

dolphin@tools:~$ jps
484 DataNode
663 SecondaryNameNode
375 NameNode
861 Jps

4.准备要上传的文件

在命令行窗口输入下面的命令:

ls Desktop/logs/

运行后显示如下,在/home/dolphin/Desktop/logs目录下有一个log文件

dolphin@tools:~$ ls Desktop/logs/
access.1556972279.log

5.查看文件内容

在命令行窗口输入下面的命令:

cat Desktop/logs/access.1556972279.log

运行后显示如下:

dolphin@tools:~$ cat Desktop/logs/access.1556972279.log 
2400,22,vm-914,7,5,49.2,8.9,0.8,10.0,4.9,0.4,4.7,0.3,4.6,4.3,4.8,2.1,0.3,6.9,2019-09-20 23:50:00
7361,22,vm-914,17,0,58.3,1.4,9.4,1.4,10.6,1.7,5.0,9.4,8.2,10.2,5.4,0.2,8.7,3.8,2019-09-20 23:51:00
7363,22,vm-914,9,1,49.7,10.5,6.3,9.5,0.1,10.4,2.8,8.5,4.7,0.2,7.5,5.4,10.7,6.6,2019-09-20 23:52:00
7361,22,vm-914,1,2,41.0,5.4,8.5,8.2,3.2,3.5,0.8,2.5,9.0,4.5,9.6,6.6,2.9,7.2,2019-09-20 23:53:00
7361,22,vm-914,21,4,55.2,3.8,9.9,4.0,6.4,5.6,8.2,6.9,6.5,1.9,5.6,6.3,3.2,2.6,2019-09-20 23:54:00
7361,22,vm-914,4,0,57.5,7.6,1.9,2.3,10.7,9.6,1.2,6.7,10.4,5.9,7.2,2.8,10.7,4.7,2019-09-20 23:55:00
7363,22,vm-914,6,3,41.4,5.4,7.7,3.0,0.5,0.5,4.3,1.5,9.8,3.8,5.9,8.1,6.4,0.1,2019-09-20 23:56:00
7361,22,vm-914,20,0,55.9,6.8,6.7,4.6,10.0,10.3,9.2,0.6,3.3,0.9,9.5,4.1,7.8,7.4,2019-09-20 23:57:00
7361,22,vm-914,9,4,54.4,6.6,4.5,7.4,8.9,4.7,2.2,5.3,7.2,2.7,5.8,3.4,7.8,10.6,2019-09-20 23:58:00
7363,22,vm-914,10,2,44.3,3.6,3.7,0.6,9.6,4.7,4.6,7.0,5.9,6.4,5.0,7.4,7.7,3.7,2019-09-20 23:59:00

6.HDFS文件夹的创建

在命令行窗口输入下面的命令:

hadoop fs -mkdir /log

运行后显示如下,此时在HDFS的根目录下创建了log1和log2两个文件夹

dolphin@tools:~$ hadoop fs -mkdir /log

7.创建HDFS文件

在命令行窗口输入下面的命令:

hadoop fs -touchz /hello.txt

此时已经在HDFS的根目录下创建了touch.txt文件了
在命令行窗口输入下面的命令:

hadoop fs -ls /

运行后显示如下,可见此时根目录下已存在hello.txt文件

dolphin@tools:~$ hadoop fs -ls /
Found 2 items
-rw-r--r--   1 dolphin supergroup          0 2019-11-18 15:04 /hello.txt
drwxr-xr-x   - dolphin supergroup          0 2019-11-18 15:01 /log

8.追加文件

在命令行窗口输入下面的命令:

hadoop fs -appendToFile Desktop/logs/access.1556972279.log /hello.txt

此时,将本地的log文件内容,追加到了刚才创建的HDFS文件中,运行下面的命令,查看追加后的文件内容

hadoop fs -cat /hello.txt

运行后显示如下,表明文件追加成功

dolphin@tools:~$ hadoop fs -cat /hello.txt
2400,22,vm-914,7,5,49.2,8.9,0.8,10.0,4.9,0.4,4.7,0.3,4.6,4.3,4.8,2.1,0.3,6.9,2019-09-20 23:50:00
7361,22,vm-914,17,0,58.3,1.4,9.4,1.4,10.6,1.7,5.0,9.4,8.2,10.2,5.4,0.2,8.7,3.8,2019-09-20 23:51:00
7363,22,vm-914,9,1,49.7,10.5,6.3,9.5,0.1,10.4,2.8,8.5,4.7,0.2,7.5,5.4,10.7,6.6,2019-09-20 23:52:00
7361,22,vm-914,1,2,41.0,5.4,8.5,8.2,3.2,3.5,0.8,2.5,9.0,4.5,9.6,6.6,2.9,7.2,2019-09-20 23:53:00
7361,22,vm-914,21,4,55.2,3.8,9.9,4.0,6.4,5.6,8.2,6.9,6.5,1.9,5.6,6.3,3.2,2.6,2019-09-20 23:54:00
7361,22,vm-914,4,0,57.5,7.6,1.9,2.3,10.7,9.6,1.2,6.7,10.4,5.9,7.2,2.8,10.7,4.7,2019-09-20 23:55:00
7363,22,vm-914,6,3,41.4,5.4,7.7,3.0,0.5,0.5,4.3,1.5,9.8,3.8,5.9,8.1,6.4,0.1,2019-09-20 23:56:00
7361,22,vm-914,20,0,55.9,6.8,6.7,4.6,10.0,10.3,9.2,0.6,3.3,0.9,9.5,4.1,7.8,7.4,2019-09-20 23:57:00
7361,22,vm-914,9,4,54.4,6.6,4.5,7.4,8.9,4.7,2.2,5.3,7.2,2.7,5.8,3.4,7.8,10.6,2019-09-20 23:58:00
7363,22,vm-914,10,2,44.3,3.6,3.7,0.6,9.6,4.7,4.6,7.0,5.9,6.4,5.0,7.4,7.7,3.7,2019-09-20 23:59:00

9.上传文件到HDFS

在命令行窗口输入下面的命令:

hadoop fs -put ./test.txt /

在命令行窗口输入下面的命令,查看文件内容:

hadoop fs -cat /test.txt

运行后显示如下,上传成功

dolphin@tools:~$ hadoop fs -cat /test.txt
Tape Scripts Test One I Understanding Basic Skill Directions: Listen to the following passage carefully and fill in the blanks with words and phrases or sentences you hear.
 (30 points) All big cities are quite similar. 
Living in a modern Asian city is not very different from living in an American city.

10.合并HDFS文件,并下载到本地

现在HDFS的根目录下有test.txt和hello.txt文件,在命令行窗口输入下面的命令:

hadoop fs -getmerge / ./merge.txt

运行后,已经将HDFS上的test.txt和hello.txt文件文件进行了合并,并且下载在本地。

11.查看合并后的文件内容

在命令行窗口输入下面的命令:

cat ./merge.txt

运行后显示如下,可见之前两个HDFS文件的内容都在这个文件中

dolphin@tools:~$ cat ./merge.txt 
2400,22,vm-914,7,5,49.2,8.9,0.8,10.0,4.9,0.4,4.7,0.3,4.6,4.3,4.8,2.1,0.3,6.9,2019-09-20 23:50:00
7361,22,vm-914,17,0,58.3,1.4,9.4,1.4,10.6,1.7,5.0,9.4,8.2,10.2,5.4,0.2,8.7,3.8,2019-09-20 23:51:00
7363,22,vm-914,9,1,49.7,10.5,6.3,9.5,0.1,10.4,2.8,8.5,4.7,0.2,7.5,5.4,10.7,6.6,2019-09-20 23:52:00
7361,22,vm-914,1,2,41.0,5.4,8.5,8.2,3.2,3.5,0.8,2.5,9.0,4.5,9.6,6.6,2.9,7.2,2019-09-20 23:53:00
7361,22,vm-914,21,4,55.2,3.8,9.9,4.0,6.4,5.6,8.2,6.9,6.5,1.9,5.6,6.3,3.2,2.6,2019-09-20 23:54:00
7361,22,vm-914,4,0,57.5,7.6,1.9,2.3,10.7,9.6,1.2,6.7,10.4,5.9,7.2,2.8,10.7,4.7,2019-09-20 23:55:00
7363,22,vm-914,6,3,41.4,5.4,7.7,3.0,0.5,0.5,4.3,1.5,9.8,3.8,5.9,8.1,6.4,0.1,2019-09-20 23:56:00
7361,22,vm-914,20,0,55.9,6.8,6.7,4.6,10.0,10.3,9.2,0.6,3.3,0.9,9.5,4.1,7.8,7.4,2019-09-20 23:57:00
7361,22,vm-914,9,4,54.4,6.6,4.5,7.4,8.9,4.7,2.2,5.3,7.2,2.7,5.8,3.4,7.8,10.6,2019-09-20 23:58:00
7363,22,vm-914,10,2,44.3,3.6,3.7,0.6,9.6,4.7,4.6,7.0,5.9,6.4,5.0,7.4,7.7,3.7,2019-09-20 23:59:00
Tape Scripts Test One I Understanding Basic Skill Directions: Listen to the following passage carefully and fill in the blanks with words and phrases or sentences you hear.
 (30 points) All big cities are quite similar. 
Living in a modern Asian city is not very different from living in an American city.

至此,本实验结束啦,开始下一个实验吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值