HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo

简介: 26 集群使用初步HDFS的设计思路l 设计思想   分而治之:将大文件、大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析; l 在大数据系统中作用:为各类分布式运算框架(如:mapreduce,spark,tez,……)提供数据存储服务 l 重点概念:文件切块,副本存放,元数据26.1 HDFS使用1、查看集群状态命令:   h

26 集群使用初步

HDFS的设计思路

l 设计思想  

分而治之:将大文件、大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析;

 

l 在大数据系统中作用:

为各类分布式运算框架(如:mapreduce,spark,tez,……)提供数据存储服务

 

l 重点概念:文件切块,副本存放,元数据


26.1 HDFS使用

1、查看集群状态

命令:   hdfs  dfsadmin –report

可以看出,集群共有3个datanode可用

也可打开web控制台查看HDFS集群信息,在浏览器打开http://hadoop:50070/

2、上传文件到HDFS

查看HDFS中的目录信息

命令:hadoop  fs  –ls  /

 

上传文件

命令:hadoop fs -put ./findbugs-1.3.9 /

[toto@hadoop software]$ hadoop fs -put ./findbugs-1.3.9 /

put: `/findbugs-1.3.9/LICENSE-ASM.txt': File exists

put: `/findbugs-1.3.9/LICENSE-AppleJavaExtensions.txt': File exists

put: `/findbugs-1.3.9/LICENSE-bcel.txt': File exists

put: `/findbugs-1.3.9/LICENSE-commons-lang.txt': File exists

put: `/findbugs-1.3.9/LICENSE-docbook.txt': File exists

put: `/findbugs-1.3.9/LICENSE-dom4j.txt': File exists

put: `/findbugs-1.3.9/LICENSE-jFormatString.txt': File exists

查看上传后的信息列表(hadoop fs –ls /hadoop fs -ls /findbugs-1.3.9

 

从HDFS下载文件

命令:hadoop fs -get /findbugs-1.3.9/LICENSE-ASM.txt

[toto@hadoop learn]$ cd /home/toto/learn

/home/toto/learn

[toto@hadoop learn]$ pwd

/home/toto/learn

[toto@hadoop learn]$ hadoop fs -get /findbugs-1.3.9/LICENSE-ASM.txt

[toto@hadoop learn]$ ls

LICENSE-ASM.txt

 

yarn的管理界面是:http://hadoop:8088/cluster

 

26.2模拟运行一个mapreduce程序

模拟运行一个mapreduce程序的时候,需要先启动hdfs,启动命令是:

[toto@hadoop1 hadoop-2.8.0]$cd /home/toto/software/hadoop-2.8.0

[toto@hadoop1 hadoop-2.8.0]$sbin/start-dfs.sh

在/home/toto/software/hadoop-2.8.0/share/hadoop/mapreduce下有一个mapreduce的运行例子:

[toto@hadoop mapreduce]$ cd /home/toto/software/hadoop-2.8.0/share/hadoop/mapreduce

[toto@hadoop mapreduce]$ pwd

/home/toto/software/hadoop-2.8.0/share/hadoop/mapreduce

[toto@hadoop mapreduce]$ ll

总用量 5088

-rw-r--r--. 1 toto hadoop  562900 3月  17 13:31 hadoop-mapreduce-client-app-2.8.0.jar

-rw-r--r--. 1 toto hadoop  782739 3月  17 13:31 hadoop-mapreduce-client-common-2.8.0.jar

-rw-r--r--. 1 toto hadoop 1571179 3月  17 13:31 hadoop-mapreduce-client-core-2.8.0.jar

-rw-r--r--. 1 toto hadoop  195000 3月  17 13:31 hadoop-mapreduce-client-hs-2.8.0.jar

-rw-r--r--. 1 toto hadoop   31533 3月  17 13:31 hadoop-mapreduce-client-hs-plugins-2.8.0.jar

-rw-r--r--. 1 toto hadoop   66999 3月  17 13:31 hadoop-mapreduce-client-jobclient-2.8.0.jar

-rw-r--r--. 1 toto hadoop 1587158 3月  17 13:31 hadoop-mapreduce-client-jobclient-2.8.0-tests.jar

-rw-r--r--. 1 toto hadoop   75495 3月  17 13:31 hadoop-mapreduce-client-shuffle-2.8.0.jar

-rw-r--r--. 1 toto hadoop  301934 3月  17 13:31 hadoop-mapreduce-examples-2.8.0.jar

drwxr-xr-x. 2 toto hadoop    4096 3月  17 13:31 jdiff

drwxr-xr-x. 2 toto hadoop    4096 3月  17 13:31 lib

drwxr-xr-x. 2 toto hadoop    4096 3月  17 13:31 lib-examples

drwxr-xr-x. 2 toto hadoop    4096 3月  17 13:31 sources

[toto@hadoop mapreduce]$

 

使用命令运行mapreduce命令:

[toto@hadoop mapreduce]$ hadoop jar hadoop-mapreduce-examples-2.8.0.jar pi 5 5

Number of Maps  = 5

Samples per Map = 5

Wrote input for Map #0

Wrote input for Map #1

Wrote input for Map #2

Wrote input for Map #3

Wrote input for Map #4

Starting Job

17/05/29 14:47:36 INFO client.RMProxy: Connecting to ResourceManager at hadoop/192.168.106.80:8032

17/05/29 14:47:37 INFO input.FileInputFormat: Total input files to process : 5

17/05/29 14:47:37 INFO mapreduce.JobSubmitter: number of splits:5

17/05/29 14:47:38 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1495998405307_0001

17/05/29 14:47:39 INFO impl.YarnClientImpl: Submitted application application_1495998405307_0001

17/05/29 14:47:39 INFO mapreduce.Job: The url to track the job: http://hadoop:8088/proxy/application_1495998405307_0001/

17/05/29 14:47:39 INFO mapreduce.Job: Running job: job_1495998405307_0001

17/05/29 14:48:00 INFO mapreduce.Job: Job job_1495998405307_0001 running in uber mode : false

17/05/29 14:48:00 INFO mapreduce.Job:  map 0% reduce 0%

进入hdfs的管理界面(http://hadoop:8088/cluster/apps),查看程序运行情况:

 



26.2 MAPREDUCE使用

mapreduce是hadoop中的分布式运算编程框架,只要按照其编程规范,只需要编写少量的业务逻辑代码即可实现一个强大的海量数据并发处理程序

 

26.2.1 Demo开发——wordcount

1、需求

从大量(比如T级别)文本文件中,统计出每一个单词出现的总次数

 

2、mapreduce实现思路

Map阶段:

a)        从HDFS的源数据文件中逐行读取数据

b)        将每一行数据切分出单词

c)        为每一个单词构造一个键值对(单词,1)

d)        将键值对发送给reduce

 

Reduce阶段:

a)        接收map阶段输出的单词键值对

b)        将相同单词的键值对汇聚成一组

c)        对每一组,遍历组中的所有“值”,累加求和,即得到每一个单词的总次数

d)        将(单词,总次数)输出到HDFS的文件中

 

 

1、  具体编码实现

(1)定义一个mapper类

//首先要定义四个泛型的类型

//keyin:  LongWritable    valuein: Text

//keyout: Text            valueout:IntWritable

 

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

         //map方法的生命周期:  框架每传一行数据就被调用一次

         //key :  这一行的起始点在文件中的偏移量

         //value: 这一行的内容

         @Override

         protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

                   //拿到一行数据转换为string

                   String line = value.toString();

                   //将这一行切分出各个单词

                   String[] words = line.split(" ");

                   //遍历数组,输出<单词,1>

                   for(String word:words){

                            context.write(new Text(word), new IntWritable(1));

                   }

         }

}

 

(2)定义一个reducer类

         //生命周期:框架每传递进来一个kv 组,reduce方法被调用一次

         @Override

         protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

                   //定义一个计数器

                   int count = 0;

                   //遍历这一组kv的所有v,累加到count中

                   for(IntWritable value:values){

                            count += value.get();

                   }

                   context.write(key, new IntWritable(count));

         }

}

 

(3)定义一个主类,用来描述job并提交job

public class WordCountRunner {

         //把业务逻辑相关的信息(哪个是mapper,哪个是reducer,要处理的数据在哪里,输出的结果放哪里。。。。。。)描述成一个job对象

         //把这个描述好的job提交给集群去运行

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

                   Configuration conf = new Configuration();

                   Job wcjob = Job.getInstance(conf);

                   //指定我这个job所在的jar包

//               wcjob.setJar("/home/hadoop/wordcount.jar");

                   wcjob.setJarByClass(WordCountRunner.class);

                  

                   wcjob.setMapperClass(WordCountMapper.class);

                   wcjob.setReducerClass(WordCountReducer.class);

                   //设置我们的业务逻辑Mapper类的输出key和value的数据类型

                   wcjob.setMapOutputKeyClass(Text.class);

                   wcjob.setMapOutputValueClass(IntWritable.class);

                   //设置我们的业务逻辑Reducer类的输出key和value的数据类型

                   wcjob.setOutputKeyClass(Text.class);

                   wcjob.setOutputValueClass(IntWritable.class);

                  

                   //指定要处理的数据所在的位置

                   FileInputFormat.setInputPaths(wcjob, "hdfs://hdp-server01:9000/wordcount/data/big.txt");

                   //指定处理完成之后的结果所保存的位置

                   FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://hdp-server01:9000/wordcount/output/"));

                  

                   //向yarn集群提交这个job

                   boolean res = wcjob.waitForCompletion(true);

                   System.exit(res?0:1);

         }

 

26.2.2 程序打包运行

1.        将程序打包

2.        准备输入数据

vi  /home/hadoop/test.txt

Hello tom

Hello jim

Hello ketty

Hello world

Ketty tom

在hdfs上创建输入数据文件夹:

hadoop   fs mkdir  -p  /wordcount/input

将words.txt上传到hdfs上

         hadoop  fs  –put  /home/hadoop/words.txt  /wordcount/input

 

3.        将程序jar包上传到集群的任意一台服务器上

 

4.        使用命令启动执行wordcount程序jar包

$ hadoop jar wordcount.jar cn.toto.bigdata.mrsimple.WordCountDriver/wordcount/input /wordcount/out

5.        查看执行结果

$ hadoop fs –cat /wordcount/out/part-r-00000

 

目录
相关文章
|
6月前
|
弹性计算 监控 网络安全
如何轻松使用AWS Web应用程序防火墙?
AWS WAF是Web应用防火墙,可防护常见网络攻击。通过创建Web ACL并设置规则,保护CloudFront、API网关、负载均衡器等资源。支持自定义规则与OWASP预定义规则集,结合CloudWatch实现监控日志,提升应用安全性和稳定性。
|
安全 JavaScript 前端开发
AppSpider 7.5.020 发布 - Web 应用程序安全测试
AppSpider 7.5.020 for Windows - Web 应用程序安全测试
162 0
|
6月前
|
存储 安全 Java
如何在 Spring Web 应用程序中使用 @SessionScope 和 @RequestScope
Spring框架中的`@SessionScope`和`@RequestScope`注解用于管理Web应用中的状态。`@SessionScope`绑定HTTP会话生命周期,适用于用户特定数据,如购物车;`@RequestScope`限定于单个请求,适合无状态、线程安全的操作,如日志记录。合理选择作用域能提升应用性能与可维护性。
274 1
|
安全 测试技术 数据库
维护的Web应用程序
【10月更文挑战第4天】维护的Web应用程序
229 4
|
11月前
|
安全 测试技术 Linux
Acunetix v25.4 发布 - Web 应用程序安全测试
Acunetix v25.4 (Linux, Windows) - Web 应用程序安全测试
334 3
Acunetix v25.4 发布 - Web 应用程序安全测试
|
10月前
|
安全 Devops 测试技术
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
205 0
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
|
安全 JavaScript Java
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
221 12
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
|
开发框架 搜索推荐 数据可视化
Django框架适合开发哪种类型的Web应用程序?
Django 框架凭借其强大的功能、稳定性和可扩展性,几乎可以适应各种类型的 Web 应用程序开发需求。无论是简单的网站还是复杂的企业级系统,Django 都能提供可靠的支持,帮助开发者快速构建高质量的应用。同时,其活跃的社区和丰富的资源也为开发者在项目实施过程中提供了有力的保障。
725 67
|
自然语言处理 安全 测试技术
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
793 0
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
|
监控 前端开发 JavaScript
使用 MERN 堆栈构建可扩展 Web 应用程序的最佳实践
使用 MERN 堆栈构建可扩展 Web 应用程序的最佳实践
293 6

热门文章

最新文章