Java programming implementation uses hash method to cut files

  • 2020-12-09 00:54:04
  • OfStack

The hashing algorithm transforms an input of any length (also known as premapping, ES2en-ES3en) into an output of a fixed length, which is the hash value. This transformation is a compression mapping, that is, the space of the hash value is usually much smaller than the space of the input, different inputs may be hashed into the same output, so it is not possible to only determine the input value of 1 from the hash value. Simply put, it is a function that compresses a message of any length into a message digest of a fixed length.

If there are large data files (url per action, ip per word, etc.) in G, the processing should be done by sharding first. Ordinary sharding method is directly based on the number of data shards, the resulting file size is similar.

But sometimes you need to put the same data in the same file. hash segmentation can be used.


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;  
  }  
} 

conclusion

That's all for Java programming and hash method to cut files, I hope it will help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out.


Related articles: