Search in sources :

Example 1 with TisUTF8

use of com.qlangtech.tis.manage.common.TisUTF8 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);
        }
    });
}
Also used : LineIterator(org.apache.commons.io.LineIterator) java.sql(java.sql) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) TargetResName(com.qlangtech.tis.coredefine.module.action.TargetResName) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DTO(com.qlangtech.tis.realtime.transfer.DTO) BigDecimal(java.math.BigDecimal) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) TisUTF8(com.qlangtech.tis.manage.common.TisUTF8) MQListenerFactory(com.qlangtech.tis.async.message.client.consumer.impl.MQListenerFactory) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) ParseException(java.text.ParseException) TISSinkFactory(com.qlangtech.tis.plugin.incr.TISSinkFactory) SinkFunction(org.apache.flink.streaming.api.functions.sink.SinkFunction) BasicDataXRdbmsReader(com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsReader) RdbmsReaderContext(com.qlangtech.tis.plugin.datax.common.RdbmsReaderContext) com.qlangtech.tis.plugin.ds(com.qlangtech.tis.plugin.ds) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) JobExecutionResult(org.apache.flink.api.common.JobExecutionResult) HdfsColMeta(com.alibaba.datax.plugin.writer.hdfswriter.HdfsColMeta) IOUtils(org.apache.commons.io.IOUtils) TestBasicFlinkSourceHandle(com.qlangtech.plugins.incr.flink.cdc.source.TestBasicFlinkSourceHandle) SelectedTab(com.qlangtech.tis.plugin.datax.SelectedTab) CloseableIterator(org.apache.flink.util.CloseableIterator) RowKind(org.apache.flink.types.RowKind) Row(org.apache.flink.types.Row) Assert(org.junit.Assert) IMQListener(com.qlangtech.tis.async.message.client.consumer.IMQListener) java.sql(java.sql) Date(java.util.Date) ParseException(java.text.ParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) Row(org.apache.flink.types.Row)

Example 2 with TisUTF8

use of com.qlangtech.tis.manage.common.TisUTF8 in project tis by qlangtech.

the class DataXCfgGenerator method startGenerateCfg.

public GenerateCfgs startGenerateCfg(IGenerateScriptFile scriptFileGenerator) throws Exception {
    GenerateCfgs cfgs = new GenerateCfgs();
    // FileUtils.forceMkdir(dataXCfgDir);
    // // 先清空文件
    // FileUtils.cleanDirectory(dataXCfgDir);
    boolean unStructedReader = dataxProcessor.isReaderUnStructed(this.pluginCtx);
    IDataxReader reader = dataxProcessor.getReader(this.pluginCtx);
    IDataxWriter writer = dataxProcessor.getWriter(this.pluginCtx);
    DataxWriter.BaseDataxWriterDescriptor writerDescriptor = writer.getWriterDescriptor();
    Map<String, IDataxProcessor.TableAlias> tabAlias = dataxProcessor.getTabAlias();
    AtomicReference<Map<String, ISelectedTab>> selectedTabsRef = new AtomicReference<>();
    java.util.concurrent.Callable<Map<String, ISelectedTab>> selectedTabsCall = () -> {
        if (selectedTabsRef.get() == null) {
            Map<String, ISelectedTab> selectedTabs = reader.getSelectedTabs().stream().collect(Collectors.toMap((t) -> t.getName(), (t) -> t));
            selectedTabsRef.set(selectedTabs);
        }
        return selectedTabsRef.get();
    };
    List<String> existDDLFiles = getExistDDLFiles();
    IGroupChildTaskIterator subTasks = reader.getSubTasks();
    IDataxReaderContext readerContext = null;
    File configFile = null;
    // List<String> subTaskName = Lists.newArrayList();
    Set<String> createDDLFiles = Sets.newHashSet();
    Optional<IDataxProcessor.TableMap> tableMapper = null;
    while (subTasks.hasNext()) {
        readerContext = subTasks.next();
        if (!dataxProcessor.isWriterSupportMultiTableInReader(this.pluginCtx)) {
            if (tabAlias.size() == 1) {
                // 针对ES的情况
                Optional<IDataxProcessor.TableMap> first = tabAlias.values().stream().filter((t) -> t instanceof IDataxProcessor.TableMap).map((t) -> (IDataxProcessor.TableMap) t).findFirst();
                if (first.isPresent()) {
                    tableMapper = first;
                }
            } else {
                // IDataxWriter writer = dataxProcessor.getWriter(this.pluginCtx);
                if (writer instanceof IDataxProcessor.INullTableMapCreator) {
                    tableMapper = Optional.empty();
                }
            }
            Objects.requireNonNull(tableMapper, "tabMapper can not be null,tabAlias.size()=" + tabAlias.size() + ",tabs:[" + tabAlias.keySet().stream().collect(Collectors.joining(",")) + "]");
        } else if (unStructedReader) {
            // 是在DataxAction的doSaveWriterColsMeta() 方法中持久化保存的
            for (IDataxProcessor.TableAlias tab : tabAlias.values()) {
                tableMapper = Optional.of((IDataxProcessor.TableMap) tab);
                break;
            }
            Objects.requireNonNull(tableMapper, "tableMap can not be null");
        } else if (dataxProcessor.isRDBMS2UnStructed(this.pluginCtx)) {
            // example: mysql -> oss
            Map<String, ISelectedTab> selectedTabs = selectedTabsCall.call();
            ISelectedTab tab = selectedTabs.get(readerContext.getSourceEntityName());
            Objects.requireNonNull(tab, readerContext.getSourceEntityName() + " relevant tab can not be null");
            IDataxProcessor.TableMap m = new IDataxProcessor.TableMap(tab);
            // m.setSourceCols(tab.getCols());
            m.setTo(tab.getName());
            m.setFrom(tab.getName());
            tableMapper = Optional.of(m);
        } else if (dataxProcessor.isRDBMS2RDBMS(this.pluginCtx)) {
            // example: mysql -> mysql
            tableMapper = Optional.of(createTableMap(tabAlias, selectedTabsCall.call(), readerContext));
        } else {
            // tableMapper = Optional.of(createTableMap(tabAlias, selectedTabsCall.call(), readerContext));
            throw new IllegalStateException("unexpect status");
        }
        scriptFileGenerator.generateScriptFile(reader, writer, readerContext, createDDLFiles, tableMapper);
    // generateScriptFile(dataXCfgDir, reader, writer, readerContext, subTaskName, createDDLFiles, tableMapper);
    }
    // 将老的已经没有用的ddl sql文件删除调
    File createDDLDir = this.dataxProcessor.getDataxCreateDDLDir(this.pluginCtx);
    for (String oldDDLFile : existDDLFiles) {
        if (!createDDLFiles.contains(oldDDLFile)) {
            FileUtils.deleteQuietly(new File(createDDLDir, oldDDLFile));
        }
    }
    long current = System.currentTimeMillis();
    // FileUtils.write(new File(dataXCfgDir, FILE_GEN), String.valueOf(current), TisUTF8.get(), false);
    cfgs.createDDLFiles = Lists.newArrayList(createDDLFiles);
    cfgs.groupedChildTask = subTasks.getGroupedInfo();
    // cfgs.dataxFiles = subTaskName;
    cfgs.genTime = current;
    return cfgs;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) StringWriter(java.io.StringWriter) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) IPluginContext(com.qlangtech.tis.util.IPluginContext) VelocityContext(org.apache.velocity.VelocityContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) File(java.io.File) JsonUtil(com.qlangtech.tis.trigger.util.JsonUtil) JSONArray(com.alibaba.fastjson.JSONArray) com.qlangtech.tis.datax(com.qlangtech.tis.datax) JSON(com.alibaba.fastjson.JSON) Lists(com.google.common.collect.Lists) VelocityEngine(org.apache.velocity.app.VelocityEngine) CollectionUtils(org.apache.commons.collections.CollectionUtils) TisUTF8(com.qlangtech.tis.manage.common.TisUTF8) DataxUtils(com.qlangtech.tis.offline.DataxUtils) JSONObject(com.alibaba.fastjson.JSONObject) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File)

Example 3 with TisUTF8

use of com.qlangtech.tis.manage.common.TisUTF8 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);
}
Also used : TargetResName(com.qlangtech.tis.coredefine.module.action.TargetResName) CUDCDCTestSuit(com.qlangtech.plugins.incr.flink.cdc.CUDCDCTestSuit) IResultRows(com.qlangtech.plugins.incr.flink.cdc.IResultRows) TIS(com.qlangtech.tis.TIS) RowVals(com.qlangtech.plugins.incr.flink.cdc.RowVals) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) BigDecimal(java.math.BigDecimal) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) CenterResource(com.qlangtech.tis.manage.common.CenterResource) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TisUTF8(com.qlangtech.tis.manage.common.TisUTF8) TestTableRegisterFlinkSourceHandle(com.qlangtech.plugins.incr.flink.cdc.source.TestTableRegisterFlinkSourceHandle) TestRow(com.qlangtech.plugins.incr.flink.cdc.TestRow) Before(org.junit.Before) MQConsumeException(com.qlangtech.tis.async.message.client.consumer.MQConsumeException) BasicDataXRdbmsReader(com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsReader) Descriptor(com.qlangtech.tis.extension.Descriptor) Test(org.junit.Test) PreparedStatement(java.sql.PreparedStatement) Collectors(java.util.stream.Collectors) JobExecutionResult(org.apache.flink.api.common.JobExecutionResult) TISEasyMock(com.qlangtech.tis.test.TISEasyMock) CloseableIterator(org.apache.flink.util.CloseableIterator) List(java.util.List) RowKind(org.apache.flink.types.RowKind) DataType(com.qlangtech.tis.plugin.ds.DataType) Row(org.apache.flink.types.Row) Assert(org.junit.Assert) IMQListener(com.qlangtech.tis.async.message.client.consumer.IMQListener) Collections(java.util.Collections) InputStream(java.io.InputStream) BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) SQLException(java.sql.SQLException) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) CUDCDCTestSuit(com.qlangtech.plugins.incr.flink.cdc.CUDCDCTestSuit) IResultRows(com.qlangtech.plugins.incr.flink.cdc.IResultRows) IMQListener(com.qlangtech.tis.async.message.client.consumer.IMQListener) TargetResName(com.qlangtech.tis.coredefine.module.action.TargetResName) DataType(com.qlangtech.tis.plugin.ds.DataType) BasicDataXRdbmsReader(com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TestRow(com.qlangtech.plugins.incr.flink.cdc.TestRow) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) MQConsumeException(com.qlangtech.tis.async.message.client.consumer.MQConsumeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) TestTableRegisterFlinkSourceHandle(com.qlangtech.plugins.incr.flink.cdc.source.TestTableRegisterFlinkSourceHandle) TestRow(com.qlangtech.plugins.incr.flink.cdc.TestRow) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 4 with TisUTF8

use of com.qlangtech.tis.manage.common.TisUTF8 in project plugins by qlangtech.

the class TestTotalpayProducter method consumeFile.

// public void testConsumeFile() throws Exception {
// this.consumeFile();
// }
private void consumeFile(DefaultMQProducer producer) throws Exception {
    GetObjectRequest getObjectRequest = new GetObjectRequest("incr-log", ossPathMap.get("order"));
    // getObjectRequest.setRange(3000, -1);
    OSSObject object = client.getObject(getObjectRequest);
    LineIterator lit = null;
    Message msg = null;
    JSONObject m = null;
    String line = null;
    String tag = null;
    AtomicInteger incr = null;
    ConcurrentHashMap<String, AtomicInteger> statis = new ConcurrentHashMap();
    long lastTimestamp = 0;
    long current;
    int allcount = 0;
    try (InputStream input = object.getObjectContent()) {
        lit = IOUtils.lineIterator(input, TisUTF8.get());
        while (lit.hasNext()) {
            try {
                line = lit.nextLine();
                allcount++;
                m = JSON.parseObject(line);
                tag = m.getString("orginTableName");
                msg = createMsg(line, tag);
                if ((incr = statis.get(tag)) == null) {
                    incr = statis.computeIfAbsent(tag, (key) -> new AtomicInteger());
                }
                incr.incrementAndGet();
                SendResult sendResult = producer.send(msg);
                Thread.sleep(10);
                current = System.currentTimeMillis();
                if (current > (lastTimestamp + 5000)) {
                    System.out.println("<---------------------------");
                    System.out.println(statis.entrySet().stream().map((e) -> e.getKey() + ":" + e.getValue().get()).collect(Collectors.joining("\n")));
                    System.out.println("allcount:" + allcount);
                    lastTimestamp = current;
                    System.out.println("--------------------------->");
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new IllegalStateException("line:" + line, e);
            }
        }
    }
// int count = 0;hh
// LineIterator lit = FileUtils.lineIterator(new File("/Users/mozhenghua/Downloads/s4shop"), TisUTF8.getName());
// JSONObject msg = null;
// JSONObject before = null;
// JSONObject after = null;
// while (lit.hasNext()) {
// count++;
// msg = JSON.parseObject(lit.nextLine());
// before = msg.getJSONObject("before");
// after = msg.getJSONObject("after");
// msg.getString("dbName");
// msg.getString("eventType");
// msg.getString("orginTableName");
// msg.getString("targetTable");
// }
// System.out.println("count:" + count);
}
Also used : LineIterator(org.apache.commons.io.LineIterator) Message(org.apache.rocketmq.common.message.Message) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OSS(com.aliyun.oss.OSS) OSSObject(com.aliyun.oss.model.OSSObject) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) ClientLogger(org.apache.rocketmq.client.log.ClientLogger) IOUtils(org.apache.commons.io.IOUtils) JSON(com.alibaba.fastjson.JSON) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TisUTF8(com.qlangtech.tis.manage.common.TisUTF8) SendResult(org.apache.rocketmq.client.producer.SendResult) JSONObject(com.alibaba.fastjson.JSONObject) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) InputStream(java.io.InputStream) OSSObject(com.aliyun.oss.model.OSSObject) Message(org.apache.rocketmq.common.message.Message) InputStream(java.io.InputStream) LineIterator(org.apache.commons.io.LineIterator) JSONObject(com.alibaba.fastjson.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SendResult(org.apache.rocketmq.client.producer.SendResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest)

Example 5 with TisUTF8

use of com.qlangtech.tis.manage.common.TisUTF8 in project tis by qlangtech.

the class SqlTaskNodeMeta method persistence.

/**
 * 将对象持久化
 *
 * @param topology
 */
public static void persistence(SqlDataFlowTopology topology, File parent) throws Exception {
    if (!parent.exists()) {
        throw new IllegalStateException("parent not exist:" + parent.getAbsolutePath());
    }
    if (topology.profile == null || StringUtils.isEmpty(topology.getName()) || topology.getTimestamp() < 1 || topology.getDataflowId() < 1) {
        throw new IllegalArgumentException("param topology's prop name timestamp or dataflowid neither can be null");
    }
    Pattern PatternjoinNode = Pattern.compile("[\\da-z]+\\-[\\da-z]+\\-[\\da-z]+\\-[\\da-z]+\\-[\\da-z]+\\.yaml");
    // 用来处理被删除的节点,如果某个节点被删除的话对应的AtomicBoolean flag 就为false
    Map<String, AtomicBoolean> oldNodeFileStats = Arrays.stream(parent.list((dir, name) -> PatternjoinNode.matcher(name).matches())).collect(Collectors.toMap((filename) -> filename, (filename) -> new AtomicBoolean()));
    String nodeFileName = null;
    AtomicBoolean hasProcess = null;
    for (SqlTaskNodeMeta process : topology.getNodeMetas()) {
        if (StringUtils.isEmpty(process.getId())) {
            throw new IllegalStateException(process.getExportName() + " relevant node property id can not be null ");
        }
        nodeFileName = (process.getId() + ".yaml");
        hasProcess = oldNodeFileStats.get(nodeFileName);
        if (hasProcess != null) {
            hasProcess.set(true);
        }
        try (OutputStreamWriter output = new OutputStreamWriter(FileUtils.openOutputStream(new File(parent, nodeFileName)))) {
            yaml.dump(process, output);
        }
    }
    oldNodeFileStats.entrySet().forEach((e) -> {
        // 老文件 没有被处理 说明已经被删除了
        if (!e.getValue().get()) {
            FileUtils.deleteQuietly(new File(parent, e.getKey()));
        }
    });
    try (OutputStreamWriter output = new OutputStreamWriter(FileUtils.openOutputStream(new File(parent, FILE_NAME_DEPENDENCY_TABS)))) {
        yaml.dump(new DumpNodes(topology.getDumpNodes()), output);
    }
    try (OutputStreamWriter output = new OutputStreamWriter(FileUtils.openOutputStream(new File(parent, FILE_NAME_PROFILE)), TisUTF8.get())) {
        JSONObject profile = new JSONObject();
        profile.put(KEY_PROFILE_TIMESTAMP, topology.getTimestamp());
        profile.put(KEY_PROFILE_TOPOLOGY, topology.getName());
        profile.put(KEY_PROFILE_ID, topology.getDataflowId());
        IOUtils.write(profile.toJSONString(), output);
    }
}
Also used : LineIterator(org.apache.commons.io.LineIterator) IDumpTable(com.qlangtech.tis.fullbuild.indexbuild.IDumpTable) TableMeta(com.qlangtech.tis.sql.parser.er.TableMeta) StringUtils(org.apache.commons.lang.StringUtils) ColumnTransfer(com.qlangtech.tis.sql.parser.meta.ColumnTransfer) TIS(com.qlangtech.tis.TIS) DefaultDumpNodeMapContext(com.qlangtech.tis.sql.parser.utils.DefaultDumpNodeMapContext) Node(org.yaml.snakeyaml.nodes.Node) ExecutePhaseRange(com.qlangtech.tis.exec.ExecutePhaseRange) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScalarStyle(org.yaml.snakeyaml.DumperOptions.ScalarStyle) Method(java.lang.reflect.Method) ITabPartition(com.qlangtech.tis.fullbuild.indexbuild.ITabPartition) DependencyNode(com.qlangtech.tis.sql.parser.meta.DependencyNode) EntityName(com.qlangtech.tis.sql.parser.tuple.creator.EntityName) IJoinTaskContext(com.qlangtech.tis.order.center.IJoinTaskContext) Property(org.yaml.snakeyaml.introspector.Property) Collectors(java.util.stream.Collectors) SqlParser(com.facebook.presto.sql.parser.SqlParser) IOUtils(org.apache.commons.io.IOUtils) LineBreak(org.yaml.snakeyaml.DumperOptions.LineBreak) JSONObject(com.alibaba.fastjson.JSONObject) Pattern(java.util.regex.Pattern) ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) Joiner(com.google.common.base.Joiner) Types(java.sql.Types) java.util(java.util) JSONField(com.alibaba.fastjson.annotation.JSONField) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TableTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator) Yaml(org.yaml.snakeyaml.Yaml) DumperOptions(org.yaml.snakeyaml.DumperOptions) IAppSourcePipelineController(com.qlangtech.tis.order.center.IAppSourcePipelineController) Lists(com.google.common.collect.Lists) TabFieldProcessor(com.qlangtech.tis.sql.parser.er.TabFieldProcessor) TisUTF8(com.qlangtech.tis.manage.common.TisUTF8) Position(com.qlangtech.tis.sql.parser.meta.Position) Constructor(org.yaml.snakeyaml.constructor.Constructor) NodeType(com.qlangtech.tis.sql.parser.meta.NodeType) ExceptionUtils(org.apache.commons.lang.exception.ExceptionUtils) com.qlangtech.tis.plugin.ds(com.qlangtech.tis.plugin.ds) FileUtils(org.apache.commons.io.FileUtils) Tag(org.yaml.snakeyaml.nodes.Tag) Maps(com.google.common.collect.Maps) TisSqlFormatException(com.qlangtech.tis.sql.parser.exception.TisSqlFormatException) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) FlowStyle(org.yaml.snakeyaml.DumperOptions.FlowStyle) JSON(com.alibaba.fastjson.JSON) ERRules(com.qlangtech.tis.sql.parser.er.ERRules) Expression(com.facebook.presto.sql.tree.Expression) ITemplateContext(com.qlangtech.tis.fullbuild.taskflow.ITemplateContext) java.io(java.io) IPrimaryTabFinder(com.qlangtech.tis.sql.parser.er.IPrimaryTabFinder) Representer(org.yaml.snakeyaml.representer.Representer) TypeDescription(org.yaml.snakeyaml.TypeDescription) Statement(com.facebook.presto.sql.tree.Statement) Pattern(java.util.regex.Pattern) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

TisUTF8 (com.qlangtech.tis.manage.common.TisUTF8)5 Collectors (java.util.stream.Collectors)5 Lists (com.google.common.collect.Lists)4 JSON (com.alibaba.fastjson.JSON)3 JSONObject (com.alibaba.fastjson.JSONObject)3 Maps (com.google.common.collect.Maps)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IOUtils (org.apache.commons.io.IOUtils)3 TIS (com.qlangtech.tis.TIS)2 IMQListener (com.qlangtech.tis.async.message.client.consumer.IMQListener)2 TargetResName (com.qlangtech.tis.coredefine.module.action.TargetResName)2 BasicDataXRdbmsReader (com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsReader)2 com.qlangtech.tis.plugin.ds (com.qlangtech.tis.plugin.ds)2 InputStream (java.io.InputStream)2 java.util (java.util)2 LineIterator (org.apache.commons.io.LineIterator)2 StringUtils (org.apache.commons.lang.StringUtils)2 HdfsColMeta (com.alibaba.datax.plugin.writer.hdfswriter.HdfsColMeta)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONField (com.alibaba.fastjson.annotation.JSONField)1