MapReduce编程

article/2025/10/16 18:01:45

一、MapReduce编程规范

MapReduce的开发一共又八个步骤,其中Map阶段分为2个步骤,Shuffle阶段4个步骤,Reduce阶段分为2个步骤。

1.1 步骤流程

Map阶段2个步骤

  • 设置InputFormat类,将数据切分为key-value(k1和v1)对,输入到第二步;
  • 自定义Map逻辑,将第一步的结果转换为另外的Key-Value(k2和v2)对,输出结果。

Shuffle阶段的4个步骤

  • 对输出的Key-Value进行分区;
  • 对不同分区的数据按照相同的Key排序;
  • (可选)对分组过的数据初步规约,降低数据的网络拷贝;
  • 对数据进行分组,相同key的value放入同一个集合中。

Reduce阶段2个步骤

  • 对多个Map任务的结果进行排序以及合并,编写Reduce函数实现自己的逻辑,对输入的key-value进行处理,转为新的key-value(k3和v3)输出;
  • 设置OutputFormat处理并保存Reduce输出的key-value数据。

1.1.1 Map阶段

Map函数的输入来自于分布式文件系统的文件块,这些文件块的格式是任意的,可以是文档,也可以是二进制格式。

文件块是一系列元素的集合,这些元素也是任意类型的,同一个元素不能跨文件块存储。

Map函数将输入的元素转换成<key,value>形式的键值对,键和值的类型也是任意的,其中键不同于一般的标志属性,即键没有唯一性。

Map阶段输出的结果是许多<key,1>

具有相同key的键值对会被发送到同一个Reduce那里。
在这里插入图片描述

1.1.2 Shuffle阶段

在这里插入图片描述

1.1.3 Reduce阶段

在这里插入图片描述

1.2 InputFormat

平时我们写MapReduce程序的时候,在设置输入格式的时候,总会调用形如job.setInputFormatClass(KeyValueTextInputFormat.class)来保证输入文件按照我们想要的格式被读取。

所有的输入格式都继承于InputFormat,这是一个抽象类,其子类有专门用于读取普通文件的FileInputFormat,用来读取数据库的DBInputFormat等等。

其实,一个输入格式InputFormat,主要无非就是要解决如何将数据分割成分片(比如多少行为一个分片),以及如何读取分片中的数据(比如按行读取)。前者由getSplits()完成,后者由RecordReader完成。

不同的InputFormat都会按自己的实现来读取输入数据并产生输入分片,一个输入分片会被单独的map task作为数据源。


下面我们先看看这些输入分片(inputSplit)是什么样的。

1.2.1 InputSplit

Mappers的输入是一个一个的输入分片,称InputSplit。InputSplit是一个抽象类,它在逻辑上包含了提供给处理这个InputSplit的Mapper的所有K-V对。

public abstract class InputSplit {public abstract long getLength() throws IOException, InterruptedException;public abstract String[] getLocations() throws IOException, InterruptedException;
}
  • getLength()用来获取InputSplit的大小,以支持对InputSplits进行排序;
  • getLocations()则用来获取存储分片的位置列表。

我们来看一个简单InputSplit子类:FileSplit。

public class FileSplit extends InputSplit implements Writable {private Path file;private long start;private long length;private String[] hosts;FileSplit() {}public FileSplit(Path file, long start, long length, String[] hosts) {this.file = file;this.start = start;this.length = length;this.hosts = hosts;}//序列化、反序列化方法,获得hosts等等……
}

从上面的源码我们可以看到,一个FileSplit是由文件路径,分片开始位置,分片大小和存储分片数据的hosts列表组成,由这些信息我们就可以从输入文件中切分出提供给单个Mapper的输入数据。这些属性会在Constructor设置,我们在后面会看到这会在InputFormat的getSplits()中构造这些分片。


我们再看CombineFileSplit

public class CombineFileSplit extends InputSplit implements Writable {private Path[] paths;private long[] startoffset;private long[] lengths;private String[] locations;private long totLength;public CombineFileSplit() {}public CombineFileSplit(Path[] files, long[] start, long[] lengths, String[] locations) {initSplit(files, start, lengths, locations);}public CombineFileSplit(Path[] files, long[] lengths) {long[] startoffset = new long[files.length];for (int i = 0; i < startoffset.length; i++) {startoffset[i] = 0;}String[] locations = new String[files.length];for (int i = 0; i < locations.length; i++) {locations[i] = "";}initSplit(files, startoffset, lengths, locations);}private void initSplit(Path[] files, long[] start, long[] lengths, String[] locations) {this.startoffset = start;this.lengths = lengths;this.paths = files;this.totLength = 0;this.locations = locations;for(long length : lengths) {totLength += length;}}//一些getter和setter方法,和序列化方法
}

与FileSplit类似,CombineFileSplit同样包含文件路径,分片起始位置,分片大小和存储分片数据的host列表。

由于CombineFileSplit是针对小文件的,它把很多小文件包在一个InputSplit内,这样一个Mapper就可以处理很多小文件。

要知道我们上面的FileSplit是对应一个输入文件的,也就是说如果用FileSplit对应的FileInputFormat来作为输入格式,那么即使文件特别小,也是单独计算成一个输入分片来处理的。当我们的输入是由大量小文件组成的,就会导致有同样大量的InputSplit,从而需要同样大量的Mapper来处理,这将很慢,想想有一堆map task要运行!!这是不符合Hadoop的设计理念的,Hadoop是为处理大文件优化的。


最后介绍TagInputSplit,这个类就是封装了一个InputSplit,然后加了一些tags在里面满足我们需要这些tags数据的情况,我们从下面就可以一目了然。

class TaggedInputSplit extends InputSplit implements Configurable, Writable {private Class<? extends InputSplit> inputSplitClass;private InputSplit inputSplit;@SuppressWarnings("unchecked")private Class<? extends InputFormat> inputFormatClass;@SuppressWarnings("unchecked")private Class<? extends Mapper> mapperClass;private Configuration conf;//getters and setters,序列化方法,getLocations()、getLength()等
}

1.2.2 InputFormat

现在我们对InputSplit的概念有了一些了解,我们继续看它是怎么被使用和计算出来的。

通过使用InputFormat,MapReduce框架可以做到:

  1. 验证作业的输入的正确性
  2. 将输入文件切分成逻辑的InputSplits,一个InputSplit将被分配给一个单独的Mapper task
  3. 提供RecordReader的实现,这个RecordReader会从InputSplit中正确读出一条一条的K-V对供Mapper使用。
public abstract class InputFormat<K, V> {public abstract List<InputSplit> getSplits(JobContext context) throws IOException,InterruptedException;public abstract RecordReader<K,V> createRecordReader(InputSplit split,TaskAttemptContext context) throws IOException,InterruptedException;
}

上面是InputFormat的源码,getSplits用来获取由输入文件计算出来的InputSplits,我们在后面会看到计算InputSplits的时候会考虑到输入文件是否可分割、文件存储时分块的大小和文件大小等因素;

createRecordReader()提供了前面第三点所说的RecordReader的实现,以将K-V对从InputSplit中正确读出来,比如LineRecordReader就以偏移值为key,一行的数据为value,这就使得所有其createRecordReader()返回了LineRecordReader的InputFormat都是以偏移值为key,一行数据为value的形式读取输入分片的。


**PathFilter被用来进行文件筛选,这样我们就可以控制哪些文件要作为输入,哪些不作为输入。**PathFilter有一个accept(Path)方法,当接收的Path要被包含进来,就返回true,否则返回false。可以通过设置mapred.input.pathFilter.class来设置用户自定义的PathFilter。

public interface PathFilter {boolean accept(Path path);
}

FileInputFormat是InputFormat的子类,它包含了一个MultiPathFilter,这个MultiPathFilter由一个过滤隐藏文件(名字前缀为’-‘或’.’)的PathFilter和一些可能存在的用户自定义的PathFilters组成,MultiPathFilter会在listStatus()方法中使用,而listStatus()方法又被getSplits()方法用来获取输入文件,也就是说实现了在获取输入分片前先进行文件过滤。

private static class MultiPathFilter implements PathFilter {private List<PathFilter> filters;public MultiPathFilter(List<PathFilter> filters) {this.filters = filters;}public boolean accept(Path path) {for (PathFilter filter : filters) {if (!filter.accept(path)) {return false;}}return true;}
}

1.2.3 TextInputFormat

我们来看看FileInputFormat的几个子类。

public class TextInputFormat extends FileInputFormat<LongWritable, Text> {@Overridepublic RecordReader<LongWritable, Text> createRecordReader(InputSplit split,TaskAttemptContext context) {return new LineRecordReader();}@Overrideprotected boolean isSplitable(JobContext context, Path file) {CompressionCodec codec = new CompressionCodecFactory(context.getConfiguration()).getCodec(file);return codec == null;}
}

二、案例

2.1 单词统计

2.1.1 数据准备

  1. 创建一个新的文件

    cd /export/servers
    vim wordcount.txt
    
  2. 向其中放入英文句子并保存

  3. 上传到HDFS

    hdfs dfs -mkdir /wordcount/
    hdfs dfs -put wordcount.txt /wordcount/
    

2.1.2 Mapper

import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;/*Mapper<KeyIn,ValueIn,KeyOut,ValueOut>KeyIn:k1ValueIn:v1KeyOut:k2ValueOut:v2*/public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {/*参数:key:k1 行偏移量value:v1 每一行的文本数据content:上下文对象*/@Overrideprotected void map(LongWritable key, Text value,Context context) throws  IOException, InterruptedException {   Text text=new Text();LongWritable longWritable=new LongWritable();//get  values stringString  valueString = value.toString();//spile string          String  wArr[] = valueString.split(" ");//map  out key/value for (String word:wArr) {text.set(word);longWritable.set(1);context.write(text,longWritable);}}
}

2.1.3 Reducer

import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;/*Reducer<KeyIn,ValueIn,KeyOut,ValueOut>KeyIn:k2ValueIn:v2KeyOut:k3ValueOut:v3*/public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> 
{     /*参数:key:新k2 行偏移量v2s:集合 新v2content:上下文对象*/@Override     protected  void reduce(Text key, Iterable<LongWritable> v2s,Context  context) throws  IOException, InterruptedException {Iterator<LongWritable> it = v2s.iterator();          //define  var sum          long  sum = 0;  //  iterator count arr     while(it.hasNext()){        sum  += it.next().get();    }context.write(key,new LongWritable(sum));    }
}

2.1.4 定义主类

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.Job;
public class TestMapReducer {public  static void main(String[] args) throws Exception{  Configuration  conf = new Configuration();conf.set("fs.default.name", "hdfs://ubuntu:9000");     //  step1 : get a job   Job  job = Job.getInstance(conf);       //step2:  set jar main class       job.setJarByClass(TestMapReducer.class);  //step3:  set map class and reducer class     job.setMapperClass(WordCountMapper.class);      job.setReducerClass(WordCountReducer.class);     //step4:  set map reduce output type      job.setMapOutputKeyClass(Text.class);    //k2job.setMapOutputValueClass(LongWritable.class); //v3       job.setOutputKeyClass(Text.class);      //k3job.setOutputValueClass(LongWritable.class); //v3// set input/output typejob.setInputFormatClass(TextInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);//step5:  set key/value output file format and input/output path     TextInputFormat.setInputPath(job, new  Path("hdfs://node01:8020/wordcount"));      TextOutputFormat.setOutputPath(job, new  Path("hdfs://node01:8082/output"));  //step6:  commit job       job.waitForCompletion(true); }
}

2.1.5 MapReduce运行模式

  1. 集群运行模式
  • 将MapReduce程序提交给Yarn集群,分发到很多节点上并发执行;
  • 处理的数据和输出结果应该位于HDFS文件系统;
  • 提交集群的实现步骤:将程序打成JAR包,并上传,然后在集群上用hadoop命令启动
执行命令:hadoop jar xxx.jar 主类名
  1. 本地运行模式
  • MapReduce程序是在本地以单进程的形式运行;
  • 处理的数据及输出结果在本地文件系统
// 这样是指本地文件系统
TextInputFormat.setInputPaths(job, new  Path("file:///home/zcc/Desktop/simple/zcc.txt"));
TextOutputFormat.setOutputPath(job, new  Path("file:///home/zcc/Desktop/simple/output"));  

三、MapReduce分区

3.1 分区概述

在MapReduce中,通过我们指定分区,会将同一个分区的数据发送到同一个Reduce中进行处理。

3.2 编程实现

案例说明:将大于15的数字和小于等于15的数字分离。

3.2.1 定义Mapper


import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.NullWritable;import java.io.IOException;
/** k1:行偏移量* v1:行文本数据* * k2:行文本数据* v2:NullWritable,v2不需要设置,但又必须有,因此使用Null占位*/public class PartitionMap extends Mapper<LongWritable,Text,Text,NullWritable>{@Overrideprotected void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException {context.write(value,NullWritable.get());}
}

3.2.2 自定义Partitioner

主要的分区逻辑就在这里,通过Partitioner将数据分发给不同的Reducer。

我们需要继承Partitioner,重写getPartition方法,默认的分区规则是只要key相同就是同一个分区。

使用setPartitionerClass设置自定义的parttitioner。

package partition;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.NullWritable;public class MyPartitoner extends Partitioner<Text,NullWritable>{/**定义分区规则,反回对应的分区编号*/@Overridepublic int getPartition(Text text,NullWritable nullWritable,int i) {String[] split=text.toString().split("\t");String numStr=split[5];if(Integer.parseInt(numStr)>15) {return 1;}return 0;}
}

3.2.3 定义Reducer


3.2.4 编写主类

package partition;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.NullWritable;public class JobMain extends Configured implements Tool{@Overridepublic int run(String[] arg0) throws Exception {//1. 获得Job实例Job job = Job.getInstance(super.getConf(),"partition_mapreduce");//2. 对job任务进行配置//第一步:设置输入类和输入的路径job.setInputFormatClass(TextInputFormat.class);TextInputFormat.addInputPath(job, new Path("hdfs://ubuntu:8020/input"));//第二步:设置Mapper类和数据类型job.setMapperClass(PartitionMap.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(NullWritable.class);//第三步:指定分区类job.setPartitionerClass(MyPartitioner.class);//第四步:指定Reducer类和数据类型job.setReducerClass(PartitionReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(NullWritable.class);//设置Reducer Task的个数job.setNumReduceTasks(2);//第五步:指定输出类和输出路径job.setOutputFormatClass(TextOutputFormat.class);TextOutputFormat.setOutputPath(job,new Path("hdfs://ubuntu:8020/out/partition_out"));boolean b=job.waitForCompletion(true);return b?1:0;}public static void main(String[] args) throws Exception{Configuration configuration=new Configuration();int code=ToolRunner.run(configuration,new JobMain(),args);System.out.println(code);}}

四、排序和序列化

4.1 概述

  • 序列化是指把结构化对象转化为字节流。
  • 反序列化是指序列化的逆过程,把字节流转为结构化对象。当要在进程间传递对象或持续化对象的时候,就需要序列化对象成字节流;反之当要将接收到或从磁盘中读取的字节流转为对象时,就要进行反序列化。
  • Hadoop的序列化机制(writable)具有精简高效的特点。
  • Writable时Hadoop的序列化格式,Hadoop定义了Writable接口,一个类要支持可序列化只需要实现这个接口即可。
  • 另外Writable有一个子接口,WritableComparable,它是即可实现序列化,也可以对key进行比较。

4.2 编程实现

要求:对下列数据进行排序,首先对第一列进行排序,第一列相同时,对第二列进行排序。

数据格式

a 1
a 9
b 3
a 7
b 8
b 10
a 5

4.2.1 自定义类型和比较器

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;import org.apache.hadoop.io.WritableComparable;public class SortBean implements WritableComparable<SortBean>{private String word;private int num;@Overridepublic void readFields(DataInput in) throws IOException {this.word=in.readUTF();this.num=in.readInt();}@Overridepublic void write(DataOutput out) throws IOException {out.writeUTF(word);out.writeInt(num);}@Overridepublic int compareTo(SortBean o) {int res=this.word.compareTo(o.getWord());if(res==0) {return this.num-o.getNum();}return res;}public String getWord() {return word;}public void setWord(String word) {this.word = word;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}@Overridepublic String toString() {return word+"\t"+num;}}

4.2.2 Mapper

import org.apache.hadoop.io.Text;import java.io.IOException;import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Mapper;public class SortMapper extends Mapper<LongWritable,Text,SortBean,NullWritable>{@Overrideprotected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {String[] split = value.toString().split("\t");SortBean sortBean=new SortBean();sortBean.setWord(split[0]);sortBean.setNum(Integer.parseInt(split[1]));context.write(sortBean,NullWritable.get());}}

4.2.3 Reducer

import java.io.IOException;import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;public class SortReducer extends Reducer<SortBean,NullWritable,SortBean,NullWritable>{@Overrideprotected void reduce(SortBean key,Iterable<NullWritable> values,Context context) throws IOException, InterruptedException {context.write(key,NullWritable.get());}}

4.2.4 主类

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.NullWritable;public class JobMain extends Configured implements Tool{@Overridepublic int run(String[] arg0) throws Exception {//1. Job job = Job.getInstance(super.getConf(),"partition_mapreduce");job.setInputFormatClass(TextInputFormat.class);TextInputFormat.addInputPath(job, new Path("hdfs://ubuntu:8020/input"));job.setMapperClass(SortMapper.class);job.setMapOutputKeyClass(SortBean.class);job.setMapOutputValueClass(NullWritable.class);job.setReducerClass(SortReducer.class);job.setOutputKeyClass(SortBean.class);job.setOutputValueClass(NullWritable.class);job.setNumReduceTasks(2);job.setOutputFormatClass(TextOutputFormat.class);TextOutputFormat.setOutputPath(job,new Path("hdfs://ubuntu:8020/out/partition_out"));boolean b=job.waitForCompletion(true);return b?1:0;}public static void main(String[] args) throws Exception{Configuration configuration=new Configuration();int code=ToolRunner.run(configuration,new JobMain(),args);System.out.println(code);	}
}

五、规约Combiner

5.1 概述

又叫合并

每一个map都可能产生大量的本地输出,Combiner的作用就是对map端的输出先做一次合并(也可以说是局部reduce),以减少在map和reduce节点之间的数据传输量,提高网络IO性能,是MapReduce的一种优化手段。

  • combiner是MR程序中,除Mapper和Reducer之外的一种组件;
  • combiner的父类就是Reducer;
  • combiner和reducer的区别在于运行的位置
    • combiner是在每一个maptask所在节点运行
    • reducer是接收全局所有Mapper的输出结果
  • combiner的意义就是对每一个maptask的输出进行局部汇总。

5.2 实现步骤

  1. 自定义一个combiner继承Reducer,重写reducer方法;
  2. 在job中设置job.setCombinerClass()

combiner能够应用的前提是不能影响最终的业务逻辑,而且combiner的输出kv,要与reducer的输入kv类型对应。

lic static void main(String[] args) throws Exception{Configuration configuration=new Configuration();int code=ToolRunner.run(configuration,new JobMain(),args);System.out.println(code);	}
}

http://chatgpt.dhexx.cn/article/FhmdBVim.shtml

相关文章

SSL/TLS

SSL/TLS 一、SSL/TLS1.1 历史发展1.2 使用场景1.3 解决的问题1.4 工作流程 二、对称加密&#xff08;Symmetric Cryptography&#xff09;2.1 工作原理2.2 翻转攻击2.3 认证加密&#xff08;Authentication Encryption&#xff09;2.4 Diffie-Hellman2.5 KDF2.6 Diffie-Hellman…

HTTPS,SSL,TLS

SSL TLS secure sockets layer 安全套接字层&#xff0c;Netscape公司研发。 transport layer security 安全传输层协议 定义 协议 年份 SSL 1.0 未知 SSL 2.0 1995 SSL 3.0 1996 TLS 1.0 1999 TLS 1.1 2006 TLS 1.2 2008 TLS 1.3 2018 IETF&#xff08;The…

TLS传输协议

TLS&#xff1a;安全传输层协议&#xff08;TLS&#xff09;用于在两个通信应用程序之间提供保密性和数据完整性。 该协议由两层组成&#xff1a;TLS 记录协议&#xff08;TLS Record&#xff09;和 TLS 握手协议&#xff08;TLS Handshake&#xff09;。 传输层安全性协议&a…

LVGL misc tlsf算法(lv_tlsf.c)

更多源码分析请访问:LVGL 源码分析大全 目录 1、概述2、算法特点3、同类型算法举例1、概述 LVGL采用的内存分配器是使用的tlsf算法。因为这个算法只是一个实时系统常用的算法,可以看作是一个工具,对LVGL本身并没有逻辑上的关联,所以这里只介绍一下算法的基本知识,就不过…

TLS/SSL 协议详解(17) Certificate verify

发送这个类型的握手需要2个前提条件 &#xff08;1&#xff09;&#xff1a;服务器端请求了客户端证书 &#xff08;2&#xff09;&#xff1a;客户端发送了非0长的证书 此时&#xff0c;客户端想要证明自己拥有该证书&#xff0c;必然需要私钥签名一段数据发给服务器验证。 …

HTTPS之TLS证书

文章目录 一. TLS概述1. TLS概述2. HTTPS 协议栈与 HTTP 的唯一区别3. TLS协议版本 二. TLS证书格式1. 概述2. 示例&#xff1a;知乎网站证书解析(mac系统)3. 通过openssl获取证书的含义 三. 证书链&#xff08;Certificate Chain&#xff09;1. 背景2. 概述3. 背景问题的解释 …

SSL和TLS简单概述

SSL和TLS简单概述 本文不会只有几个比较重要的概念,科普性质的文章,方便自己记忆,极大概率存在缺陷 如果想了解这方面的内容&#xff0c;请参阅官方文档。 SSL和TLS TLS是更安全版本的ssl,先出的的ssh,一个基于加密机制的应用,之后为了方便给其他应用层使用然后引入了ssl,最…

动态内存管理——tlsf

定义 TLSF(全称Two-Level Segregated Fit) 源码 https://github.com/mattconte/tlsf 代码 结构体 typedef struct block_header_t {/* 指向上一个物理块。*/struct block_header_t * prev_phys_block;/* 此块的大小&#xff0c;不包括块头。*/size_t size;/* 下一个和上一…

SSL与TLS协议详解

写在最前面的话&#xff1a;这篇文章是我借鉴了Eric Rescorla的《SSL and TLS》一书之后对该书的前半部分内容整合而做。如您需要开发围绕SSL、TLS的程序建议参阅原著或者RFC相关文档。 一、关于SSL、TLS与HTTPS的三两事 什么是SSL、TLS&#xff1a; 众所周知&#xff0c;真…

TLS协议/SSL协议

历史背景 SSL(Secure Socket Layer 安全套接层)是基于HTTPS下的一个协议加密层&#xff0c;最初是由网景公司&#xff08;Netscape&#xff09;研发&#xff0c;后被IETF&#xff08;The Internet Engineering Task Force - 互联网工程任务组&#xff09;标准化后写入&#xf…

TLS加密体系

谈到这个词&#xff0c;可能大家的第一印象就是加密&#xff0c;而对TLS了解甚少。那么在介绍 TLS 加密体系之前先来讲一讲加密。 一提到加密&#xff0c;可能很多人脑海中会浮现出电视剧里特务的场景&#xff0c;他们拿出一台电报机&#xff0c;“滴滴滴滴”按下情报报文&…

TLS概述

握手过程 可分为5步&#xff08;使用Diffie – Hellman算法&#xff09;&#xff1a; 第一步&#xff0c;浏览器给出协议版本号、一个客户端生成的随机数&#xff08;Client random&#xff09;&#xff0c;以及客户端支持的加密方法。 第二步&#xff0c;服务器确认双方使用的…

SSL与DTLS简介

目录 SSL简介 DTLS-基于UDP的TLS 记录层 传输层映射 早期我们在访问web时使用HTTP协议&#xff0c;该协议在传输数据时使用明文传输&#xff0c;会带来了以下风险&#xff1a; 信息窃听风险&#xff0c;第三方可以获取通信内容&#xff1b; 信息篡改风险&#xff0c;第三方…

TLS/SSL 协议

TLS/SSL 协议的工作原理 TLS/SSL 协议的工作原理 • 身份验证 • 保密性 • 完整 TLS/SSL 发展 TLS 协议 • Record 记录协议 • 对称加密 • Handshake 握手协议 • 验证通讯双方的身份 • 交换加解密的安全套件 • 协商加密参 TLS 安全密码套件解 对称加密的工作原理&am…

SSL/TLS详解

SSL/TLS详解 1. 前言 ​ 我们都知道Https就是加密协议中采用了SSL/TLS协议&#xff0c;这是面试常客&#xff0c;如果被问到了&#xff0c;你懂的越多&#xff0c;答得越深&#xff0c;你的面评相应来说也就会越高&#xff0c;对于SSL/TLS&#xff0c;我们不仅仅要知道其为数…

TLS协议简单介绍

TLS简介 介绍 TLS&#xff08;Transport Layer Security&#xff09;即安全传输层协议&#xff0c;在两个通信应用程序之间提供保密性和数据完整性。最典型的应用就是HTTPS。HTTPS&#xff0c;即HTTP over TLS&#xff0c;就是安全的HTTP&#xff0c;运行在HTTP层之下&#x…

esp-idf的内存管理——tlsf之上的封装

目录 1 为什么要封装2 先看结构2.1 multi heapnote1note2 2.2 heap caps2.3 层次关系 3 再看接口3.1 内存的申请3.2 内存的释放3.2 堆完整性检测3.3 其它 参考 1 为什么要封装 封装通常会降低效率&#xff0c;但能够带来诸如通用性提升等好处&#xff0c;idf在tlsf的基础上增加…

SSL/TLS 证书管理

SSL 证书发现 随着组织的 IT 基础架构的扩展&#xff0c;他们为每台计算机获取证书以保护其资源和域。此外&#xff0c;开发人员通常会创建许多自签名证书&#xff0c;以便在产品的开发阶段保护内部网络。组织通常最终会拥有数千个证书。自动发现证书提供了对证书基础结构的完…

TLS协议。

IPSec通过安全关联实现IP分组安全关联两端之间的安全传输过程&#xff0c;TLS通过建立安全连接实现数据在两个应用进程之间的安全传输过程。TLS建立安全连接时&#xff0c;实现安全连接两端应用进程之间的双向身份鉴别过程&#xff0c;保证经过安全连接传输的数据的保密性和完整…

SSL/TLS协议

SSL/TLS协议 文章目录 SSL/TLS协议1 协议历史2 协议的目标3 SSL体系结构4 两个主要的协议5 SSL的两个重要概念6 会话状态参数7 连接状态参数8 SSL Record Protocol9 SSL记录协议中的操作10 SSL握手协议使用的消息11 SSL握手协议的流程 1 协议历史 1994年Netscape开发了SSL(Sec…