How to use the command genid (with examples)
- Linux
- December 25, 2023
The genid command is a versatile tool for generating various types of IDs such as snowflakes, UUIDs, and GAIDs (Generic Anonymous IDs). It provides a simple way to generate unique identifiers for different use cases.
Use case 1: Generate a UUIDv4
Code:
genid uuid
Motivation: Generating UUIDv4 (Universally Unique Identifier) is often required in applications that need to generate a unique identifier. This can be useful in database records, distributed systems, or anything that requires a unique identifier. UUIDv4 is randomly generated and provides a high probability of uniqueness.
Explanation: The genid uuid
command generates a UUIDv4.
Example output:
f47ac10b-58cc-4372-a567-0e02b2c3d479
Use case 2: Generate a UUIDv5 using a namespace UUID and a specific name
Code:
genid uuidv5 {ce598faa-8dd0-49ee-8525-9e24fff71dca} name
Motivation: UUIDv5 is generated using a namespace UUID and a specific name, which provides a way to generate a consistent UUID based on a given name and namespace. This can be useful in scenarios where unique identifiers need to be generated based on specific inputs.
Explanation: The genid uuidv5
command is followed by a namespace UUID and a specific name. The namespace UUID is enclosed in curly braces {}. The generated UUIDv5 is based on the combination of the namespace UUID and the name.
Example output:
c3e32e9e-5d28-5416-aaf0-559a16e32767
Use case 3: Generate a Discord Snowflake, without a trailing newline (useful in shell scripts)
Code:
genid --script snowflake
Motivation: Generating a Discord Snowflake without a trailing newline is useful when incorporating the generated snowflake in shell scripts where a trailing newline can cause issues or interfere with further operations.
Explanation: The genid --script snowflake
command generates a Discord Snowflake without a trailing newline. The --script
option ensures that the output doesn’t include a newline character.
Example output:
722973948509769472
Use case 4: Generate a Generic Anonymous ID with a specific “real ID”
Code:
genid gaid real_id
Motivation: A Generic Anonymous ID (GAID) is often used to anonymize real IDs while still maintaining a relationship between the two. This can be useful in scenarios where data privacy is important, and the association between real IDs and GAIDs needs to be maintained.
Explanation: The genid gaid
command is followed by a specific “real ID”. The generated GAID is based on the provided “real ID”.
Example output:
gaid:5f536583f44873.9025036136
Use case 5: Generate a Snowflake with the epoch set to a specific date
Code:
genid snowflake --epoch=unix_epoch_time
Motivation: Snowflakes are unique IDs used in distributed systems, and they include a timestamp component. Generating a Snowflake with the epoch set at a specific date can be useful in scenarios where the epoch needs to be aligned with a specific point in time.
Explanation: The genid snowflake --epoch=unix_epoch_time
command generates a Snowflake with the epoch set to a specific date. The --epoch
option is used to specify the desired epoch time. The unix_epoch_time
should be replaced with the actual Unix epoch time value.
Example output:
1189782632937733632
Conclusion:
The genid command provides a convenient way to generate various types of IDs such as UUIDv4, UUIDv5, Discord Snowflake, GAID, and Snowflake with a specific epoch. By understanding and utilizing the different use cases, developers can generate unique identifiers for their specific requirements.