use of com.qlangtech.tis.async.message.client.consumer.IMQListener in project plugins by qlangtech.
the class CUDCDCTestSuit method verfiyTableCrudProcess.
protected void verfiyTableCrudProcess(String tabName, BasicDataXRdbmsReader dataxReader, ISelectedTab tab, IResultRows consumerHandle, IMQListener<JobExecutionResult> imqListener) throws com.qlangtech.tis.async.message.client.consumer.MQConsumeException, InterruptedException {
List<ISelectedTab> tabs = Collections.singletonList(tab);
List<TestRow> exampleRows = Lists.newArrayList();
Date now = new Date();
TestRow row = null;
Map<String, Object> vals = null;
int insertCount = 5;
for (int i = 1; i <= insertCount; i++) {
vals = Maps.newHashMap();
vals.put(keyBaseId, i);
vals.put("start_time", timeFormat.get().format(now));
vals.put("update_date", dateFormat.get().format(now));
vals.put("update_time", timeFormat.get().format(now));
vals.put("price", BigDecimal.valueOf(199, 2));
vals.put("json_content", "{\"name\":\"baisui#" + i + "\"}");
vals.put("col_blob", new ByteArrayInputStream("Hello world".getBytes(TisUTF8.get())));
vals.put(keyCol_text, "我爱北京天安门" + i);
row = new TestRow(RowKind.INSERT, new RowVals(vals));
row.idVal = i;
exampleRows.add(row);
}
// 执行三条更新
row = exampleRows.get(3);
row.updateVals.put(keyCol_text, (statement, index, ovals) -> {
String newVal = "update#" + ovals.getString(keyCol_text);
statement.setString(index, newVal);
return newVal;
});
row.updateVals.put(keyStart_time, (statement, index, ovals) -> {
String v = "2012-11-13 11:11:35";
statement.setTimestamp(index, parseTimestamp(v));
return v;
});
row = exampleRows.get(4);
row.updateVals.put(keyCol_text, (statement, index, ovals) -> {
String v = "update#" + ovals.getString(keyCol_text);
statement.setString(index, v);
return v;
});
row.updateVals.put(keyStart_time, (statement, index, ovals) -> {
String v = "2012-11-13 11:11:35";
statement.setTimestamp(index, parseTimestamp(v));
return v;
});
row = exampleRows.get(0);
row.updateVals.put(keyCol_text, (statement, index, ovals) -> {
String v = "update#" + ovals.getString(keyCol_text);
statement.setString(index, v);
return v;
});
row.updateVals.put(keyStart_time, (statement, index, ovals) -> {
String v = "2012-11-12 11:11:35";
statement.setTimestamp(index, parseTimestamp(v));
return v;
});
// 执行两条删除
row = exampleRows.get(1);
row.willbeDelete = true;
row = exampleRows.get(3);
row.willbeDelete = true;
imqListener.start(dataxName, dataxReader, tabs, null);
Thread.sleep(1000);
final String insertBase = "insert into " + createTableName(tabName) + "(" + cols.stream().map((col) -> getColEscape() + col.getName() + getColEscape()).collect(Collectors.joining(" , ")) + ") " + "values(" + cols.stream().map((col) -> "?").collect(Collectors.joining(" , ")) + ")";
CloseableIterator<Row> snapshot = consumerHandle.getRowSnapshot(tabName);
// insertCount
BasicDataSourceFactory dataSourceFactory = (BasicDataSourceFactory) dataxReader.getDataSourceFactory();
Assert.assertNotNull("dataSourceFactory can not be null", dataSourceFactory);
dataSourceFactory.visitFirstConnection((conn) -> {
startProcessConn(conn);
PreparedStatement statement = null;
try {
// 执行添加
System.out.println("start to insert");
for (TestRow r : exampleRows) {
statement = conn.prepareStatement(insertBase);
statement.setInt(1, r.getInt("base_id"));
statement.setTimestamp(2, parseTimestamp(r.getString("start_time")));
statement.setDate(3, new java.sql.Date(dateFormat.get().parse(r.getString("update_date")).getTime()));
statement.setTimestamp(4, parseTimestamp(r.getString("update_time")));
statement.setBigDecimal(5, r.getBigDecimal("price"));
statement.setString(6, r.getString("json_content"));
// statement.setBlob(7, r.getInputStream("col_blob"));
statement.setBinaryStream(7, r.getInputStream("col_blob"));
statement.setString(8, r.getString("col_text"));
Assert.assertEquals(1, executePreparedStatement(conn, statement));
statement.close();
sleepForAWhile();
System.out.println("wait to show insert rows");
waitForSnapshotStarted(snapshot);
List<TestRow> rows = fetchRows(snapshot, 1, false);
for (TestRow rr : rows) {
System.out.println("------------" + rr.getInt(keyBaseId));
assertTestRow(tabName, RowKind.INSERT, consumerHandle, r, rr);
}
// System.out.println("########################");
}
// 执行更新
for (TestRow exceptRow : exampleRows) {
if (!exceptRow.execUpdate()) {
continue;
}
List<Map.Entry<String, RowValsUpdate.UpdatedColVal>> cols = exceptRow.updateVals.getCols();
String updateSql = String.format("UPDATE " + createTableName(tabName) + " set %s WHERE base_id=%s", cols.stream().map((e) -> e.getKey() + " = ?").collect(Collectors.joining(",")), exceptRow.getIdVal());
statement = conn.prepareStatement(updateSql);
int colIndex = 1;
for (Map.Entry<String, RowValsUpdate.UpdatedColVal> col : cols) {
col.getValue().setPrepColVal(statement, colIndex++, exceptRow.vals);
}
Assert.assertTrue(updateSql, executePreparedStatement(conn, statement) > 0);
statement.close();
sleepForAWhile();
waitForSnapshotStarted(snapshot);
List<TestRow> rows = fetchRows(snapshot, 1, false);
for (TestRow rr : rows) {
// System.out.println("------------" + rr.getInt(keyBaseId));
assertTestRow(tabName, RowKind.UPDATE_AFTER, consumerHandle, exceptRow, rr);
}
}
// 执行删除
for (TestRow r : exampleRows) {
if (!r.execDelete()) {
continue;
}
String deleteSql = String.format("DELETE FROM " + createTableName(tabName) + " WHERE base_id=%s", r.getIdVal());
try (Statement statement1 = conn.createStatement()) {
Assert.assertTrue(deleteSql, executeStatement(conn, statement1, (deleteSql)) > 0);
sleepForAWhile();
waitForSnapshotStarted(snapshot);
List<TestRow> rows = fetchRows(snapshot, 1, true);
for (TestRow rr : rows) {
// System.out.println("------------" + rr.getInt(keyBaseId));
assertTestRow(tabName, RowKind.DELETE, consumerHandle, r, rr);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
use of com.qlangtech.tis.async.message.client.consumer.IMQListener in project plugins by qlangtech.
the class TestKafkaMQListener method testRegisterTopic.
public void testRegisterTopic() throws Exception {
TiKVKafkaMQListenerFactory listenerFactory = new TiKVKafkaMQListenerFactory();
listenerFactory.topic = "baisui";
listenerFactory.groupId = "test1";
listenerFactory.mqAddress = "192.168.28.201:9092";
listenerFactory.offsetResetStrategy = "latest";
IMQListener imqListener = listenerFactory.create();
// imqListener.setConsumerHandle(new MockConsumer());
// BasicDataSourceFactory dataSource, List<ISelectedTab > tabs, ISink sink
// imqListener.start();
Thread.sleep(99999999);
}
use of com.qlangtech.tis.async.message.client.consumer.IMQListener in project plugins by qlangtech.
the class TestFlinkCDCMySQLSourceFactory method testBinlogConsumeWithDataStreamRegisterInstaneDetailTable.
/**
* 测试 instancedetail
*
* @throws Exception
*/
@Test
public void testBinlogConsumeWithDataStreamRegisterInstaneDetailTable() throws Exception {
FlinkCDCMySQLSourceFactory mysqlCDCFactory = new FlinkCDCMySQLSourceFactory();
mysqlCDCFactory.startupOptions = "latest";
final String tabName = "instancedetail";
CUDCDCTestSuit cdcTestSuit = new CUDCDCTestSuit() {
@Override
protected BasicDataSourceFactory createDataSourceFactory(TargetResName dataxName) {
return createMySqlDataSourceFactory(dataxName);
}
@Override
protected String getColEscape() {
return "`";
}
@Override
protected IResultRows createConsumerHandle(String tabName) {
return new TestTableRegisterFlinkSourceHandle(tabName, cols);
}
@Override
protected void verfiyTableCrudProcess(String tabName, BasicDataXRdbmsReader dataxReader, ISelectedTab tab, IResultRows consumerHandle, IMQListener<JobExecutionResult> imqListener) throws MQConsumeException, InterruptedException {
// super.verfiyTableCrudProcess(tabName, dataxReader, tab, consumerHandle, imqListener);
List<ISelectedTab> tabs = Collections.singletonList(tab);
List<TestRow> exampleRows = Lists.newArrayList();
exampleRows.add(this.parseTestRow(RowKind.INSERT, TestFlinkCDCMySQLSourceFactory.class, tabName + "/insert1.txt"));
Assert.assertEquals(1, exampleRows.size());
imqListener.start(dataxName, dataxReader, tabs, null);
Thread.sleep(1000);
CloseableIterator<Row> snapshot = consumerHandle.getRowSnapshot(tabName);
BasicDataSourceFactory dataSourceFactory = (BasicDataSourceFactory) dataxReader.getDataSourceFactory();
Assert.assertNotNull("dataSourceFactory can not be null", dataSourceFactory);
dataSourceFactory.visitFirstConnection((conn) -> {
startProcessConn(conn);
for (TestRow t : exampleRows) {
RowVals<Object> vals = t.vals;
final String insertBase = "insert into " + createTableName(tabName) + "(" + cols.stream().filter((c) -> vals.notNull(c.getName())).map((col) -> getColEscape() + col.getName() + getColEscape()).collect(Collectors.joining(" , ")) + ") " + "values(" + cols.stream().filter((c) -> vals.notNull(c.getName())).map((col) -> "?").collect(Collectors.joining(" , ")) + ")";
PreparedStatement statement = conn.prepareStatement(insertBase);
AtomicInteger ci = new AtomicInteger();
cols.stream().filter((c) -> vals.notNull(c.getName())).forEach((col) -> {
col.type.accept(new DataType.TypeVisitor<Void>() {
@Override
public Void longType(DataType type) {
try {
statement.setLong(ci.incrementAndGet(), Long.parseLong(vals.getString(col.getName())));
} catch (SQLException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void doubleType(DataType type) {
try {
statement.setDouble(ci.incrementAndGet(), Double.parseDouble(vals.getString(col.getName())));
} catch (SQLException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void dateType(DataType type) {
try {
statement.setDate(ci.incrementAndGet(), java.sql.Date.valueOf(vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void timestampType(DataType type) {
try {
statement.setTimestamp(ci.incrementAndGet(), java.sql.Timestamp.valueOf(vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void bitType(DataType type) {
try {
statement.setByte(ci.incrementAndGet(), Byte.parseByte(vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void blobType(DataType type) {
try {
try (InputStream input = new ByteArrayInputStream(vals.getString(col.getName()).getBytes(TisUTF8.get()))) {
statement.setBlob(ci.incrementAndGet(), input);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void varcharType(DataType type) {
try {
statement.setString(ci.incrementAndGet(), (vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void intType(DataType type) {
try {
statement.setInt(ci.incrementAndGet(), Integer.parseInt(vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void floatType(DataType type) {
try {
statement.setFloat(ci.incrementAndGet(), Float.parseFloat(vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void decimalType(DataType type) {
try {
statement.setBigDecimal(ci.incrementAndGet(), BigDecimal.valueOf(Double.parseDouble(vals.getString(col.getName()))));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void timeType(DataType type) {
try {
statement.setTime(ci.incrementAndGet(), java.sql.Time.valueOf(vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void tinyIntType(DataType dataType) {
try {
statement.setShort(ci.incrementAndGet(), Short.parseShort(vals.getString(col.getName())));
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void smallIntType(DataType dataType) {
tinyIntType(dataType);
return null;
}
});
});
Assert.assertEquals(1, executePreparedStatement(conn, statement));
statement.close();
sleepForAWhile();
System.out.println("wait to show insert rows");
waitForSnapshotStarted(snapshot);
List<TestRow> rows = fetchRows(snapshot, 1, false);
for (TestRow rr : rows) {
System.out.println("------------" + rr.get("instance_id"));
assertTestRow(tabName, RowKind.INSERT, consumerHandle, t, rr);
}
}
});
}
};
cdcTestSuit.startTest(mysqlCDCFactory, tabName);
}
use of com.qlangtech.tis.async.message.client.consumer.IMQListener in project plugins by qlangtech.
the class TISFlinkCDCStart method deploy.
private static void deploy(TargetResName dataxName, BasicFlinkSourceHandle tableStreamHandle, ReplicasSpec incrSpec, long timestamp) throws Exception {
// BasicFlinkSourceHandle tisFlinkSourceHandle = new TISFlinkSourceHandle();
if (tableStreamHandle == null) {
throw new IllegalStateException("tableStreamHandle has not been instantiated");
}
// ElasticSearchSinkFactory esSinkFactory = new ElasticSearchSinkFactory();
// IPluginContext pluginContext = IPluginContext.namedContext(dataxName.getName());
// List<TISSinkFactory> sinkFactories = TISSinkFactory.sinkFactory.getPlugins(pluginContext, null);
// logger.info("sinkFactories size:" + sinkFactories.size());
// for (TISSinkFactory factory : sinkFactories) {
// sinkFactory = factory;
// break;
// }
// Objects.requireNonNull(sinkFactory, "sinkFactories.size():" + sinkFactories.size());
tableStreamHandle.setSinkFuncFactory(TISSinkFactory.getIncrSinKFactory(dataxName.getName()));
// List<MQListenerFactory> mqFactories = HeteroEnum.MQ.getPlugins(pluginContext, null);
MQListenerFactory mqFactory = HeteroEnum.getIncrSourceListenerFactory(dataxName.getName());
mqFactory.setConsumerHandle(tableStreamHandle);
// for (MQListenerFactory factory : mqFactories) {
// factory.setConsumerHandle(tableStreamHandle);
// mqFactory = factory;
// }
// Objects.requireNonNull(mqFactory, "mqFactory can not be null, mqFactories size:" + mqFactories.size());
IMQListener mq = mqFactory.create();
IDataxProcessor dataXProcess = DataxProcessor.load(null, dataxName.getName());
DataxReader reader = (DataxReader) dataXProcess.getReader(null);
if (reader == null) {
throw new IllegalStateException("dataXReader is illegal");
}
// DBConfigGetter rdbmsReader = (DBConfigGetter) reader;
List<ISelectedTab> tabs = reader.getSelectedTabs();
mq.start(dataxName, reader, tabs, dataXProcess);
}
use of com.qlangtech.tis.async.message.client.consumer.IMQListener in project plugins by qlangtech.
the class CUDCDCTestSuit method startTest.
public void startTest(MQListenerFactory cdcFactory, String tabName) throws Exception {
BasicDataXRdbmsReader dataxReader = createDataxReader(dataxName, tabName);
// replay();
List<SelectedTab> selectedTabs = dataxReader.getSelectedTabs();
Optional<SelectedTab> firstSelectedTab = selectedTabs.stream().filter((t) -> tabName.equals(t.name)).findFirst();
Assert.assertTrue("firstSelectedTab:" + tabName + " must be present", firstSelectedTab.isPresent());
ISelectedTab tab = firstSelectedTab.get();
this.cols = Lists.newArrayList();
HdfsColMeta cMeta = null;
for (ISelectedTab.ColMeta c : tab.getCols()) {
cMeta = new HdfsColMeta(c.getName(), c.isNullable(), c.isPk(), c.getType());
cols.add(cMeta);
}
IResultRows consumerHandle = getTestBasicFlinkSourceHandle(tabName);
cdcFactory.setConsumerHandle(consumerHandle.getConsumerHandle());
IMQListener<JobExecutionResult> imqListener = cdcFactory.create();
this.verfiyTableCrudProcess(tabName, dataxReader, tab, consumerHandle, imqListener);
consumerHandle.cancel();
}
Aggregations