Search in sources :

Example 16 with SourceDataEntry

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

the class RedisSourceTaskTest method testTask.

@Test
public void testTask() throws Exception {
    if (this.task != null) {
        RedisEvent redisEvent = getRedisEvent();
        this.task.getEventProcessor().commit(redisEvent);
        Collection<SourceDataEntry> col = this.task.poll();
        Assert.assertNotNull(col);
        Assert.assertEquals(1, col.size());
        Assert.assertNotNull(this.task.getConfig());
    }
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) RedisEvent(org.apache.rocketmq.connect.redis.pojo.RedisEvent) Test(org.junit.Test)

Example 17 with SourceDataEntry

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

the class WorkerSinkTask method convertToSinkDataEntry.

private SinkDataEntry convertToSinkDataEntry(MessageExt message) {
    Map<String, String> properties = message.getProperties();
    String queueName;
    EntryType entryType;
    Schema schema;
    Long timestamp;
    Object[] datas = new Object[1];
    if (null == recordConverter || recordConverter instanceof RocketMQConverter) {
        queueName = properties.get(RuntimeConfigDefine.CONNECT_TOPICNAME);
        String connectEntryType = properties.get(RuntimeConfigDefine.CONNECT_ENTRYTYPE);
        entryType = StringUtils.isNotEmpty(connectEntryType) ? EntryType.valueOf(connectEntryType) : null;
        String connectTimestamp = properties.get(RuntimeConfigDefine.CONNECT_TIMESTAMP);
        timestamp = StringUtils.isNotEmpty(connectTimestamp) ? Long.valueOf(connectTimestamp) : null;
        String connectSchema = properties.get(RuntimeConfigDefine.CONNECT_SCHEMA);
        schema = StringUtils.isNotEmpty(connectSchema) ? JSON.parseObject(connectSchema, Schema.class) : null;
        datas = new Object[1];
        datas[0] = message.getBody();
    } else {
        final byte[] messageBody = message.getBody();
        final SourceDataEntry sourceDataEntry = JSON.parseObject(new String(messageBody), SourceDataEntry.class);
        final Object[] payload = sourceDataEntry.getPayload();
        final byte[] decodeBytes = Base64.getDecoder().decode((String) payload[0]);
        Object recodeObject;
        if (recordConverter instanceof JsonConverter) {
            JsonConverter jsonConverter = (JsonConverter) recordConverter;
            jsonConverter.setClazz(Object[].class);
            recodeObject = recordConverter.byteToObject(decodeBytes);
            datas = (Object[]) recodeObject;
        }
        schema = sourceDataEntry.getSchema();
        entryType = sourceDataEntry.getEntryType();
        queueName = sourceDataEntry.getQueueName();
        timestamp = sourceDataEntry.getTimestamp();
    }
    DataEntryBuilder dataEntryBuilder = new DataEntryBuilder(schema);
    dataEntryBuilder.entryType(entryType);
    dataEntryBuilder.queue(queueName);
    dataEntryBuilder.timestamp(timestamp);
    List<Field> fields = schema.getFields();
    if (null != fields && !fields.isEmpty()) {
        for (Field field : fields) {
            dataEntryBuilder.putFiled(field.getName(), datas[field.getIndex()]);
        }
    }
    SinkDataEntry sinkDataEntry = dataEntryBuilder.buildSinkDataEntry(message.getQueueOffset());
    return sinkDataEntry;
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) JsonConverter(org.apache.rocketmq.connect.runtime.converter.JsonConverter) Schema(io.openmessaging.connector.api.data.Schema) DataEntryBuilder(io.openmessaging.connector.api.data.DataEntryBuilder) RocketMQConverter(org.apache.rocketmq.connect.runtime.converter.RocketMQConverter) Field(io.openmessaging.connector.api.data.Field) SinkDataEntry(io.openmessaging.connector.api.data.SinkDataEntry) EntryType(io.openmessaging.connector.api.data.EntryType)

Example 18 with SourceDataEntry

use of io.openmessaging.connector.api.data.SourceDataEntry 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 19 with SourceDataEntry

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

the class MongoSourceConnectorTest method testPoll.

@Test
public void testPoll() throws Exception {
    LinkedBlockingQueue<SourceDataEntry> entries = new LinkedBlockingQueue<>();
    ReplicaSetsContext context = new ReplicaSetsContext(sourceTaskConfig);
    Field dataEntryQueue = ReplicaSetsContext.class.getDeclaredField("dataEntryQueue");
    dataEntryQueue.setAccessible(true);
    dataEntryQueue.set(context, entries);
    ReplicationEvent event = new ReplicationEvent();
    event.setOperationType(OperationType.INSERT);
    event.setNamespace("test.person");
    event.setTimestamp(new BsonTimestamp(1565609506, 1));
    event.setDocument(new Document("testKey", "testValue"));
    event.setH(324243242L);
    event.setEventData(Optional.ofNullable(new Document("testEventKey", "testEventValue")));
    event.setObjectId(Optional.empty());
    context.publishEvent(event, new ReplicaSetConfig("", "testReplicaName", "localhost:27027"));
    List<SourceDataEntry> sourceDataEntries = (List<SourceDataEntry>) context.poll();
    Assert.assertTrue(sourceDataEntries.size() == 1);
    SourceDataEntry sourceDataEntry = sourceDataEntries.get(0);
    Assert.assertEquals("test-person", sourceDataEntry.getQueueName());
    ByteBuffer sourcePartition = sourceDataEntry.getSourcePartition();
    Assert.assertEquals("testReplicaName", new String(sourcePartition.array()));
    ByteBuffer sourcePosition = sourceDataEntry.getSourcePosition();
    Position position = JSONObject.parseObject(new String(sourcePosition.array()), Position.class);
    Assert.assertEquals(position.getTimeStamp(), 1565609506);
    Assert.assertEquals(position.getInc(), 1);
    Assert.assertEquals(position.isInitSync(), false);
    EntryType entryType = sourceDataEntry.getEntryType();
    Assert.assertEquals(EntryType.CREATE, entryType);
    String queueName = sourceDataEntry.getQueueName();
    Assert.assertEquals("test-person", queueName);
    Schema schema = sourceDataEntry.getSchema();
    Assert.assertTrue(schema.getFields().size() == 6);
    Object[] payload = sourceDataEntry.getPayload();
    Assert.assertTrue(payload.length == 6);
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Position(org.apache.connect.mongo.replicator.Position) Schema(io.openmessaging.connector.api.data.Schema) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) ReplicationEvent(org.apache.connect.mongo.replicator.event.ReplicationEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Document(org.bson.Document) ByteBuffer(java.nio.ByteBuffer) BsonTimestamp(org.bson.BsonTimestamp) Field(java.lang.reflect.Field) EntryType(io.openmessaging.connector.api.data.EntryType) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) List(java.util.List) JSONObject(com.alibaba.fastjson.JSONObject) Test(org.junit.Test)

Example 20 with SourceDataEntry

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

the class RedisSourceTask method poll.

@Override
public Collection<SourceDataEntry> poll() {
    try {
        KVEntry event = this.eventProcessor.poll();
        if (event == null) {
            return null;
        }
        event.queueName(Options.REDIS_QEUEUE.name());
        event.entryType(EntryType.UPDATE);
        Collection<SourceDataEntry> res = this.kvEntryConverter.kVEntryToDataEntries(event);
        LOGGER.info("send data entries: {}", res);
        return res;
    } catch (InterruptedException e) {
        LOGGER.error("redis task interrupted. {}", e);
        this.stop();
    } catch (Exception e) {
        LOGGER.error("redis task error. {}", e);
        this.stop();
    }
    return null;
}
Also used : KVEntry(org.apache.rocketmq.connect.redis.pojo.KVEntry) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) IOException(java.io.IOException)

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