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);
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations