Interface GenericRecord


public interface GenericRecord
A generic object interface that is returned to the user when the domain class can not be created from any of the distributed Hazelcast data structures like 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 Details

    • newBuilder

      @Nonnull GenericRecordBuilder newBuilder()
      Creates a GenericRecordBuilder 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 and GenericRecordBuilder.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

      @Nonnull GenericRecordBuilder newBuilderWithClone()
      Returned 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"))
               .setInt64("id", 4)
               .setString("surname", genericRecord.getString("surname"))
               .setInt32("age", genericRecord.getInt32("age"))
               .build();
       
      `newBuilderWithClone` used as follows:
      
       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

      @Nonnull Set<String> getFieldNames()
      Returns:
      set of field names of this GenericRecord
    • getFieldKind

      @Nonnull FieldKind getFieldKind(@Nonnull String fieldName)
      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

      boolean getBoolean(@Nonnull String fieldName)
      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

      char getChar(@Nonnull String fieldName)
      Supported only for Portable. Not applicable for Compact
      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

      byte getInt8(@Nonnull String fieldName)
      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

      short getInt16(@Nonnull String fieldName)
      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

      int getInt32(@Nonnull String fieldName)
      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

      long getInt64(@Nonnull String fieldName)
      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

      float getFloat32(@Nonnull String fieldName)
      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

      double getFloat64(@Nonnull String fieldName)
      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

      @Nullable String getString(@Nonnull String fieldName)
      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

      @Nullable BigDecimal getDecimal(@Nonnull String fieldName)
      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

      @Nullable LocalTime getTime(@Nonnull String fieldName)
      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

      @Nullable LocalDate getDate(@Nonnull String fieldName)
      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

      @Nullable LocalDateTime getTimestamp(@Nonnull String fieldName)
      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

      @Nullable OffsetDateTime getTimestampWithTimezone(@Nonnull String fieldName)
      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

      @Nullable GenericRecord getGenericRecord(@Nonnull String fieldName)
      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

      @Nullable boolean[] getArrayOfBoolean(@Nonnull String fieldName)
      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

      @Nullable char[] getArrayOfChar(@Nonnull String fieldName)
      Supported only for Portable. Not applicable for Compact
      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

      @Nullable byte[] getArrayOfInt8(@Nonnull String fieldName)
      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

      @Nullable short[] getArrayOfInt16(@Nonnull String fieldName)
      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

      @Nullable int[] getArrayOfInt32(@Nonnull String fieldName)
      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

      @Nullable long[] getArrayOfInt64(@Nonnull String fieldName)
      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

      @Nullable float[] getArrayOfFloat32(@Nonnull String fieldName)
      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

      @Nullable double[] getArrayOfFloat64(@Nonnull String fieldName)
      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

      @Nullable String[] getArrayOfString(@Nonnull String fieldName)
      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

      @Nullable BigDecimal[] getArrayOfDecimal(@Nonnull String fieldName)
      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

      @Nullable LocalTime[] getArrayOfTime(@Nonnull String fieldName)
      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

      @Nullable LocalDate[] getArrayOfDate(@Nonnull String fieldName)
      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

      @Nullable LocalDateTime[] getArrayOfTimestamp(@Nonnull String fieldName)
      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

      @Nullable OffsetDateTime[] getArrayOfTimestampWithTimezone(@Nonnull String fieldName)
      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

      @Nullable GenericRecord[] getArrayOfGenericRecord(@Nonnull String fieldName)
      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

      @Nullable Boolean getNullableBoolean(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Byte getNullableInt8(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Short getNullableInt16(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Integer getNullableInt32(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Long getNullableInt64(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Float getNullableFloat32(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Double getNullableFloat64(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Boolean[] getArrayOfNullableBoolean(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Byte[] getArrayOfNullableInt8(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Short[] getArrayOfNullableInt16(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Integer[] getArrayOfNullableInt32(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Long[] getArrayOfNullableInt64(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Float[] getArrayOfNullableFloat32(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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

      @Nullable Double[] getArrayOfNullableFloat64(@Nonnull String fieldName)
      Supported only for Compact. Not applicable to Portable.
      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.