Interface GenericRecord
IMap
, IQueue
etc.
On remote calls like in the distributed executor service or EntryProcessors, you may need access to the domain object. In case the class of the domain object is not available on the cluster, GenericRecord allows you to read and write the object without the domain class on the classpath. Here is an example with EntryProcessor:
map.executeOnKey(key, (EntryProcessor<Object, Object, Object>) entry -> {
Object value = entry.getValue();
GenericRecord genericRecord = (GenericRecord) value;
int id = genericRecord.getInt32("id");
return null;
});
Another example with EntryProcessor to demonstrate how to read, modify and
set the value back to the map:
map.executeOnKey("key", (EntryProcessor<Object, Object, Object>) entry -> {
GenericRecord genericRecord = (GenericRecord) entry.getValue();
GenericRecord modifiedGenericRecord = genericRecord.newBuilderWithClone()
.setInt32("age", 22)
.build();
entry.setValue(modifiedGenericRecord);
return null;
});
GenericRecord also allows reading from a cluster without having the classes
on the client side. For Portable
, when PortableFactory
is not
provided in the config at the start, a
HazelcastSerializationException
was thrown stating that a factory
could not be found. Starting from 4.1, the objects will be returned as
GenericRecord
. This way, the clients can read and write objects back
to the cluster without the need to have the domain classes on the classpath.
Currently, this is valid for Portable
and
Compact
serializable
objects.
- Since:
- 5.2
-
Method Summary
Modifier and TypeMethodDescriptionboolean[]
getArrayOfBoolean
(String fieldName) char[]
getArrayOfChar
(String fieldName) Supported only forPortable
.getArrayOfDate
(String fieldName) getArrayOfDecimal
(String fieldName) float[]
getArrayOfFloat32
(String fieldName) double[]
getArrayOfFloat64
(String fieldName) getArrayOfGenericRecord
(String fieldName) short[]
getArrayOfInt16
(String fieldName) int[]
getArrayOfInt32
(String fieldName) long[]
getArrayOfInt64
(String fieldName) byte[]
getArrayOfInt8
(String fieldName) Boolean[]
getArrayOfNullableBoolean
(String fieldName) Supported only forCompact
.Float[]
getArrayOfNullableFloat32
(String fieldName) Supported only forCompact
.Double[]
getArrayOfNullableFloat64
(String fieldName) Supported only forCompact
.Short[]
getArrayOfNullableInt16
(String fieldName) Supported only forCompact
.Integer[]
getArrayOfNullableInt32
(String fieldName) Supported only forCompact
.Long[]
getArrayOfNullableInt64
(String fieldName) Supported only forCompact
.Byte[]
getArrayOfNullableInt8
(String fieldName) Supported only forCompact
.String[]
getArrayOfString
(String fieldName) getArrayOfTime
(String fieldName) getArrayOfTimestamp
(String fieldName) getArrayOfTimestampWithTimezone
(String fieldName) boolean
getBoolean
(String fieldName) char
Supported only forPortable
.getDecimal
(String fieldName) getFieldKind
(String fieldName) Returns the kind of the field for the given field name.float
getFloat32
(String fieldName) double
getFloat64
(String fieldName) getGenericRecord
(String fieldName) short
int
long
byte
getNullableBoolean
(String fieldName) Supported only forCompact
.getNullableFloat32
(String fieldName) Supported only forCompact
.getNullableFloat64
(String fieldName) Supported only forCompact
.getNullableInt16
(String fieldName) Supported only forCompact
.getNullableInt32
(String fieldName) Supported only forCompact
.getNullableInt64
(String fieldName) Supported only forCompact
.getNullableInt8
(String fieldName) Supported only forCompact
.getTimestamp
(String fieldName) getTimestampWithTimezone
(String fieldName) Creates aGenericRecordBuilder
allows to create a new object.ReturnedGenericRecordBuilder
can be used to have exact copy and also just to update a couple of fields.
-
Method Details
-
newBuilder
Creates aGenericRecordBuilder
allows to create a new object. This method is a convenience method to get a builder, without creating the schema/class definition for this type. Here you can see a new object is constructed from an existing GenericRecord with its schema/class definition:GenericRecord newGenericRecord = genericRecord.newBuilder() .setString("name", "bar") .setInt32("id", 4) .build();
See
GenericRecordBuilder.portable(ClassDefinition)
to create a GenericRecord in Portable format with a different class definition andGenericRecordBuilder.compact(String)
to create a GenericRecord in Compact format with a different schema.- Returns:
- an empty generic record builder with same class definition as this one
-
newBuilderWithClone
ReturnedGenericRecordBuilder
can be used to have exact copy and also just to update a couple of fields. By default, it will copy all the fields. So instead of following where only the `id` field is updated,
`newBuilderWithClone` used as follows:GenericRecord modifiedGenericRecord = genericRecord.newBuilder() .setString("name", genericRecord.getString("name")) .setInt64("id", 4) .setString("surname", genericRecord.getString("surname")) .setInt32("age", genericRecord.getInt32("age")) .build();
GenericRecord modifiedGenericRecord = genericRecord.newBuilderWithClone() .setInt32("id", 4) .build();
- Returns:
- a generic record builder with same schema/class definition as this one and populated with same values.
-
getFieldNames
- Returns:
- set of field names of this GenericRecord
-
getFieldKind
Returns the kind of the field for the given field name.If the field with the given name does not exist,
FieldKind.NOT_AVAILABLE
is returned.This method can be used to check the existence of a field, which can be useful when the class is evolved.
- Parameters:
fieldName
- name of the field.- Returns:
- kind of the field
-
getBoolean
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getChar
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the class definition or the type of the field does not match the one in the class definition.
-
getInt8
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getInt16
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getInt32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getInt64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getFloat32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getFloat64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getString
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getDecimal
- Parameters:
fieldName
- the name of the field- Returns:
- decimal which is arbitrary precision and scale floating-point
number as
BigDecimal
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getTime
- Parameters:
fieldName
- the name of the field- Returns:
- time field consisting of hour, minute, seconds and nanos parts as
LocalTime
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getDate
- Parameters:
fieldName
- the name of the field- Returns:
- date field consisting of year, month of the year and day of the
month as
LocalDate
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getTimestamp
- Parameters:
fieldName
- the name of the field- Returns:
- timestamp field consisting of year, month of the year, day of the
month, hour, minute, seconds, nanos parts as
LocalDateTime
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getTimestampWithTimezone
- Parameters:
fieldName
- the name of the field- Returns:
- timestamp with timezone field consisting of year, month of the
year, day of the month, offset seconds, hour, minute, seconds, nanos
parts as
OffsetDateTime
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getGenericRecord
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfBoolean
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfChar
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the class definition or the type of the field does not match the one in the class definition.
-
getArrayOfInt8
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfInt16
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfInt32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfInt64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfFloat32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfFloat64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfString
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getArrayOfDecimal
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.- See Also:
-
getArrayOfTime
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.- See Also:
-
getArrayOfDate
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.- See Also:
-
getArrayOfTimestamp
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.- See Also:
-
getArrayOfTimestampWithTimezone
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.- See Also:
-
getArrayOfGenericRecord
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema/class definition or the type of the field does not match the one in the schema/class definition.
-
getNullableBoolean
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getNullableInt8
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getNullableInt16
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getNullableInt32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getNullableInt64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getNullableFloat32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getNullableFloat64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getArrayOfNullableBoolean
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getArrayOfNullableInt8
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getArrayOfNullableInt16
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getArrayOfNullableInt32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getArrayOfNullableInt64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getArrayOfNullableFloat32
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-
getArrayOfNullableFloat64
- Parameters:
fieldName
- the name of the field- Returns:
- the value of the field
- Throws:
HazelcastSerializationException
- if the field name does not exist in the schema or the type of the field does not match the one in the schema.
-