Interface GenericRecord


@Beta
public interface GenericRecord
A generic object interface that is returned to 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 distributed executor service or EntryProcessors, you may need to access to the domain object. In case class of the domain object is not available on the cluster, GenericRecord allows to access, read and write the objects back without the class of the domain object on the classpath. Here is an read example with EntryProcessor:

 map.executeOnKey(key, (EntryProcessor) 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 back to the map:
 map.executeOnKey("key", (EntryProcessor) entry -> {
             GenericRecord genericRecord = (GenericRecord) entry.getValue();
             GenericRecord modifiedGenericRecord = genericRecord.cloneWithBuilder()
                     .setInt("age",22).build();
             entry.setValue(modifiedGenericRecord);
             return null;
         });
 

GenericRecord also allows to read 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 the objects back to the cluster without needing the classes of the domain objects on the classpath.

Currently this is valid for Portable objects.

Since:
4.1
  • Method Details

    • newBuilder

      Creates a 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.

      Returns:
      an empty generic record builder with same class definition as this one
    • cloneWithBuilder

      @Nonnull GenericRecordBuilder cloneWithBuilder()
      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"))
                               .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();
       
      Returns:
      a generic record builder with same class definition as this one and populated with same values.
    • getFieldNames

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

      @Nonnull FieldType getFieldType​(@Nonnull String fieldName)
      Parameters:
      fieldName - the name of the field
      Returns:
      field type for the given field name
      Throws:
      IllegalArgumentException - if the field name does not exist in the class definition
    • hasField

      boolean hasField​(@Nonnull String fieldName)
      Parameters:
      fieldName - the name of the field
      Returns:
      true if field exists in the definition of the class. Note that returns true even if the field is null.
    • 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 class definition or the type of the field does not match the one in the class definition.
    • getByte

      byte getByte​(@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 class definition or the type of the field does not match the one in the class definition.
    • getChar

      char getChar​(@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 class definition or the type of the field does not match the one in the class definition.
    • getDouble

      double getDouble​(@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 class definition or the type of the field does not match the one in the class definition.
    • getFloat

      float getFloat​(@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 class definition or the type of the field does not match the one in the class definition.
    • getInt

      int getInt​(@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 class definition or the type of the field does not match the one in the class definition.
    • getLong

      long getLong​(@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 class definition or the type of the field does not match the one in the class definition.
    • getShort

      short getShort​(@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 class definition or the type of the field does not match the one in the 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 class definition or the type of the field does not match the one in the 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 class definition or the type of the field does not match the one in the 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 class definition or the type of the field does not match the one in the 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 class definition or the type of the field does not match the one in the 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 class definition or the type of the field does not match the one in the 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 class definition or the type of the field does not match the one in the 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 class definition or the type of the field does not match the one in the class definition.
    • getBooleanArray

      @Nullable boolean[] getBooleanArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getByteArray

      @Nullable byte[] getByteArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getCharArray

      @Nullable char[] getCharArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getDoubleArray

      @Nullable double[] getDoubleArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getFloatArray

      @Nullable float[] getFloatArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getIntArray

      @Nullable int[] getIntArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getLongArray

      @Nullable long[] getLongArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getShortArray

      @Nullable short[] getShortArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getStringArray

      @Nullable String[] getStringArray​(@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 class definition or the type of the field does not match the one in the class definition.
    • getDecimalArray

      @Nullable BigDecimal[] getDecimalArray​(@Nonnull String fieldName)
      Parameters:
      fieldName - the name of the field
      Returns:
      array of Decimal's as BigDecimal[]
      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.
      See Also:
      getDecimal(String)
    • getTimeArray

      @Nullable LocalTime[] getTimeArray​(@Nonnull String fieldName)
      Parameters:
      fieldName - the name of the field
      Returns:
      array of Time's as LocalTime[]
      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.
      See Also:
      getTime(String)
    • getDateArray

      @Nullable LocalDate[] getDateArray​(@Nonnull String fieldName)
      Parameters:
      fieldName - the name of the field
      Returns:
      array of Date's to LocalDate[]
      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.
      See Also:
      getDate(String)
    • getTimestampArray

      @Nullable LocalDateTime[] getTimestampArray​(@Nonnull String fieldName)
      Parameters:
      fieldName - the name of the field
      Returns:
      array of Timestamp's as LocalDateTime[]
      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.
      See Also:
      getTimestamp(String)
    • getTimestampWithTimezoneArray

      @Nullable OffsetDateTime[] getTimestampWithTimezoneArray​(@Nonnull String fieldName)
      Parameters:
      fieldName - the name of the field
      Returns:
      array of TimestampWithTimezone's as OffsetDateTime[]
      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.
      See Also:
      getTimestampWithTimezone(String)
    • getGenericRecordArray

      @Nullable GenericRecord[] getGenericRecordArray​(@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 class definition or the type of the field does not match the one in the class definition.