【大数据】Hadoop实验报告

实验一 熟悉常用的Linux操作和Hadoop操作

1.实验目的

Hadoop运行在Linux系统上,因此,需要学习实践一些常用的Linux命令。本实验旨在熟悉常用的Linux操作和Hadoop操作,为顺利开展后续其他实验奠定基础。

2.实验平台
  • 操作系统:Linux;
  • Hadoop版本:2.7.1。
3.实验内容和要求

(一)熟悉常用的Linux操作
请按要求上机实践如下linux基本命令。

cd命令:切换目录

(1)切换到目录 /usr/local
在这里插入图片描述

(2)切换到当前目录的上一级目录
在这里插入图片描述

(3)切换到当前登录Linux系统的用户的自己的主文件夹
在这里插入图片描述

ls命令:查看文件与目录

(4)查看目录/usr下所有的文件
在这里插入图片描述

mkdir命令:新建新目录

(5)进入“/tmp”目录,创建一个名为“a”的目录,并查看“/tmp”目录下已经存在哪些目录
在这里插入图片描述

(6)进入“/tmp”目录,创建目录“a1/a2/a3/a4”
在这里插入图片描述

rmdir命令:删除空的目录
(7)将上面创建的目录a(在“/tmp”目录下面)删除
在这里插入图片描述

(8)删除上面创建的目录“a1/a2/a3/a4” (在“/tmp”目录下面),然后查看“/tmp”目录下面存在哪些目录
在这里插入图片描述

cp命令:复制文件或目录

(9)将当前用户的主文件夹下的文件.bashrc复制到目录“/usr”下,并重命名为bashrc1
在这里插入图片描述

(10)在目录“/tmp”下新建目录test,再把这个目录复制到“/usr”目录下
在这里插入图片描述

mv命令:移动文件与目录,或更名

(11)将“/usr”目录下的文件bashrc1移动到“/usr/test”目录下
在这里插入图片描述

(12)将“/usr”目录下的test目录重命名为test2
在这里插入图片描述

rm命令:移除文件或目录

(13)将“/usr/test2”目录下的bashrc1文件删除
$ sudo rm /usr/test2/bashrc1
在这里插入图片描述

(14)将“/usr”目录下的test2目录删除
$ sudo rm –r /usr/test2
在这里插入图片描述

cat命令:查看文件内容

(15)查看当前用户主文件夹下的.bashrc文件内容
在这里插入图片描述

tac命令:反向查看文件内容

(16)反向查看当前用户主文件夹下的.bashrc文件的内容
在这里插入图片描述

more命令:一页一页翻动查看

(17)翻页查看当前用户主文件夹下的.bashrc文件的内容
在这里插入图片描述

head命令:取出前面几行

(18)查看当前用户主文件夹下.bashrc文件内容前20行
在这里插入图片描述

(19)查看当前用户主文件夹下.bashrc文件内容,后面50行不显示,只显示前面几行
在这里插入图片描述

tail命令:取出后面几行

(20)查看当前用户主文件夹下.bashrc文件内容最后20行
在这里插入图片描述

(21) 查看当前用户主文件夹下.bashrc文件内容,并且只列出50行以后的数据
在这里插入图片描述

touch命令:修改文件时间或创建新文件

(22)在“/tmp”目录下创建一个空文件hello,并查看文件时间
在这里插入图片描述

(23)修改hello文件,将文件时间整为5天前

chown命令:修改文件所有者权限

在这里插入图片描述

(24)将hello文件所有者改为root帐号,并查看属性
在这里插入图片描述

find命令:文件查找

(25)找出主文件夹下文件名为.bashrc的文件
在这里插入图片描述

tar命令:压缩命令

(26)在根目录“/”下新建文件夹test,然后在根目录“/”下打包成test.tar.gz
在这里插入图片描述

(27)把上面的test.tar.gz压缩包,解压缩到“/tmp”目录
$ sudo tar -zxv -f /test.tar.gz -C /tmp
在这里插入图片描述

grep命令:查找字符串

(28)从“~/.bashrc”文件中查找字符串’examples’
在这里插入图片描述

(29)请在“~/.bashrc”中设置,配置Java环境变量
在这里插入图片描述

(30)查看JAVA_HOME变量的值
在这里插入图片描述

(二)熟悉常用的Hadoop操作
(31)使用hadoop用户登录Linux系统,启动Hadoop(Hadoop的安装目录为“/usr/local/hadoop”),为hadoop用户在HDFS中创建用户目录“/user/hadoop”
在这里插入图片描述

(32)接着在HDFS的目录“/user/hadoop”下,创建test文件夹,并查看文件列表
在这里插入图片描述

(33)将Linux系统本地的“~/.bashrc”文件上传到HDFS的test文件夹中,并查看test
在这里插入图片描述

(34)将HDFS文件夹test复制到Linux系统本地文件系统的“/usr/local/hadoop”目录下
在这里插入图片描述

实验二 熟悉常用的HDFS操作

1.实验目的
  • 理解HDFS在Hadoop体系结构中的角色;
  • 熟练使用HDFS操作常用的Shell命令;
2.实验平台
  • 操作系统:Linux(建议Ubuntu16.04);
  • Hadoop版本:2.7.1;
  • JDK版本:1.7或以上版本;
  • Java IDE:Eclipse。
3.实验步骤

(一)编程实现以下功能,并利用Hadoop提供的Shell命令完成相同任务:
(1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件;

Shell命令:
追加到末尾

hadoop fs -appendToFile /usr/local/hadoop/test.txt /user/text.txt

覆盖原文件

hadoop fs -copyFromLocal -f /usr/local/hadoop/test.txt /user/text.txt

Java代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*; 
public class HDFSApi {
    
      public static boolean test(Configuration conf, String path) throws IOException {
            FileSystem fs = FileSystem.get(conf);       
          return fs.exists(new Path(path)); 
    } 
        public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException {
    
        FileSystem fs = FileSystem.get(conf); 
        Path localPath = new Path(localFilePath); 
        Path remotePath = new Path(remoteFilePath);             fs.copyFromLocalFile(false, true, localPath, remotePath);      
   fs.close(); 
    } 
  public static void appendToFile(Configuration conf, String  localFilePath, String remoteFilePath) throws IOException {
    
        FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        FileInputStream in = new FileInputStream(localFilePath); 
        FSDataOutputStream out = fs.append(remotePath); 
   byte[] data = new byte[1024];      
   int read = -1;         
while ( (read = in.read(data)) > 0 ) {
             	out.write(data, 0, read); 
        }         
out.close();         in.close();         fs.close(); 
    } 
public static void main(String[] args) {
    
 	 	Configuration conf = new Configuration();     conf.set("fs.default.name","hdfs://localhost:9000"); 
 	 	String localFilePath = "/home/hadoop/text.txt";   
 	 	String remoteFilePath = "/user/hadoop/text.txt";    
 	 	String choice = "append";
	 	String choice = "overwrite";    	 
 	 	try {
     	 	 	 	 	
Boolean fileExists = false;  	 	 	
if (HDFSApi.test(conf, remoteFilePath)) {
     	 	 	 	
fileExists = true; 
 System.out.println(remoteFilePath + " 已存在.");  	
 	 } else {
    
 	 	 	 	System.out.println(remoteFilePath + " 不存在."); 
 	 	 	} 
 	 	 	if ( !fileExists) {
    // 文件不存在,则上传 
 	 	 	 	HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); 
 	 	 	 	System.out.println(localFilePath + " 已上传至 " + remoteFilePath); 
 	 	 	} else if ( choice.equals("overwrite") ) {
       // 选择覆盖 
 	 	 	 	HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); 
 	 	 	 	System.out.println(localFilePath + " 已覆盖 " + remoteFilePath); 
	 	} else if ( choice.equals("append") ) {
      // 选择追加 
 	 	 	 	HDFSApi.appendToFile(conf, localFilePath, remoteFilePath); 
 	 	 	 	System.out.println(localFilePath + " 已追加至 " + remoteFilePath); 
 	 	 	} 
 	 	} catch (Exception e) {
    
 	 	 	e.printStackTrace(); 
 	 	} 
 	} 
} 

(2)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;

Shell命令:

if $(hadoop fs -test -e /usr/local/hadoop/test.txt);
then $(hadoop fs -copyToLocal /user/test.txt /usr/local/hadoop/test.txt); 
else $(hadoop fs -copyToLocal /user/test.txt /usr/local/hadoop/test2.txt); 

Java代码:

Import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.*; 
import java.io.*; 
public class HDFSApi {
    
public 	static 	void 	copyToLocal(Configuration 	conf, 	String 	remoteFilePath, localFilePath) throws IOException {
    
      FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        File f = new File(localFilePath); 
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(); 
         break; 
        }
} 
System.out.println("将重新命名为: " + localFilePath); ()); 
 }        Path localPath = new Path(localFilePath);       
 fs.copyToLocalFile(remotePath, localPath);       
 fs.close(); 
    } 
 	public static void main(String[] args) {
    
 	 	Configuration conf = new Configuration();     conf.set("fs.default.name","hdfs://localhost:9000"); 
 	 String localFilePath = "/home/hadoop/text.txt";    
String remoteFilePath = "/user/hadoop/text.txt";   
try {
    
 	HDFSApi.copyToLocal(conf, remoteFilePath, localFilePath); 
 	System.out.println("下载完成"); 
} catch (Exception e) {
    
 	e.printStackTrace(); 
}
}
}

(3)将HDFS中指定文件的内容输出到终端中;

Shell命令:

hadoop fs -cat text.txt

Java代码:

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; 
 
public class HDFSApi {
    
    /** 
     * 读取文件内容 
     */ 
    public static void cat(Configuration conf, String remoteFilePath) throws IOException {
    
        FileSystem fs = FileSystem.get(conf); 
        Path remotePath = new Path(remoteFilePath); 
        FSDataInputStream in = fs.open(remotePath); 
        BufferedReader d = new BufferedReader(new InputStreamReader(in));         String line = null; 
        while ( (line = d.readLine()) != null ) {
    
         	System.out.println(line); 
        } 
       d.close();        in.close();        fs.close(); 
    } 
 	public static void main(String[] args) {
    
 	 	Configuration conf = new Configuration();     conf.set("fs.default.name","hdfs://localhost:9000");
String remoteFilePath = "/user/local/hadoop/text.txt";    // HDFS 路径
try {
    
 	System.out
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值