tencent cloud

Feedback

Java Code Mode

Last updated: 2023-08-03 17:51:33

    To make it easier for users experienced in Java to connect to iPaaS, the Dataway code mode supports Java scripts.

    Use of IDE

    1. Hover over any Dataway textbox, and the mode selection buttons will pop up automatically. Click Code to enter the code mode.
    2. Click the textbox, and the code editor will pop up. Click Java to enter the Java script editor.
    3. After editing the script, click Confirm.

    The Java code mode supports data reference on the flow data panel.

    Script Structure

    A Java script must be in compliance with JDK 8 syntax.

    The class name must be Handler, and you must define a function with the signature Object eval(Message msg) as the entry function.

    import com.tencent.ipaas.dataway.common.message.Message; 
    import com.tencent.ipaas.dataway.common.message.DataRef; 
    

    You can add other import statements based on required data types.

    // The following `import` statements are fixed and cannot be deleted.
    import com.tencent.ipaas.dataway.common.message.Message;
    import com.tencent.ipaas.dataway.common.message.DataRef; 
    
    /**
     * Entry class of `dataway-java`, which must be named `Handler`
     */
    public class Handler {
        /**
         * Entry function, whose signature must be `Object eval(Message msg)`
         * @param msg Input a `Message` object
         * @return Any data object supported by Dataway
         */
        public Object eval(Message msg) {
            return msg.getPayload();
        }
    }
    

    Data Types

    The Java code mode supports various data types, making it easy for you to manipulate different data.

    Type Description Corresponding Python Type Example Method
    null `null` in Java. None null -
    String String, i.e., native `String` type in Java. str "abc" -
    Boolean Boolean, i.e, native `bool` type in Java. bool true/false -
    float/Float Float, i.e., native `float` type in Java. float 123.456 -
    int/Integer Integer, i.e., native `int` type in Java. int 123 -
    long/Long Long integer, i.e., native `Long` type in Java. 123L -
    short/Short Short integer, i.e., native `Short` type in Java. 123 -
    byte[] Byte array, i.e., `byte[]` type in Java. bytes byte[]{1,2,3} -
    java.util.List List (a sequence container), i.e., native `List` type in Java. list new java.util.ArrayList<>() For more information, see Interface List.
    java.util.Map Dictionary (a key-value pair container), i.e., native `Map` type in Java. dict new java.util.HashMap<>() For more information, see Interface Map.
    java.time.OffsetDateTime Time, i.e., native `OffsetDateTime` type in Java. datetime.datetime java.time.OffsetDateTime.now() For more information, see Class OffsetDateTime.
    java.time.LocalDate Date, i.e., native `LocalDate` type in Java. datetime.date java.time.LocalDate.now() For more information, see Class LocalDate.
    java.time.OffsetTime Time, i.e., native `OffsetTime` type in Java. datetime.time java.time.OffsetTime.now() For more information, see Class OffsetTime.
    java.math.BigDecimal Decimal number, i.e., native `BigDecimal` type in Java. decimal.Decimal new java.math.BigDecimal("1") For more information, see Class BigDecimal.
    com.tencent.ipaas.dataway.common.message.Entity (data type unique to iPaaS) Entity data in iPaaS, which represents a binary object and is accessed as an `Entity` object. It contains information such as `blob`, `mimeType`, and `encoding`. Entity `payload` in a message constructed by the HTTP Listener component. For more information, see Using an `Entity` Object.
    com.tencent.ipaas.dataway.common.message.Multimap (data type unique to iPaaS) Multi-value map. Like `xml` but unlike `dict`, this type supports duplicate `key` values. It is inherited from `HashMap<String, List>`. MultiMap Object obtained after data in `application/www-form-urlencoded` format is parsed Constructor: Multimap(Map dict)
    Static constructor: Multimap fromSetEntry(Set<Map.Entry> set)
    Gets the first value of the specified key: Object getFirst(Object key)
    Gets the list of all values of the specified key: List getAll(Object key)
    Gets the key-value pair set: Set<Map.Entry> toSetEntry()
    It is inherited from the `HashMap` method.
    com.tencent.ipaas.dataway.common.message.FormDataParts (data type unique to iPaaS) Array + list data structure, which is similar to `orderDict` in Python. It is inherited from `LinkedHashMap<String, Object>`. FormDataParts Object obtained after data in `multipart/form-data` format is parsed Constructor: FormDataParts(String boundary)
    Gets the value of the specified keyword (if the keyword is `int` or `long`, the keyword will be used as the key number; otherwise, the keyword will be the key): Object get(Object key)
    It is inherited from the `LinkedHashMap` method.
    com.tencent.ipaas.dataway.common.message.Message (data type unique to iPaaS, which cannot be constructed in Dataway) Flow message in iPaaS, which is accessed as a `Message` object. Message `msg` parameter in the `public Object eval(Message msg)` entry function Gets the payload: Object getPayload()
    Gets attributes: Map getAttrs()
    Gets variables: Map<Object, Object> getVars()/Object getVar(String name)
    Gets the error: DataWayError getError()
    com.tencent.ipaas.dataway.common.message.DataSet (data type unique to iPaaS, which cannot be constructed in Dataway) Data set in data integration, which is generated by the data integration component. RecordSet Output of the **Builder** component Gets the ID: Long getId()
    Gets the schema: Schema getSchema()
    Gets partitions: Long getPartitions()
    com.tencent.ipaas.dataway.common.message.Record (data type unique to iPaaS, which cannot be constructed in Dataway) Single data record in data integration, which contains the schema. Record It can be obtained by using the Foreach component to traverse `DataSet`. Gets all data: List getData()
    Gets the schema: Schema getSchema()
    Gets the data of the specified keyword (if the keyword is `int` or `long`, the keyword will be the column number; otherwise, the keyword will be the field name): Object get(Object key)
    Returns whether the field name is contained: boolean contains(String name)
    Gets the field name iterator: Iterator<String> getIterator()
    com.tencent.ipaas.dataway.common.message.Schema (data type unique to iPaaS, which cannot be constructed in Dataway) Data dictionary in data integration, which describes the metadata. Schema It can be obtained through the `getSchema()` method of `Record` and is applicable to each data record. Gets all field information: RecordField[] getFields()
    Gets the field information of the specified field name: RecordField getField(String name)
    Converts data into the dictionary format such as `{"Fields":[{"Name":"name","Type":"string"}]}`: Map<?,?> toMap()
    com.tencent.ipaas.dataway.common.message.RecordField (data type unique to iPaaS, which cannot be constructed in Dataway) Field information in data integration, which describes the metadata of a single field. Schema `RecordField` can be obtained through the `getField(name)` method of `Schema`. Gets the field name: String getName()
    Gets the field type: String getType()

    Using an Entity Object

    Basic methods

    In Java code mode of iPaaS, the Entity type is used to represent the entity data in flows. It is an encapsulation object of binary data and contains blob, mimeType, and encoding.

    Field Description
    blob Raw binary data.
    mimeType Content format of binary data, such as application/json, application/www-form-urlencoded, and multipart/form-data.
    encoding Character encoding type of binary data, such as utf-8 and gbk.

    You can access content in Entity as follows:

    Access Method Description
    byte[] getBlob() Gets the payload data of the message object. A byte[] object will be returned.
    String getMimeType() Gets the MIME type of the message object. A String object will be returned.
    String getEncoding() Gets the encoding type of the message object. A String object will be returned.
    Object getValue() Deserializes blob in the payload based on the MIME and encoding types and returns the result. This type is defined in the type system in Java code mode.
    Object get(Object key) Deserializes the content in message based on the MIME and encoding types and gets the value of the specified key.

    Currently, the MIME types supported for deserialization and types of the deserialized value are as listed below:

    • text/plain → String
    • application/json → Object, which is the same as JSON
    • application/x-www-form-urlencoded → Multimap
    • application/xml → Map
    • application/csv → List<Map<String,String>>, i.e., list of mappings between field names and values
    • multipart/form-data → FormDataParts

    Constructor

    Entity.fromValue static method

    This method is used to encapsulate the value type data into an Entity object and return it as follows:

    Entity.fromValue(Object value, String mimeType, String encoding)
    

    The fromValue function tries serializing value based on the specified MIME and encoding types to get the data of byte[] type, encapsulates it into an Entity object, and returns the object.

    • The mimeType parameter is required. Currently, six MIME types are supported: text/plain, application/json, application/x-www-form-urlencoded, application/csv, application/xml, and multipart/form-data.
    • The encoding parameter is required and can be any valid encoding type.

    Entity.fromBytes static method

    This method is used to encapsulate a String or a byte[] object into an Entity object and return it as follows:

    Entity.fromBytes(Object data, String mimeType, String encoding)
    

    The verification rules of the MIME type and encoding type parameters in fromBytes are similar to those in the fromValue function but differ in that the value of the MIME type parameter is not limited and can be any MIME type.

    • If the data parameter passed to the fromBytes function is of byte[] type, the function will directly return an Entity object consisting of parameters data, mimeType, and encoding.
    • If the passed data parameter is of String type, it will be encoded as a byte[] object based on the encoding parameter and constructed as an Entity object.

    Use limits

    An Entity object is essentially an encapsulation object of binary data. For ease of use, object content access methods like entity.get() are provided. Before using these features, note that for some special operations, the system will try deserializing the binary data in the Entity object, and runtime errors will occur if parsing fails. Such special operations include:

    • Using entity.getValue() to get the structured result after parsing.
    • Using entity.get(key) to get the content of an element in the structured result.

    If you perform the above special operations on a non-compliant Entity object, runtime errors will occur.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support