How can I retrieve a blob from a Result (ResultSet) returned by executing a query?

Hi

I’m saving a couple of blobs in a document.

String addJsonDocument(String docId, String json, Map<String, Blob> blobs) {
	MutableDocument doc = new MutableDocument(docId, json);
    blobs.forEach(doc::setBlob);//"key1",blob1;"key2",blob2
    database.save(doc)
}

And when I retrieve the document by “id” I can access the blobs without any problem:

Document docReaded = database.getDocument(id);
Blob blob = docReaded.getBlob("key1");

But when I retrieve several documents from a query I don’t know how to get the blob.

Query listQuery = QueryBuilder.select(SelectResult.all()).from(DataSource.database(database)).where(getWhereExpresion(filter));
ResultSet resultSet = listQuery.execute();
for (Result result : resultSet) {
	try {
		// In fact, HashMap<String, Object> dictFromJSONstring2 = objectMapper.readValue(arg.getValue().traverse(), HashMap.class);
		// Object att = dictFromJSONstring2.get("key1"); return a Map with blob properties, but the blob???
		objectMapper.readTree(result.toJSON()).fields()
				.forEachRemaining(arg -> ifs.add(objectMapper.convertValue(arg.getValue(), Info.class)));
	} catch (JsonProcessingException e) {
		.....
	}
}

I have noticed that the result is reported, as indicated in the documentation.

{
....
....
....
"key1": {
		"length": 2251,
		"content_type": "image/jpeg",
		"digest": "sha1-xXcns7UvPRgkU42niSw5sFk3xhY=",
		"@type": "blob"
	}
}

How can I get the blob?

The workaround I am doing is to get the “id” with the query and then launch another query for “id” to get the “Document” but it is slower.

Thank you in advance for any links, notes, advice.

You cannot get the blob directly from a query because, like the document itself, it is not present at that time. The only thing saved in the document itself is the metadata that you have discovered. The actual blob is saved external to the database so at some point you are going to have to do another read. However, I think you can probably take that result and pass it directly to the getBlob method. Note that this is not a method in the public API, but for various reasons it is unlikely to change.

1 Like

Thank you very much.
That’s what I was afraid of.

Note that the signature of getBlob was supported
getBlob(@NonNull Map<String, Object> props)
but I had no luck. Debugging for quite some time I thought I figured out why but I was still forced to do another read.

Thank you so much. Even though I’ve barely scratched the surface, it’s a great product.

That API should be able to give you a blob based on the information you presented in your previous post. If that is not working then I am curious why and if the logs say anything.