如果有大型数据文件(如每行为url或者ip或者单词等的),以G为单位的,处理的时候需先切分。普通切分方法直接根据数据条数切分,得到的每个文件大小相近。

但是有时需要将相同数据放到相同文件中。可以使用hash切分法。

public class Test {

	static int HASHLEN = 1000;

	public static void main(String[] args) {
// TODO Auto-generated method stub
String words [] = {"yes" ,"an" ,"go"};
for(String word:words){
int temp = hash(word.toCharArray());
System.out.println(temp);
}
}
public static int hash(char[] word) {
int index = 0;
int i=0;
while(i<word.length) {
index += index * 31 + word[i];
i++;
}
return index % HASHLEN;
}
}

相同单词一定得到相同的返回值,不同单词也可能得到相同返回值

05-29 00:38