Created
March 12, 2023 09:40
-
-
Save ourway/4fb428c27522ee9cf400422e5c83913d to your computer and use it in GitHub Desktop.
Find the shard_number based on the key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
# Get user input | |
key = input("Enter key: ") | |
num_shards = int(input("Enter number of shard databases: ")) | |
# Hash the key | |
hashed_key = hashlib.sha256(key.encode()).hexdigest() | |
# Convert the hash to an integer | |
hashed_int = int(hashed_key, 16) | |
# Determine the shard number | |
shard_number = hashed_int % num_shards | |
# Print the shard number and the hash key | |
print(f"The key '{key}' with hash '{hashed_key}' goes to shard {shard_number}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment