Generate UUID in Clojure

Since Clojure is a JVM-based language, we can use the same libraries and methods we would have used in Java to generate UUIDs, as long as we do so in Clojure syntax. The following code snipped produces a valid version 4 UUID:


(import java.util.UUID)
(UUID/randomUUID)

If you need to generate a specific UUID version, you need to use a third-party library such as uuid-creator which offers more flexible options:


(import com.github.f4b6a3.uuid.UuidCreator)
(import com.github.f4b6a3.uuid.enums.UuidLocalDomain)
(import com.github.f4b6a3.uuid.enums.UuidNamespace)

;;generate UUID version 1
(UuidCreator/getTimeBased)

;;generate UUID version 2
(UuidCreator/getDceSecurity UuidLocalDomain/LOCAL_DOMAIN_PERSON 1234)

;;generate UUID version 3
(UuidCreator/getNameBasedMd5 UuidNamespace/NAMESPACE_URL "https://github.com/")

;;generate UUID version 4
(UuidCreator/getRandomBased)

;;generate UUID version 5
(UuidCreator/getNameBasedSha1 UuidNamespace/NAMESPACE_URL "https://github.com/")

;;generate UUID version 6
(UuidCreator/getTimeOrdered)

;;generate UUID version 7
(UuidCreator/getTimeOrderedEpoch)

How do I generate UUID in...

Find out how to generate UUIDs in your favorite language here: Bash, Groovy, Kotlin, Python, Scala, ClojureScript, Java, JavaScript, or return to Online UUID Generator