Find keywords in oversized files

Source: Internet
Author: User

A text file with 1 billion records that has been stored in the order of the keywords. Design the algorithm to quickly find a record of the keyword from the file.
Because it is already sequenced, you can use the binary lookup method to retrieve it.
Also because the file is too large, you can divide the file into 1000 copies of the child record set read 10,000 times, each read 1 billion/10,000 records;
Each time you read 1 billion/10,000 records, remember to save their keywords in the records array,
When a child Recordset is read, the keyword key>=records[0] is simply compared to the keyword key and the last record key of Records[i].

If Key<=key ', then key is retrieved using the binary lookup method in this records array, and if it is not retrieved, the lookup fails, returns false to the end of the loop, and the output true ends the loop.

If Key>key ', stating that it is not in the child Recordset, read the next child Recordset and loop the above steps.
If all read to the end of the file is not found, then output false;

The topic to record about 6,000, each read into 1000, simulated the problem;

/* Find the keyword in the file:
* There are about 6,000 to 7,000 records in the file
* Read 1000 records at a time, save their keywords in an array, find them
J is the number of records to read in; The last record array is labeled J-1
* Because the records in the document are orderly, when reading a recordset, there is bound to be key>=recored[0];
* Only need to determine the size of key and Recored[j-1],
* If key>record[j-1] Description keyword is not in this Recordset, go to the next loop.
* If KEY<=RECORD[J-1] then find in this recordset:
* If found, returns true to end loop,
* If not found, indicates that the entire file does not exist in this keyword, return false end loop
When the entire file is read, it is not found and returns false;

public static int recordnumber=6292;
public static file File=new file ("E:" +file.separator+ "program" +file.separator+ "Filesio" +file.separator+ "Text.txt") ;

*/


public static Boolean findkey (int key) throws ioexception{
BufferedReader reader=new BufferedReader (new FileReader (file));
for (int i=0;i<7;i++) {
Integer[] Record=new integer[1000];
Int J;
String s;
for (j=0;j<1000;j++) {
if ((S=reader.readline ())!=null) {
String[] Strarray=s.split ("");
Record[j]=integer.parseint (Strarray[0]);
}else{
Break
}
}
if (Key<=record[j-1]) {
Return Twodevidefindkey (Record,0,j-1,key);
}

}

return false;
}
Two-Part search method
public static Boolean Twodevidefindkey (integer[] array,int low,int high,int key) {
if (Low<=high) {
int mid=low+ (high-low)/2;
if (Array[mid]==key) return true;
if (Array[mid]>key) {
Return Twodevidefindkey (Array,low,mid-1,key);
}else{
Return Twodevidefindkey (Array,mid+1,high,key);
}
}else
return false;
}

-------------------------------------------file creation, writing


/*
* Create files and write 6,000 or so records:
* Each record line, the first character is a keyword, followed by the content.
* No record of the same keywords;
* Records are sorted by keyword
*/
public static void Createrecordfile () throws ioexception{
if (!file.exists ()) {
SYSTEM.OUT.PRINTLN ("File does not exist, will create file");
if (!file.createnewfile ()) {
System.out.println ("File already exists");
}else{
System.out.println ("file created");
}
}
PrintWriter out=new PrintWriter (new FileWriter (file));
Sortedset<integer> keyset=new treeset<integer> ();
for (int i=0;i<10000;i++) {
int key= (int) (Math.random () *10000);
if (!keyset.contains (key))
Keyset.add (key);
}
Recordnumber=keyset.size ();
Iterator<integer> Iter=keyset.iterator ();
int counter=0;
while (Iter.hasnext ()) {
int Key=iter.next ();
Out.println (key+ "" + "example" +counter++);
}
if (out!=null) out.close ();
System.out.println ("Number of records:" +recordnumber);
}

Find keywords in oversized files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.