Search in sources :

Example 6 with DataEntryBuilder

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

the class CassandraSourceTask method poll.

@Override
public Collection<SourceDataEntry> poll() {
    List<SourceDataEntry> res = new ArrayList<>();
    try {
        if (tableQueue.size() > 1)
            querier = tableQueue.poll(1000, TimeUnit.MILLISECONDS);
        else
            querier = tableQueue.peek();
        Timer timer = new Timer();
        try {
            Thread.currentThread();
            // 毫秒
            Thread.sleep(1000);
        } catch (Exception e) {
            throw e;
        }
        querier.poll();
        for (Table dataRow : querier.getList()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("nextQuery", "database");
            jsonObject.put("nextPosition", "table");
            Schema schema = new Schema();
            schema.setDataSource(dataRow.getDatabase());
            schema.setName(dataRow.getName());
            schema.setFields(new ArrayList<>());
            for (int i = 0; i < dataRow.getColList().size(); i++) {
                String columnName = dataRow.getColList().get(i);
                String rawDataType = dataRow.getRawDataTypeList().get(i);
                Field field = new Field(i, columnName, ColumnParser.mapConnectorFieldType(rawDataType));
                schema.getFields().add(field);
            }
            DataEntryBuilder dataEntryBuilder = new DataEntryBuilder(schema);
            dataEntryBuilder.timestamp(System.currentTimeMillis()).queue(dataRow.getName()).entryType(EntryType.UPDATE);
            for (int i = 0; i < dataRow.getColList().size(); i++) {
                Object[] value = new Object[2];
                value[0] = value[1] = dataRow.getParserList().get(i).getValue(dataRow.getDataList().get(i));
                dataEntryBuilder.putFiled(dataRow.getColList().get(i), JSONObject.toJSONString(value));
            }
            SourceDataEntry sourceDataEntry = dataEntryBuilder.buildSourceDataEntry(ByteBuffer.wrap((ConstDefine.PREFIX + config.getDbUrl() + config.getDbPort()).getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8)));
            res.add(sourceDataEntry);
            log.debug("sourceDataEntry : {}", JSONObject.toJSONString(sourceDataEntry));
        }
    } catch (Exception e) {
        log.error("Cassandra task poll error, current config:" + JSON.toJSONString(config), e);
    }
    log.debug("dataEntry poll successfully,{}", JSONObject.toJSONString(res));
    return res;
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Table(org.apache.rocketmq.connect.cassandra.schema.Table) Schema(io.openmessaging.connector.api.data.Schema) ArrayList(java.util.ArrayList) DataEntryBuilder(io.openmessaging.connector.api.data.DataEntryBuilder) Field(io.openmessaging.connector.api.data.Field) Timer(java.util.Timer) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject)

Example 7 with DataEntryBuilder

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

the class RedisEntryConverter method kVEntryToDataEntries.

@Override
public List<SourceDataEntry> kVEntryToDataEntries(KVEntry kvEntry) {
    Schema schema = getRedisSchema(kvEntry.getValueType());
    String partition = kvEntry.getPartition();
    if (partition == null) {
        throw new IllegalStateException("partition info error.");
    }
    List<SourceDataEntry> res = new ArrayList<>();
    List<Object> values = splitValue(kvEntry.getValueType(), kvEntry.getValue(), this.maxValueSize);
    for (int i = 0; i < values.size(); i++) {
        DataEntryBuilder builder = newDataEntryBuilderWithoutValue(schema, kvEntry);
        builder.putFiled(Options.REDIS_VALUE.name(), values.get(i));
        builder.timestamp(System.currentTimeMillis());
        SourceDataEntry entry = builder.buildSourceDataEntry(ByteBuffer.wrap(kvEntry.getPartition().getBytes()), ByteBuffer.wrap(RedisPositionConverter.longToJson(kvEntry.getOffset()).toJSONString().getBytes()));
        res.add(entry);
    }
    return res;
}
Also used : 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)

Example 8 with DataEntryBuilder

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

the class RedisEntryConverter method newDataEntryBuilderWithoutValue.

private DataEntryBuilder newDataEntryBuilderWithoutValue(Schema schema, KVEntry kvEntry) {
    DataEntryBuilder dataEntryBuilder = new DataEntryBuilder(schema);
    dataEntryBuilder.queue(kvEntry.getQueueName());
    dataEntryBuilder.entryType(kvEntry.getEntryType());
    dataEntryBuilder.putFiled(Options.REDIS_COMMAND.name(), kvEntry.getCommand());
    dataEntryBuilder.putFiled(Options.REDIS_KEY.name(), kvEntry.getKey());
    dataEntryBuilder.putFiled(Options.REDIS_PARAMS.name(), kvEntry.getParams());
    return dataEntryBuilder;
}
Also used : DataEntryBuilder(io.openmessaging.connector.api.data.DataEntryBuilder)

Example 9 with DataEntryBuilder

use of io.openmessaging.connector.api.data.DataEntryBuilder 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)

Aggregations

DataEntryBuilder (io.openmessaging.connector.api.data.DataEntryBuilder)9 Schema (io.openmessaging.connector.api.data.Schema)8 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)8 Field (io.openmessaging.connector.api.data.Field)5 ArrayList (java.util.ArrayList)5 JSONObject (com.alibaba.fastjson.JSONObject)4 SinkDataEntry (io.openmessaging.connector.api.data.SinkDataEntry)2 ByteBuffer (java.nio.ByteBuffer)2 EntryType (io.openmessaging.connector.api.data.EntryType)1 ConnectException (io.openmessaging.connector.api.exception.ConnectException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Map (java.util.Map)1 Timer (java.util.Timer)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)1 OffsetWrapper (org.apache.rocketmq.common.admin.OffsetWrapper)1 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)1