java⼤⽂件整数怎么排序问题
给你1个⽂件bigdata,⼤⼩4663M,5亿个数,⽂件中的数据随机,如下⼀⾏⼀个整数:
sort命令排序
6196302
3557681
6121580
2039345
2095006
1746773
7934312
2016371
7123302
8790171
2966901
...
7005375
现在要对这个⽂件进⾏排序,怎么搞?
内部排序
先尝试内排,选2种排序⽅式:
3路快排:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26private final int cutoff = 8;
public<T> void perform(Comparable<T>[] a) {
perform(a,0,a.length - 1);
}
private<T> int median3(Comparable<T>[] a,int x,int y,int z) {    if(lessThan(a[x],a[y])) {
if(lessThan(a[y],a[z])) {
return y;
}
else if(lessThan(a[x],a[z])) {
return z;
}else{
return x;
}
}else{
if(lessThan(a[z],a[y])){
return y;
}else if(lessThan(a[z],a[x])) {
return z;
}else{
return x;
}
}
}
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81  }
private<T> void perform(Comparable<T>[] a,int low,int high) {    int n = high - low + 1;
//当序列⾮常⼩,⽤插⼊排序
if(n <= cutoff) {
InsertionSort insertionSort = ateInsertionSort();      insertionSort.perform(a,low,high);
//当序列中⼩时,使⽤median3
}else if(n <= 100) {
int m = median3(a,low,low + (n >>> 1),high);
exchange(a,m,low);
//当序列⽐较⼤时,使⽤ninther
}else{
int gap = n >>> 3;
int m = low + (n >>> 1);
int m1 = median3(a,low,low + gap,low + (gap << 1));
int m2 = median3(a,m - gap,m,m + gap);
int m3 = median3(a,high - (gap << 1),high - gap,high);
int ninther = median3(a,m1,m2,m3);
exchange(a,ninther,low);
}
if(high <= low)
return;
//lessThan
int lt = low;
/
/greaterThan
int gt = high;
//中⼼点
Comparable<T> pivot = a[low];
int i = low + 1;
/*
* 不变式:
*  a[low..lt-1] ⼩于pivot -> 前部(first)
*  a[lt..i-1] 等于 pivot -> 中部(middle)
*  a[-1] ⼤于 pivot -> 后部(final)
*
*  ] 待考察区域
*/
while(i <= gt) {
if(lessThan(a[i],pivot)) {
//i-> ,lt ->
exchange(a,lt++,i++);
}else if(lessThan(pivot,a[i])) {
exchange(a,i,gt--);
}else{
i++;
}
}
// a[low..lt-1] < v = ] < a[gt+1..high].
perform(a,low,lt - 1);
81 82    perform(a,low,lt - 1);    perform(a,gt + 1,high);  }
归并排序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49/**
* ⼩于等于这个值的时候,交给插⼊排序
*/
private final int cutoff = 8;
/**
* 对给定的元素序列进⾏排序
*
* @param a 给定元素序列
*/
@Override
public<T> void perform(Comparable<T>[] a) {
Comparable<T>[] b = a.clone();
perform(b, a, 0, a.length - 1);
}
private<T> void perform(Comparable<T>[] src,Comparable<T>[] dest,int low,int high) {
if(low >= high)
return;
//⼩于等于cutoff的时候,交给插⼊排序
if(high - low <= cutoff) {
return;
}
int mid = low + ((high - low) >>> 1);
perform(dest,src,low,mid);
perform(dest,src,mid + 1,high);
//考虑局部有序 src[mid] <= src[mid+1]
if(lessThanOrEqual(src[mid],src[mid+1])) {
System.arraycopy(src,low,dest,low,high - low + 1);
}
//src[low .. mid] + src[mid+1 .. high] -> dest[low .. high]
merge(src,dest,low,mid,high);
}
private<T> void merge(Comparable<T>[] src,Comparable<T>[] dest,int low,int mid,int high) {
for(int i = low,v = low,w = mid + 1; i <= high; i++) {
if(w > high || v <= mid && lessThanOrEqual(src[v],src[w])) {
dest[i] = src[v++];
}else{
dest[i] = src[w++];
}
}
49 }
数据太多,递归太深 ->栈溢出?加⼤Xss?
数据太多,数组太长 -> OOM?加⼤Xmx?
耐⼼不⾜,没跑出来.⽽且要将这么⼤的⽂件读⼊内存,在堆中维护这么⼤个数据量,还有内排中不断的拷贝,对栈和堆都是很⼤的压⼒,不具备通⽤性。
sort命令来跑
1sort-n bigdata -o bigdata.sorted
跑了多久呢?24分钟.
为什么这么慢?
粗略的看下我们的资源:
1. 内存
jvm-heap/stack,native-heap/stack,page-cache,block-buffer
2. 外存
swap + 磁盘
数据量很⼤,函数调⽤很多,系统调⽤很多,内核/⽤户缓冲区拷贝很多,脏页回写很多,io-wait很⾼,io很繁忙,堆栈数据不断交换⾄swap,线程切换很多,每个环节的锁也很多.
总之,内存吃紧,问磁盘要空间,脏数据持久化过多导致cache频繁失效,引发⼤量回写,回写线程⾼,导致cpu⼤量时间⽤于上下⽂切换,⼀切,都很糟糕,所以24分钟不细看了,⽆法忍受.
位图法
1 2 3 4 5
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24private BitSet bits;
public void perform(
String largeFileName,
int total,
String destLargeFileName,
Castor<Integer> castor,
int readerBufferSize,
int writerBufferSize,
boolean asc) throws IOException {
System.out.println("BitmapSort Started.");
long start = System.currentTimeMillis();
bits = new BitSet(total);
InputPart<Integer> largeIn = ateCharBufferedInputPart(largeFileName, readerBufferSize);
OutputPart<Integer> largeOut = ateCharBufferedOutputPart(destLargeFileName, writerBufferSize);  largeOut.delete();
Integer data;
int off = 0;
try{
while(true) {
data = ad();
if(data == null)
break;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64        break;
int v = data;
set(v);
off++;
}
largeIn.close();
int size = bits.size();
System.out.println(String.format("lines : %d ,bits : %d", off, size));
if(asc) {
for(int i = 0; i < size; i++) {
if(get(i)) {
largeOut.write(i);
}
}
}else{
for(int i = size - 1; i >= 0; i--) {
if(get(i)) {
largeOut.write(i);
}
}
}
largeOut.close();
long stop = System.currentTimeMillis();
long elapsed = stop - start;
System.out.println(String.format("BitmapSort Completed.elapsed : %dms",elapsed));  }finally{
largeIn.close();
largeOut.close();
}
}
private void set(int i) {
bits.set(i);
}
private boolean get(int v) {
(v);
}
nice!跑了190秒,3分来钟.
以核⼼内存4663M/32⼤⼩的空间跑出这么个结果,⽽且⼤量时间在⽤于I/O,不错.
问题是,如果这个时候突然内存条坏了1、2根,或者只有极少的内存空间怎么搞?
外部排序
该外部排序上场了.
外部排序⼲嘛的?
内存极少的情况下,利⽤分治策略,利⽤外存保存中间结果,再⽤多路归并来排序; map-reduce的嫡系.