Search in sources :

Example 1 with Schema

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

the class CassandraSinkTask method put.

@Override
public void put(Collection<SinkDataEntry> sinkDataEntries) {
    try {
        if (tableQueue.size() > 1) {
            updater = tableQueue.poll(1000, TimeUnit.MILLISECONDS);
        } else {
            updater = tableQueue.peek();
        }
        log.info("Cassandra Sink Task trying to put()");
        for (SinkDataEntry record : sinkDataEntries) {
            Map<Field, Object[]> fieldMap = new HashMap<>();
            Object[] payloads = record.getPayload();
            Schema schema = record.getSchema();
            EntryType entryType = record.getEntryType();
            String cfName = schema.getName();
            String keyspaceName = 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) {
                log.info("Cassandra Sink Task trying to call updater.push()");
                Boolean isSuccess = updater.push(keyspaceName, cfName, fieldMap, entryType);
                if (!isSuccess) {
                    log.error("push data error, keyspaceName:{}, cfName:{}, entryType:{}, fieldMap:{}", keyspaceName, cfName, 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 2 with Schema

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

the class MongoTest method testInitSyncCopy.

@Test
public void testInitSyncCopy() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("person");
    collection.deleteMany(new Document());
    int count = 1000;
    List<String> documents = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        Document document = new Document();
        document.put("name", "test" + i);
        document.put("age", i);
        document.put("sex", i % 2 == 0 ? "boy" : "girl");
        collection.insertOne(document);
        documents.add(document.getObjectId("_id").toHexString());
    }
    SourceTaskConfig sourceTaskConfig = new SourceTaskConfig();
    Map<String, List<String>> insterest = new HashMap<>();
    List<String> collections = new ArrayList<>();
    collections.add("*");
    insterest.put("test", collections);
    sourceTaskConfig.setInterestDbAndCollection(JSONObject.toJSONString(insterest));
    ReplicaSetConfig replicaSetConfig = new ReplicaSetConfig("", "test", "localhost");
    ReplicaSetsContext replicaSetsContext = new ReplicaSetsContext(sourceTaskConfig);
    ReplicaSet replicaSet = new ReplicaSet(replicaSetConfig, replicaSetsContext);
    Field running = ReplicaSet.class.getDeclaredField("running");
    running.setAccessible(true);
    running.set(replicaSet, new AtomicBoolean(true));
    InitSync initSync = new InitSync(replicaSetConfig, mongoClient, replicaSetsContext, replicaSet);
    initSync.start();
    int syncCount = 0;
    while (syncCount < count) {
        Collection<SourceDataEntry> sourceDataEntries = replicaSetsContext.poll();
        Assert.assertTrue(sourceDataEntries.size() > 0);
        for (SourceDataEntry sourceDataEntry : sourceDataEntries) {
            ByteBuffer sourcePartition = sourceDataEntry.getSourcePartition();
            Assert.assertEquals("test", new String(sourcePartition.array()));
            ByteBuffer sourcePosition = sourceDataEntry.getSourcePosition();
            Position position = new Position();
            position.setInitSync(true);
            position.setTimeStamp(0);
            position.setInc(0);
            Assert.assertEquals(position, JSONObject.parseObject(new String(sourcePosition.array()), Position.class));
            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() == 2);
            Object[] payload = sourceDataEntry.getPayload();
            Assert.assertTrue(payload.length == 2);
            Assert.assertEquals(payload[0].toString(), "test.person");
            Assert.assertTrue(documents.contains(JSONObject.parseObject(payload[1].toString(), Document.class).get("_id", JSONObject.class).getString("$oid")));
            syncCount++;
        }
    }
    Assert.assertTrue(syncCount == count);
}
Also used : HashMap(java.util.HashMap) Schema(io.openmessaging.connector.api.data.Schema) ArrayList(java.util.ArrayList) ConnectionString(com.mongodb.ConnectionString) Document(org.bson.Document) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) ReplicaSet(org.apache.connect.mongo.replicator.ReplicaSet) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Position(org.apache.connect.mongo.replicator.Position) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InitSync(org.apache.connect.mongo.initsync.InitSync) EntryType(io.openmessaging.connector.api.data.EntryType) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) Test(org.junit.Test)

Example 3 with Schema

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

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

the class MongoDataEntry method oplogSchema.

private static Schema oplogSchema(String dataSourceName) {
    Schema schema = new Schema();
    schema.setDataSource(dataSourceName);
    schema.setName(SCHEMA_OPLOG_NAME);
    oplogField(schema);
    return schema;
}
Also used : Schema(io.openmessaging.connector.api.data.Schema)

Example 5 with Schema

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

Aggregations

Schema (io.openmessaging.connector.api.data.Schema)18 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)12 Field (io.openmessaging.connector.api.data.Field)9 JSONObject (com.alibaba.fastjson.JSONObject)8 DataEntryBuilder (io.openmessaging.connector.api.data.DataEntryBuilder)8 ArrayList (java.util.ArrayList)7 EntryType (io.openmessaging.connector.api.data.EntryType)6 SinkDataEntry (io.openmessaging.connector.api.data.SinkDataEntry)5 ByteBuffer (java.nio.ByteBuffer)5 HashMap (java.util.HashMap)3 Field (java.lang.reflect.Field)2 List (java.util.List)2 Position (org.apache.connect.mongo.replicator.Position)2 ReplicaSetConfig (org.apache.connect.mongo.replicator.ReplicaSetConfig)2 ReplicaSetsContext (org.apache.connect.mongo.replicator.ReplicaSetsContext)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 RocketMQConverter (org.apache.rocketmq.connect.runtime.converter.RocketMQConverter)2 Document (org.bson.Document)2 Test (org.junit.Test)2 ConnectionString (com.mongodb.ConnectionString)1