@Beta public 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.getInt("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.cloneWithBuilder()
.setInt("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
objects.
@Nonnull GenericRecordBuilder newBuilder()
GenericRecordBuilder
allows to create a new object. This method is a convenience method to get a builder,
without creating the class definition for this type. Here you can see a new object is constructed from an existing
GenericRecord with its class definition:
GenericRecord newGenericRecord = genericRecord.newBuilder() .setString("name", "bar") .setInt("id", 4).build();
see GenericRecordBuilder.portable(ClassDefinition)
to create a GenericRecord in Portable format
with a different class definition.
@Nonnull GenericRecordBuilder cloneWithBuilder()
GenericRecordBuilder
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,
GenericRecord modifiedGenericRecord = genericRecord.newBuilder() .setString("name", genericRecord.getString("name")) .setLong("id", 4) .setString("surname", genericRecord.getString("surname")) .setInt("age", genericRecord.getInt("age")).build();`cloneWithBuilder` used as follows:
GenericRecord modifiedGenericRecord = genericRecord.cloneWithBuilder().setInt("id", 4).build();
@Nonnull Set<String> getFieldNames()
@Nonnull FieldType getFieldType(@Nonnull String fieldName)
fieldName
- the name of the fieldIllegalArgumentException
- if the field name does not exist in the class definitionboolean hasField(@Nonnull String fieldName)
fieldName
- the name of the fieldboolean getBoolean(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.byte getByte(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.char getChar(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.double getDouble(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.float getFloat(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.int getInt(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.long getLong(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.short getShort(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable String getString(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable BigDecimal getDecimal(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable LocalTime getTime(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable LocalDate getDate(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable LocalDateTime getTimestamp(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable OffsetDateTime getTimestampWithTimezone(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable GenericRecord getGenericRecord(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable boolean[] getBooleanArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable byte[] getByteArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable char[] getCharArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable double[] getDoubleArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable float[] getFloatArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable int[] getIntArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable long[] getLongArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable short[] getShortArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable String[] getStringArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.@Nullable BigDecimal[] getDecimalArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.getDecimal(String)
@Nullable LocalTime[] getTimeArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.getTime(String)
@Nullable LocalDate[] getDateArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.getDate(String)
@Nullable LocalDateTime[] getTimestampArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.getTimestamp(String)
@Nullable OffsetDateTime[] getTimestampWithTimezoneArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.getTimestampWithTimezone(String)
@Nullable GenericRecord[] getGenericRecordArray(@Nonnull String fieldName)
fieldName
- the name of the fieldHazelcastSerializationException
- 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.Copyright © 2021 Hazelcast, Inc.. All rights reserved.