Este contenido no está disponible en el idioma seleccionado.
Chapter 4. Data conversion
Data Grid uses transcoders to convert data between various encodings that are identified by media types.
4.1. Hot Rod DataFormat API Copiar enlaceEnlace copiado en el portapapeles!
Read and write operations on remote caches via the Hot Rod endpoint use the client marshaller by default. Hot Rod provides a DataFormat
API for Java clients that you can use to perform cache operations with different media type encodings and/or marshallers.
Different marshallers for key and values
You can override marshallers for keys and values at run time.
For example, to bypass all serialization in the Hot Rod client and read the byte[]
array stored in the remote cache:
Using different marshallers and data formats for keys with keyMarshaller()
and keyType()
methods can interfere with client intelligence routing mechanisms, causing extra network hops within the Data Grid cluster. If performance is critical, you should use the same encoding for keys on the client and on the server.
Reading data in different encodings
Request and send data in different encodings specified by a org.infinispan.commons.dataconversion.MediaType
as follows:
Using custom value marshallers
You can use custom marshallers for values, as in the following example that returns values as org.codehaus.jackson.JsonNode
objects.
In this example, Data Grid Server handles the data conversion and throws an exception if it does not support the specified media type.
Returning values as XML
The following code snippet returns values as XML:
For example, the preceding get(key)
call returns values such as:
<?xml version="1.0" ?><string>Hello!</string>
<?xml version="1.0" ?><string>Hello!</string>
Reference
4.2. Converting data on demand with embedded caches Copiar enlaceEnlace copiado en el portapapeles!
Embedded caches have a default request encoding of application/x-java-object
and a storage encoding that corresponds to the media type that you configure for the cache. This means that Data Grid marshalls POJOs from the application to the storage encoding for the cache and then returns POJOs back to the application. In some complex scenarios you can use the AdvancedCache
API to change the default conversion to and from POJOs to other encodings.
The following example uses the withMediaType()
method to return values as application/json
on demand.
Advanced cache with MediaType
Value returned in JSON format
{ "_type":"org.infinispan.sample.Person", "name":"John", "surname":"Doe" }
{
"_type":"org.infinispan.sample.Person",
"name":"John",
"surname":"Doe"
}