Search in sources :

Example 6 with SourceDataEntry

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

the class KafkaSourceTaskTest method pollTest.

@Test
public void pollTest() throws Exception {
    KafkaSourceTask task = new KafkaSourceTask();
    Field config = KafkaSourceTask.class.getDeclaredField("config");
    config.setAccessible(true);
    Collection<SourceDataEntry> list = task.poll();
    Assert.assertEquals(list.size(), 1);
    list = task.poll();
    Assert.assertEquals(list.size(), 0);
}
Also used : Field(java.lang.reflect.Field) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Test(org.junit.Test)

Example 7 with SourceDataEntry

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

the class MongoDataEntry method createSouceDataEntry.

public static SourceDataEntry createSouceDataEntry(ReplicationEvent event, ReplicaSetConfig replicaSetConfig) {
    DataEntryBuilder dataEntryBuilder;
    if (event.getOperationType().equals(OperationType.CREATED)) {
        Schema schema = createdSchema(replicaSetConfig.getReplicaSetName());
        dataEntryBuilder = new DataEntryBuilder(schema);
        dataEntryBuilder.timestamp(System.currentTimeMillis()).queue(event.getNamespace().replace(".", "-").replace("$", "-")).entryType(event.getEntryType());
        dataEntryBuilder.putFiled(CREATED, event.getDocument().toJson());
        dataEntryBuilder.putFiled(NAMESPACE, event.getNamespace());
    } else {
        Schema schema = oplogSchema(replicaSetConfig.getReplicaSetName());
        dataEntryBuilder = new DataEntryBuilder(schema);
        dataEntryBuilder.timestamp(System.currentTimeMillis()).queue(event.getNamespace().replace(".", "-").replace("$", "-")).entryType(event.getEntryType());
        dataEntryBuilder.putFiled(OPERATION_TYPE, event.getOperationType().name());
        dataEntryBuilder.putFiled(TIMESTAMP, event.getTimestamp().getValue());
        dataEntryBuilder.putFiled(VERSION, event.getV());
        dataEntryBuilder.putFiled(NAMESPACE, event.getNamespace());
        dataEntryBuilder.putFiled(PATCH, event.getEventData().isPresent() ? JSONObject.toJSONString(event.getEventData().get()) : "");
        dataEntryBuilder.putFiled(OBJECT_ID, event.getObjectId().isPresent() ? JSONObject.toJSONString(event.getObjectId().get()) : "");
    }
    String position = createPosition(event, replicaSetConfig);
    SourceDataEntry sourceDataEntry = dataEntryBuilder.buildSourceDataEntry(ByteBuffer.wrap(replicaSetConfig.getReplicaSetName().getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(position.getBytes(StandardCharsets.UTF_8)));
    return sourceDataEntry;
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Schema(io.openmessaging.connector.api.data.Schema) DataEntryBuilder(io.openmessaging.connector.api.data.DataEntryBuilder)

Example 8 with SourceDataEntry

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

the class JdbcSourceTask 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("JDBC 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.jdbc.schema.Table) Schema(io.openmessaging.connector.api.data.Schema) DataEntryBuilder(io.openmessaging.connector.api.data.DataEntryBuilder) Field(io.openmessaging.connector.api.data.Field) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject)

Example 9 with SourceDataEntry

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

the class RedisEntryConverterTest method test.

@Test
public void test() {
    KVEntry entry = getKVEntry();
    KVEntryConverter converter = new RedisEntryConverter();
    Collection<SourceDataEntry> res = converter.kVEntryToDataEntries(entry);
    Assert.assertNotNull(res);
    Assert.assertEquals(1, res.size());
    Assert.assertEquals("key", ((List<SourceDataEntry>) res).get(0).getPayload()[1]);
    Assert.assertEquals("value", ((List<SourceDataEntry>) res).get(0).getPayload()[2]);
    Assert.assertEquals("set", ((List<SourceDataEntry>) res).get(0).getPayload()[0]);
    Map<String, Object> params = (Map<String, Object>) ((List<SourceDataEntry>) res).get(0).getPayload()[3];
    Assert.assertEquals("replId", params.get(Options.REDIS_REPLID.name()));
}
Also used : RedisEntryConverter(org.apache.rocketmq.connect.redis.converter.RedisEntryConverter) KVEntry(org.apache.rocketmq.connect.redis.pojo.KVEntry) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) KVEntryConverter(org.apache.rocketmq.connect.redis.converter.KVEntryConverter) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 10 with SourceDataEntry

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

the class WorkerSourceTask method run.

/**
 * Start a source task, and send data entry to MQ cyclically.
 */
@Override
public void run() {
    try {
        producer.start();
        log.info("Source task producer start.");
        state.compareAndSet(WorkerTaskState.NEW, WorkerTaskState.PENDING);
        sourceTask.initialize(new SourceTaskContext() {

            @Override
            public PositionStorageReader positionStorageReader() {
                return positionStorageReader;
            }

            @Override
            public KeyValue configs() {
                return taskConfig;
            }
        });
        sourceTask.start(taskConfig);
        state.compareAndSet(WorkerTaskState.PENDING, WorkerTaskState.RUNNING);
        log.info("Source task start, config:{}", JSON.toJSONString(taskConfig));
        while (WorkerState.STARTED == workerState.get() && WorkerTaskState.RUNNING == state.get()) {
            try {
                Collection<SourceDataEntry> toSendEntries = sourceTask.poll();
                if (null != toSendEntries && toSendEntries.size() > 0) {
                    sendRecord(toSendEntries);
                }
            } catch (Exception e) {
                log.error("Source task runtime exception", e);
                state.set(WorkerTaskState.ERROR);
            }
        }
        sourceTask.stop();
        state.compareAndSet(WorkerTaskState.STOPPING, WorkerTaskState.STOPPED);
        log.info("Source task stop, config:{}", JSON.toJSONString(taskConfig));
    } catch (Exception e) {
        log.error("Run task failed.", e);
        state.set(WorkerTaskState.ERROR);
    } finally {
        if (producer != null) {
            producer.shutdown();
            log.info("Source task producer shutdown.");
        }
    }
}
Also used : ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) KeyValue(io.openmessaging.KeyValue) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) SourceTaskContext(io.openmessaging.connector.api.source.SourceTaskContext) PositionStorageReader(io.openmessaging.connector.api.PositionStorageReader) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Aggregations

SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)24 Schema (io.openmessaging.connector.api.data.Schema)12 DataEntryBuilder (io.openmessaging.connector.api.data.DataEntryBuilder)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 JSONObject (com.alibaba.fastjson.JSONObject)6 Field (io.openmessaging.connector.api.data.Field)5 ByteBuffer (java.nio.ByteBuffer)5 EntryType (io.openmessaging.connector.api.data.EntryType)4 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 KeyValue (io.openmessaging.KeyValue)3 BytesMessage (javax.jms.BytesMessage)3 MapMessage (javax.jms.MapMessage)3 Message (javax.jms.Message)3 ObjectMessage (javax.jms.ObjectMessage)3 StreamMessage (javax.jms.StreamMessage)3 TextMessage (javax.jms.TextMessage)3 MQClientException (org.apache.rocketmq.client.exception.MQClientException)3 KVEntry (org.apache.rocketmq.connect.redis.pojo.KVEntry)3