Search in sources :

Example 6 with SinkDataEntry

use of io.openmessaging.connector.api.data.SinkDataEntry in project rocketmq-externals by apache.

the class JdbcSinkTask method put.

@Override
public void put(Collection<SinkDataEntry> sinkDataEntries) {
    try {
        if (tableQueue.size() > 1) {
            updater = tableQueue.poll(1000, TimeUnit.MILLISECONDS);
        } else {
            updater = tableQueue.peek();
        }
        for (SinkDataEntry record : sinkDataEntries) {
            Map<Field, Object[]> fieldMap = new HashMap<>();
            Object[] payloads = record.getPayload();
            Schema schema = record.getSchema();
            EntryType entryType = record.getEntryType();
            String tableName = schema.getName();
            String dbName = schema.getDataSource();
            List<Field> fields = schema.getFields();
            Boolean parseError = false;
            if (!fields.isEmpty()) {
                for (Field field : fields) {
                    Object fieldValue = payloads[field.getIndex()];
                    Object[] value = JSONObject.parseArray((String) fieldValue).toArray();
                    if (value.length == 2) {
                        fieldMap.put(field, value);
                    } else {
                        log.error("parseArray error, fieldValue:{}", fieldValue);
                        parseError = true;
                    }
                }
            }
            if (!parseError) {
                Boolean isSuccess = updater.push(dbName, tableName, fieldMap, entryType);
                if (!isSuccess) {
                    log.error("push data error, dbName:{}, tableName:{}, entryType:{}, fieldMap:{}", dbName, tableName, fieldMap, entryType);
                }
            }
        }
    } catch (Exception e) {
        log.error("put sinkDataEntries error, {}", e);
    }
}
Also used : Field(io.openmessaging.connector.api.data.Field) SinkDataEntry(io.openmessaging.connector.api.data.SinkDataEntry) EntryType(io.openmessaging.connector.api.data.EntryType) HashMap(java.util.HashMap) Schema(io.openmessaging.connector.api.data.Schema) JSONObject(com.alibaba.fastjson.JSONObject)

Example 7 with SinkDataEntry

use of io.openmessaging.connector.api.data.SinkDataEntry in project rocketmq-externals by apache.

the class WorkerDirectTask method sendRecord.

private void sendRecord(Collection<SourceDataEntry> sourceDataEntries) {
    List<SinkDataEntry> sinkDataEntries = new ArrayList<>(sourceDataEntries.size());
    ByteBuffer position = null;
    ByteBuffer partition = null;
    for (SourceDataEntry sourceDataEntry : sourceDataEntries) {
        Schema schema = sourceDataEntry.getSchema();
        Object[] payload = sourceDataEntry.getPayload();
        DataEntryBuilder dataEntryBuilder = new DataEntryBuilder(schema).entryType(sourceDataEntry.getEntryType()).queue(sourceDataEntry.getQueueName()).timestamp(sourceDataEntry.getTimestamp());
        if (schema.getFields() != null) {
            schema.getFields().forEach(field -> dataEntryBuilder.putFiled(field.getName(), payload[field.getIndex()]));
        }
        SinkDataEntry sinkDataEntry = dataEntryBuilder.buildSinkDataEntry(-1L);
        sinkDataEntries.add(sinkDataEntry);
        position = sourceDataEntry.getSourcePosition();
        partition = sourceDataEntry.getSourcePartition();
    }
    try {
        sinkTask.put(sinkDataEntries);
        try {
            if (null != position && null != partition) {
                positionManagementService.putPosition(position, partition);
            }
        } catch (Exception e) {
            log.error("Source task save position info failed.", e);
        }
    } catch (Exception e) {
        log.error("Send message error, error info: {}.", e);
    }
}
Also used : SinkDataEntry(io.openmessaging.connector.api.data.SinkDataEntry) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Schema(io.openmessaging.connector.api.data.Schema) ArrayList(java.util.ArrayList) DataEntryBuilder(io.openmessaging.connector.api.data.DataEntryBuilder) ByteBuffer(java.nio.ByteBuffer)

Example 8 with SinkDataEntry

use of io.openmessaging.connector.api.data.SinkDataEntry in project rocketmq-externals by apache.

the class FileSinkTask method put.

@Override
public void put(Collection<SinkDataEntry> sinkDataEntries) {
    for (SinkDataEntry record : sinkDataEntries) {
        Object[] payloads = record.getPayload();
        log.trace("Writing line to {}: {}", logFilename(), payloads);
        Schema schema = record.getSchema();
        List<Field> fields = schema.getFields();
        for (Field field : fields) {
            FieldType type = field.getType();
            if (type.equals(FieldType.STRING)) {
                log.info("Writing line to {}: {}", logFilename(), payloads[field.getIndex()]);
                outputStream.println(String.valueOf(payloads[field.getIndex()]));
            }
        }
    }
}
Also used : Field(io.openmessaging.connector.api.data.Field) SinkDataEntry(io.openmessaging.connector.api.data.SinkDataEntry) Schema(io.openmessaging.connector.api.data.Schema) FieldType(io.openmessaging.connector.api.data.FieldType)

Aggregations

SinkDataEntry (io.openmessaging.connector.api.data.SinkDataEntry)8 Schema (io.openmessaging.connector.api.data.Schema)5 Field (io.openmessaging.connector.api.data.Field)4 EntryType (io.openmessaging.connector.api.data.EntryType)3 ArrayList (java.util.ArrayList)3 JSONObject (com.alibaba.fastjson.JSONObject)2 DataEntryBuilder (io.openmessaging.connector.api.data.DataEntryBuilder)2 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)2 HashMap (java.util.HashMap)2 FieldType (io.openmessaging.connector.api.data.FieldType)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 WriteStatus (org.apache.hudi.client.WriteStatus)1 HoodieAvroPayload (org.apache.hudi.common.model.HoodieAvroPayload)1 HoodieKey (org.apache.hudi.common.model.HoodieKey)1 HoodieRecord (org.apache.hudi.common.model.HoodieRecord)1 MessageExt (org.apache.rocketmq.common.message.MessageExt)1 JsonConverter (org.apache.rocketmq.connect.runtime.converter.JsonConverter)1 RocketMQConverter (org.apache.rocketmq.connect.runtime.converter.RocketMQConverter)1