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