如何通过PojoRepository在MarkLogic指定目录插入文档并自定义URI?
Got it, let's break down how to fix your URI formatting issue and answer your directory question when using MarkLogic's PojoRepository with Java.
1. Why you're seeing the encoded URI right now
By default, the PojoRepository uses a built-in UriMaker that:
- Takes the fully qualified class name of your POJO (like
com.PersonalizedMAM.Pojo.AssetPojo) as a prefix - URL-encodes the ID you provide (so
/images/Akhil.jpgbecomes%2Fimages%2FAkhil.jpg) - Appends
.jsonas the suffix
That's why you end up with com.PersonalizedMAM.Pojo.AssetPojo/%2Fimages%2FAkhil.jpg.json.
2. Customizing the URI format
You have two straightforward ways to get the URI structure you want:
Option A: Manually specify the full URI when inserting
Use the overloaded insert method that accepts a URI parameter. This lets you define exactly what the URI should be, no encoding surprises:
// Create your AssetPojo instance AssetPojo asset = new AssetPojo(); // Set your asset's properties here // Define your desired URI (pick either format you want) // Format 1: com.PersonalizedMAM.Pojo.AssetPojo/images/Akhil.jpg.json URI uriFormat1 = URI.create("com.PersonalizedMAM.Pojo.AssetPojo/images/Akhil.jpg.json"); // Format 2: /images/com.PersonalizedMAM.Pojo.AssetPojo/Akhil.jpg.json URI uriFormat2 = URI.create("/images/com.PersonalizedMAM.Pojo.AssetPojo/Akhil.jpg.json"); // Insert with your custom URI pojoRepository.insert(uriFormat1, asset); // or uriFormat2
Option B: Create a custom UriMaker for automatic URI generation
If you want this format to apply to all inserts without manually specifying the URI every time, implement a custom UriMaker. This replaces the default behavior:
// Initialize the repository with your custom UriMaker PojoRepository<AssetPojo, String> pojoRepository = client.newPojoRepository( AssetPojo.class, String.class, new UriMaker<AssetPojo, String>() { @Override public URI makeUri(AssetPojo pojo, String id) { // Build the URI exactly how you want it String className = AssetPojo.class.getName(); // Example for format 1: class name + path + id + .json return URI.create(className + "/" + id + ".json"); // Example for format 2: /images/ + class name + / + id + .json // return URI.create("/images/" + className + "/" + id + ".json"); } } ); // Now inserting with the ID will use your custom format (no encoding of slashes) pojoRepository.insert("/images/Akhil.jpg", asset);
3. Inserting into a specific "directory"
MarkLogic doesn't use physical directories—instead, directories are just logical groupings based on URI prefixes. So to insert a document into a specific directory, just make sure your URI starts with that directory path.
For example:
- To insert into
/images/, use a URI like/images/Akhil.jpg.json(or with the class name included as part of the path, like/images/com.PersonalizedMAM.Pojo.AssetPojo/Akhil.jpg.json) - You can even create nested directories by extending the URI prefix, e.g.,
/assets/images/com.PersonalizedMAM.Pojo.AssetPojo/Akhil.jpg.json
MarkLogic will recognize these as part of the respective directory structures, and you can query documents by directory later using that prefix.
内容的提问来源于stack exchange,提问作者akhilesh sabbisetti

