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 Detail

      • 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
        java.util.Set<java.lang.String> getFieldNames()
        Returns:
        set of field names of this GenericRecord
      • getFieldKind

        @Nonnull
        FieldKind getFieldKind​(@Nonnull
                               java.lang.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
                           java.lang.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
                     java.lang.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
                     java.lang.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
                       java.lang.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
                     java.lang.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
                      java.lang.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
                         java.lang.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
                          java.lang.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
        java.lang.String getString​(@Nonnull
                                   java.lang.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
        java.math.BigDecimal getDecimal​(@Nonnull
                                        java.lang.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
        java.time.LocalTime getTime​(@Nonnull
                                    java.lang.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
        java.time.LocalDate getDate​(@Nonnull
                                    java.lang.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
        java.time.LocalDateTime getTimestamp​(@Nonnull
                                             java.lang.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
        java.time.OffsetDateTime getTimestampWithTimezone​(@Nonnull
                                                          java.lang.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
                                       java.lang.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
                                    java.lang.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
                              java.lang.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
                              java.lang.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
                                java.lang.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
                              java.lang.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
                               java.lang.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
                                  java.lang.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
                                   java.lang.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
        java.lang.String[] getArrayOfString​(@Nonnull
                                            java.lang.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
        java.math.BigDecimal[] getArrayOfDecimal​(@Nonnull
                                                 java.lang.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:
        getDecimal(String)
      • getArrayOfTime

        @Nullable
        java.time.LocalTime[] getArrayOfTime​(@Nonnull
                                             java.lang.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:
        getTime(String)
      • getArrayOfDate

        @Nullable
        java.time.LocalDate[] getArrayOfDate​(@Nonnull
                                             java.lang.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:
        getDate(String)
      • getArrayOfTimestamp

        @Nullable
        java.time.LocalDateTime[] getArrayOfTimestamp​(@Nonnull
                                                      java.lang.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:
        getTimestamp(String)
      • getArrayOfTimestampWithTimezone

        @Nullable
        java.time.OffsetDateTime[] getArrayOfTimestampWithTimezone​(@Nonnull
                                                                   java.lang.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:
        getTimestampWithTimezone(String)
      • getArrayOfGenericRecord

        @Nullable
        GenericRecord[] getArrayOfGenericRecord​(@Nonnull
                                                java.lang.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
        java.lang.Boolean getNullableBoolean​(@Nonnull
                                             java.lang.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
        java.lang.Byte getNullableInt8​(@Nonnull
                                       java.lang.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
        java.lang.Short getNullableInt16​(@Nonnull
                                         java.lang.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
        java.lang.Integer getNullableInt32​(@Nonnull
                                           java.lang.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
        java.lang.Long getNullableInt64​(@Nonnull
                                        java.lang.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
        java.lang.Float getNullableFloat32​(@Nonnull
                                           java.lang.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
        java.lang.Double getNullableFloat64​(@Nonnull
                                            java.lang.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
        java.lang.Boolean[] getArrayOfNullableBoolean​(@Nonnull
                                                      java.lang.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
        java.lang.Byte[] getArrayOfNullableInt8​(@Nonnull
                                                java.lang.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
        java.lang.Short[] getArrayOfNullableInt16​(@Nonnull
                                                  java.lang.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
        java.lang.Integer[] getArrayOfNullableInt32​(@Nonnull
                                                    java.lang.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
        java.lang.Long[] getArrayOfNullableInt64​(@Nonnull
                                                 java.lang.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
        java.lang.Float[] getArrayOfNullableFloat32​(@Nonnull
                                                    java.lang.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
        java.lang.Double[] getArrayOfNullableFloat64​(@Nonnull
                                                     java.lang.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.