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.readInt("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()
                     .writeInt("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 be 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 GenericRecord.Builder 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()
            .writeUTF("name", "bar")
            .writeInt("id", 4).build();
      
       

      see GenericRecord.Builder.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 GenericRecord.Builder cloneWithBuilder()
      Returned GenericRecord.Builder 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()
                               .writeUTF("name", genericRecord.readUTF("name"))
                               .writeLong("id", 4)
                               .writeUTF("surname", genericRecord.readUTF("surname"))
                               .writeInt("age", genericRecord.readInt("age")).build();
       
      `cloneWithBuilder` used as follows:
           GenericRecord modifiedGenericRecord = genericRecord.cloneWithBuilder().writeInt("id", 4).build();
       
      Returns:
      a generic record builder with same class definition as this one and populated with same values.
    • 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.
    • readBoolean

      boolean readBoolean​(@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.
    • readByte

      byte readByte​(@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.
    • readChar

      char readChar​(@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.
    • readDouble

      double readDouble​(@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.
    • readFloat

      float readFloat​(@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.
    • readInt

      int readInt​(@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.
    • readLong

      long readLong​(@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.
    • readShort

      short readShort​(@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.
    • readUTF

      @Nullable String readUTF​(@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.
    • readGenericRecord

      @Nullable GenericRecord readGenericRecord​(@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.
    • readBooleanArray

      @Nullable boolean[] readBooleanArray​(@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.
    • readByteArray

      @Nullable byte[] readByteArray​(@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.
    • readCharArray

      @Nullable char[] readCharArray​(@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.
    • readDoubleArray

      @Nullable double[] readDoubleArray​(@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.
    • readFloatArray

      @Nullable float[] readFloatArray​(@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.
    • readIntArray

      @Nullable int[] readIntArray​(@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.
    • readLongArray

      @Nullable long[] readLongArray​(@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.
    • readShortArray

      @Nullable short[] readShortArray​(@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.
    • readUTFArray

      @Nullable String[] readUTFArray​(@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.
    • readGenericRecordArray

      @Nullable GenericRecord[] readGenericRecordArray​(@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.