问题
现有2个待推广手机号码数据文件,A文件1000W行,B文件是100W,文件中每行记录只有手机号,号码有重复,请设计高效方案先对A、B数据文件分别进行号码剔重,再找出B文件中在A文件存在的号码,请写出核心设计思想,并编写代码完整实现。(注:请用纯c实现,不许采用数据库、Memcached等第三方中间件,号码数据文件请自行模拟生成)答题要求:请同时提供Word设计文档,和程序源代码(如程序中使用了第三方Jar请注明,jar包请不要上传),打包上传。
思路及分析
号码剔重步骤
package art.programming.algorithm;import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set;import org.junit.Test;public class BigFileSpliter {private MapfileWriterMap = new HashMap ();public void split(File file) throws IOException{ InputStream is; BufferedReader br; String line; is = new FileInputStream(file); br = new BufferedReader(new InputStreamReader(is));while((line = br.readLine()) != null){ //System.out.println(line); append(line.substring(0,3)+".txt", line); } closeAllWriters(); br.close(); is.close(); }public void append(String fileName, String appendStr) throws IOException{if (!fileWriterMap.containsKey(fileName)){ File file = new File(fileName); if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file); fileWriterMap.put(fileName, fw); } fileWriterMap.get(fileName).write(appendStr); fileWriterMap.get(fileName).write(' '); }public void closeAllWriters(){ for(Writer fw : fileWriterMap.values()){ try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }public List getAllFileNames(){ Set keys = fileWriterMap.keySet(); List klist = new ArrayList (); for (String key : keys){ klist.add(key); }Collections.sort(klist); return klist; }@Test public void testSplit() throws IOException{ new BigFileSpliter().split(new File("cellphone.txt")); } }
第二步,将文件按照文件名排序,依次读取文件中的电话号码
package art.programming.algorithm;import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;public class BigFileSort {private final static int RANGE = 32; private int[] bitMap;private File outputFile; private File inputFile; private int bitMapLen = (99999999/RANGE) + RANGE; FileWriter fw;public BigFileSort(String inputFileName, String outputFileName) throws IOException{ bitMap = new int[bitMapLen]; outputFile = new File(outputFileName); if (outputFile.exists()){ outputFile.delete(); } outputFile.createNewFile(); inputFile = new File(inputFileName); fw = new FileWriter(outputFile); }public void sortAndOutput() throws IOException{ BigFileSpliter bigFileSpliter = new BigFileSpliter(); bigFileSpliter.split(inputFile); for (String fileName : bigFileSpliter.getAllFileNames()){ readAndSetBitMap(fileName); output(fileName.substring(0, 3)); } //Clean up fw.close(); }private void readAndSetBitMap(String fileName) throws IOException{ InputStream is; BufferedReader br; String line; is = new FileInputStream(fileName); br = new BufferedReader(new InputStreamReader(is)); while((line = br.readLine()) != null){ long tmp = Long.parseLong(line.substring(3, 11)); int mod = (int) tmp / RANGE; int offset = (int) tmp % RANGE; int bit = bitMap[(int)mod]; if (getBit(bit, offset) != 1){ bit = setBit(bit, offset, 1); bitMap[(int)mod] = bit; }else{ System.out.println(fileName.subSequence(0, 3) + toStringRep(tmp) + " duplicates!"); } } br.close(); is.close(); }private void output(String prefix) throws IOException{for (int i=0; i< bitMapLen; i++){ for (int offset=0; offset){ if (getBit(bitMap[i], offset) == 1){ long tmp = i * RANGE + offset; String cellphoneNum = toStringRep(tmp); fw.write(prefix+cellphoneNum); fw.write(" "); } } }for (int i=0; i< bitMapLen; i++){ bitMap[i] = 0; } }private String toStringRep(long tmp){ String cellphoneNum = String.valueOf(tmp); if (cellphoneNum.length()<8){ StringBuilder sb = new StringBuilder(); for (int j=0; j< 8 - cellphoneNum.length(); j++){ sb.append('0'); } sb.append(cellphoneNum); cellphoneNum = sb.toString(); } return cellphoneNum; }private int getBit(int signinNum, int bitIndex) { if( (signinNum & ( 1 << bitIndex)) != 0){ return 1; } return 0; }private int setBit(int num, int bitIndex, int zeroOrOne){ if (zeroOrOne == 1){ num = num | (1 << bitIndex); }else{ num = num - (1 << bitIndex); } return num; }//整体测试以下 @Test public void testSort() throws IOException{ long begin = System.currentTimeMillis(); new BigFileSort("cellphone.txt", "sortedCellphone.txt").sortAndOutput(); System.out.println(System.currentTimeMillis() - begin); } }
整个过程,事件花费大约60秒左右