如何在Google Healthcare API FHIR中使用numeric id?求助将UUID转换为numeric id的实现方法
Hey there, let's tackle your two questions: converting UUIDs to numeric IDs, and using those numeric IDs with the Google Healthcare API FHIR service.
UUIDs are 128-bit values, so you can directly convert them to a numeric (integer) representation. The key here is to handle the full 128 bits to avoid collisions—truncating to a smaller bit size (like 64 bits) could lead to duplicate IDs, which you definitely want to avoid.
Here are examples in common languages:
Python
Use the built-in uuid module to parse the UUID and convert it to an integer, then cast to a string if you need a numeric string for FHIR:
import uuid # Example UUID target_uuid = "550e8400-e29b-41d4-a716-446655440000" uuid_obj = uuid.UUID(target_uuid) # Convert to 128-bit integer numeric_id = int(uuid_obj) # Or get a string representation of the numeric ID numeric_id_str = str(numeric_id)
Java
Use java.util.UUID to extract the most and least significant bits, or convert directly to a BigInteger for full 128-bit support:
import java.util.UUID; import java.math.BigInteger; public class UuidToNumeric { public static void main(String[] args) { UUID uuid = UUID.fromString("550e8400-e29b-41d4-a716-446655440000"); // Convert to BigInteger (full 128-bit value) BigInteger numericId = new BigInteger(1, uuid.toString().replace("-", ""), 16); String numericIdStr = numericId.toString(); System.out.println(numericIdStr); } }
Important note: If you’re working with systems that only support 64-bit integers, you’ll have to accept collision risk or use a hashing approach (but hashing can also cause collisions). Stick to full 128-bit numeric values whenever possible.
The Google Healthcare API FHIR service allows you to use numeric IDs for resources—since FHIR resource IDs are strings, you just need to pass the numeric ID as a string (e.g., "1234567890123456789").
Creating a Resource with a Numeric ID
To create a resource with your custom numeric ID, use a PUT request (instead of POST, which lets the server generate an ID). Here’s how to do it with a REST call:
Construct the endpoint URL with your numeric ID:
https://healthcare.googleapis.com/v1/projects/{PROJECT_ID}/locations/{LOCATION}/datasets/{DATASET_ID}/fhirStores/{FHIR_STORE_ID}/fhir/{RESOURCE_TYPE}/{NUMERIC_ID}Send a
PUTrequest with your FHIR resource data (no need to include theidfield in the request body unless you want to explicitly set it—though the URL path takes precedence):
Example Python request using requests:
import requests # Your numeric ID (from the UUID conversion step) numeric_id_str = "12345678901234567890123456789012" # FHIR endpoint endpoint = f"https://healthcare.googleapis.com/v1/projects/your-project/locations/us-central1/datasets/your-dataset/fhirStores/your-fhir-store/fhir/Patient/{numeric_id_str}" headers = { "Content-Type": "application/fhir+json", "Authorization": "Bearer YOUR_ACCESS_TOKEN" } # Sample Patient resource patient_data = { "resourceType": "Patient", "name": [{"family": "Doe", "given": ["Jane"]}], "birthDate": "1985-05-15", "gender": "female" } response = requests.put(endpoint, json=patient_data, headers=headers) if response.status_code == 201: print("Resource created successfully!") else: print(f"Error: {response.status_code}") print(response.json())
Key Considerations
- Uniqueness: Ensure your numeric ID is unique across the resource type—just like UUIDs, duplicate IDs will cause conflicts when creating resources.
- ID Length: The Google Healthcare API allows FHIR resource IDs up to 64 characters. A full 128-bit integer as a string is 39 characters long, which fits comfortably.
- Updating Resources: Once you create a resource with a numeric ID, you can use the same ID in
PUTorPATCHrequests to update it, just like any other FHIR resource ID.
内容的提问来源于stack exchange,提问作者Clesio SAMA

