提取出某日访问百度次数最多的那个IP(Java实现)

思路参考july博客http://blog.csdn.net/v_july_v/article/details/7382693

1、海量日志数据,提取出某日访问百度次数最多的那个IP。


因为内存容量有限。所以需对大的文件进行切割。在分割文件时应使相同的IP保存到同一个文件中。可以采用取模操作。

注意:相同的IP必须存储到相同的文件中

因为每个IP(相当于字符串)对应了一个hashcode,相同的IP的hashcode肯定相同,通过hashcode对某个数取模,比如100.,这样原文件分割成100个文件。

根据取模的结果存储到相应的文件中。相同的IP会存储到同一个文件中。分割后的文件大小大约为原来的1/n(若对n取模),若对100取模大约为原文件的1/100。如果分割

后的文件中可能有部分文件内存中还装载(load)不下,可以对该文件继续分割直至内存可以装下为止。(比如对该文件继续对2求模)


对于分割后的文件,求每个文件上出现次数最多的IP。此时可以用hashmap存储每个IP出现的次数。key存储为IP字符串,value为该字符串出现的次数。每访问文件中的一条记录(IP),若该IP在hashmap中已存在,相应的value增加1。否则向hashmap中插入(put)一条新的记录。

统计该hashmap上拥有最大value的项


最后比较所有文件上访问最多的IP便求出了访问次数最多的IP




import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
public class MassiveIP {
	//generate the massive numbers of IPs
	public void generateIP(String fileName){
		PrintWriter out =null;
		try {
			
			out=new PrintWriter(fileName);
			String s;
			Random r=new Random();
			
			for(int i=0;i<100000000;i++){
				s="159.227.";
				s+=r.nextInt(256)+"."+r.nextInt(256);
				out.println(s);
			}
				
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			if (out != null)
				out.close( );
		}
		
	}
	//split the file to make it fit into the memory
	public void FileSplit(String fileName){
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader (fileName));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PrintWriter[] out=new PrintWriter[100];
		for(int i=0;i<100;i++)
			try {
				//specify split file name
				out[i]=new PrintWriter(fileName+i);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    String IP = null;
	    try {
			while((IP =reader.readLine())!= null ) {
				IP=reader.readLine();
				int fileNum=IP.hashCode()%100;
				fileNum=(fileNum>=0?fileNum:fileNum+100);
			//	System.out.println(fileNum);
				out[fileNum].println(IP);
				
	    	}
			for(int i=0;i<100;i++)
				out[i].close();
			   
			//}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	//find IP with the largest number of occurrence
	public Map.Entry<String,Integer>  statitics(String fileName){
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader (fileName));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		String IP = null;
		try {
			while((IP =reader.readLine())!= null){
				//to judge whether the IP is already
				//existed in the HashMap
				if(map.containsKey(IP)){
					map.put(IP, map.get(IP)+1);
				}
				else
					map.put(IP,1);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Entry in HashMap with the maximum value 
		//which means the IP with the largest occurrence
		Map.Entry<String,Integer>  maxEntry=null;
		for (Map.Entry<String,Integer> entry : map.entrySet()){
		    if (maxEntry == null || entry.getValue()>maxEntry.getValue()) {
		        maxEntry = entry;
		    }
		}
		try {
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return maxEntry;
	}
	public static void main(String[] args){
		MassiveIP m=new MassiveIP();
		String FileName="D://Data//test.txt";
		m.generateIP(FileName);
		m.FileSplit(FileName);
		List<Map.Entry<String,Integer>>l
			=new ArrayList<Map.Entry<String,Integer>>();
		for(int i=0;i<100;i++)
			l.add(m.statitics(FileName+i));
		Map.Entry<String,Integer>maxEntry=l.get(0);
		for(int j=1;j<100;j++){
			if(l.get(j).getValue()>maxEntry.getValue())
				maxEntry=l.get(j);
		}
		System.out.println(maxEntry.getKey());
		System.out.println(maxEntry.getValue());
		
	}

}


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值