Kafka 解析
Kafak采用内存映射文件、硬盘顺序写入技术提示性能。即便是顺序写入硬盘,硬盘的访问速度还是不可能追上内存。所以Kafka的数据并不是实时的写入硬盘,
它充分利用了现代操作系统分页存储来利用内存提高I/O效率。
一、顺序写入
1. 机械硬盘
机械硬盘的结构你可以想象成一个唱片机,它有一个旋转的盘片和一个能沿半径方向移动的磁头。处理读取和写入请求时,
首先可以根据请求的开始地址算出要处理的数据在磁盘上的位置,之后要进行以下几步工作:
2. 固态硬盘
java io操作中通常采用BufferedReader,BufferedInputStream等带缓冲的IO类处理大文件,不过java nio中引入MappedByteBuffer操作大文件的方式,其读写性能极高。
File.read()将文件从硬盘拷贝到内核空间的一个缓冲区,再将这些数据拷贝到用户空间,实际上进行了两次数据拷贝。
FileChannal.map()直接将文件从硬盘拷贝到用户空间,只进行了一次数据拷贝。
1.
public class MapMemeryBuffer {
public static void main(String[] args) throws Exception {
ByteBuffer byteBuf = ByteBuffer.allocate(14 * 1024 * 1024);
byte[] bytes = new byte[14 * 1024 * 1024];
FileInputStream fis = new FileInputStream("d:\\java_transactions_book.pdf");
FileOutputStream fos = new FileOutputStream("d:\\java_transactions_book_copy.pdf");
FileChannel fileChannel = fis.getChannel(); long timeStar = System.currentTimeMillis();
// 读取
//fileChannel.read(byteBuf);
MappedByteBuffer mbb = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
long timeEnd = System.currentTimeMillis();
System.out.println("Read time :" + (timeEnd - timeStar) + "ms"); timeStar = System.currentTimeMillis();
// 写入
//fos.write(bytes);
// After a sequence of channel-read or put operations,
// invoke this method to prepare for a sequence of channel-write or relative get operations.
mbb.flip();
timeEnd = System.currentTimeMillis();
System.out.println("Write time :" + (timeEnd - timeStar) + "ms"); fos.flush();
fileChannel.close();
fis.close();
}
}
2.注意
A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected.
The content of a mapped byte buffer can change at any time, for example if the content of the corresponding region of the mapped file
is changed by this program or another. Whether or not such changes occur, and when they occur, is operating-system dependent and
therefore unspecified.
All or part of a mapped byte buffer may become inaccessible at any time, for example if the mapped file is truncated. An attempt to access an
inaccessible region of a mapped byte buffer will not change the buffer's content and will cause an unspecified exception to be thrown either at
the time of the access or at some later time. It is therefore strongly recommended that appropriate precautions be taken to avoid the manipulation
of a mapped file by this program, or by a concurrently running program, except to read or write the file's content.
Mapped byte buffers otherwise behave no differently than ordinary direct byte buffers.
解决:
AccessController.doPrivileged(newPrivilegedAction() {
publicObject run() {
try{
Method getCleanerMethod = buffer.getClass().getMethod("cleaner",newClass[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner)
getCleanerMethod.invoke(byteBuffer,newObject[0]);
cleaner.clean();
} catch(Exception e) {
e.printStackTrace();
}
returnnull;
}
});
参考:
Kafka 解析的更多相关文章
- Spark Streaming on Kafka解析和安装实战
本课分2部分讲解: 第一部分,讲解Kafka的概念.架构和用例场景: 第二部分,讲解Kafka的安装和实战. 由于时间关系,今天的课程只讲到如何用官网的例子验证Kafka的安装是否成功.后续课程会接着 ...
- Kafka设计解析(一)- Kafka背景及架构介绍
本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...
- Kafka深度解析
本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...
- Kafka深度解析,众人推荐,精彩好文!
作者: Jason Guo 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,并保证即使对TB级以上数据 ...
- Kafka深度解析(如何在producer中指定partition)(转)
原文链接:Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十四)定义一个avro schema使用comsumer发送avro字符流,producer接受avro字符流并解析
参考<在Kafka中使用Avro编码消息:Consumer篇>.<在Kafka中使用Avro编码消息:Producter篇> 在了解如何avro发送到kafka,再从kafka ...
- Kafka学习之一深度解析
背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能 高吞吐 ...
- kafka设计原理介绍
背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能 高吞吐 ...
- 关于Kafka配额的讨论(1)
Kafka自0.9.0.0版本引入了配额管理(quota management),旨在broker端对clients发送请求进行限流(throttling).目前Kafka支持两大类配额管理: 网络带 ...
随机推荐
- Python之Web前端Dom, jQuery
Python之Web前端: Dom jQuery ###Dom 一. 什么是Dom? 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它 ...
- VS:101 Visual Studio 2010 Tips
101 Visual Studio 2010 Tips Tip #1 How to not accidentally copy a blank line TO – Text Editor ...
- C#中(double)str;与Convert.ToDouble(str);有什么区别
1. string str = "32323";2. double a = (double)str ;3. double a = Convert.ToDouble(str); 第3 ...
- 【Java EE 学习 49 下】【Spring学习第一天】【MVC】【注解回顾】
一.MVC 1.使用Spring有一个非常大的好处,那就是能够实现完全面向接口编程,传统的使用Dao.Service并不能实现完全的面向接口编程. 2.示例:https://github.com/kd ...
- JVM 1.6 GC
JVM调优是一门艺术. JVM调优的重点是减少Major GC的次数,因为Major GC会暂停程序比较长的时间.如果Major GC的次数比较多,意味着应用程序的JVM内存参数需要调整. JVM内存 ...
- 使用ajax.dll时js脚本错误-XXX未定义
操作系统:Windows 7 IIS:7.5 ajax.dll现在用的比较少,但是以前的项目有这个,使用的时候很容易出现这个错误,因为总是会遗漏配置. 使用ajax.dll时,js脚本错误,无法调用后 ...
- RESTful API 设计指南 (转)
RESTful API 设计指南 2016-02-23 ImportNew (点击上方公号,可快速关注) 作者:阮一峰 链接:http://www.ruanyifeng.com/blog/2014/0 ...
- 数据库错误:check the manual that corresponds to your MySQL server version for the right sy
检查对应到您的MySQL服务器版本附近使用正确的语法手册 数据库插入的时候出现上述问题,总结了两方面原因: 1.语法错误,这是百度之得到的大部分结果,但是没有解决我的问题 2.仔细观察我的sql语句, ...
- SQL性能学习汇总 00
.在一条语句中不要重复使用相同的函数 .尽可能在存储过程中使用临时变量和临时表 如 IF (Object_ID('tempdb..#T') IS NOT NULL) DROP TABLE #T SEL ...
- 【Spring】获取资源文件+从File+从InputStream对象获取正文数据
1.获取资源文件或者获取文本文件等,可以通过Spring的Resource的方式获取 2.仅有File对象即可获取正文数据 3.仅有InputStream即可获取正文数据 package com.sx ...