ID modulo shard

Source: Internet
Author: User
Tags hash

Based on the ID of the module shard, in the big data storage when used to,

The following is the modulo code:

public static void Main (string[] args) {
		//modulo method one for
		(int i = 0; i < 10000; i++) {
			//Fetch hash
			String key = String.valueof (i);
			Long value = long.valueof (Key.hashcode ());
	        int hash= (int) (value ^ (value >>>));  
	        int index=hash%10;  
	        SYSTEM.OUT.PRINTLN (index);
		}
		
		Modulo method two for
		(int i = 0; i < 10000; i++) {
			//Fetch hash
			String key = string.valueof (i);
			A power square (2,8,16,32)
	        int queuesize = 8 with a quantity of 2;
	        int h;
	        int hash = (key = = null)? 0: (H = key.hashcode ()) ^ (h >>> +);
	        int index = (queueSize-1) & hash;
	        SYSTEM.OUT.PRINTLN (index);
		}
	}

PS: The above with two methods to take the mold, after testing can be used, the first method is divided into 10 parts according to the number of storage, the second method is divided into 8 parts according to the number of storage,

Index is the corresponding slice index, whether it is a database or queue, cache, etc., can be used.

The reason why Hashcode is taken in the above code is because the data type is not deterministic, so we take hashcode to calculate




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.