Search in sources :

Example 1 with Table

use of org.apache.rocketmq.connect.jdbc.schema.Table 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)

Example 2 with Table

use of org.apache.rocketmq.connect.jdbc.schema.Table in project rocketmq-externals by apache.

the class Querier method poll.

public void poll() {
    try {
        PreparedStatement stmt;
        LinkedList<Table> tableLinkedList = new LinkedList<>();
        for (Map.Entry<String, Database> entry : schema.getDbMap().entrySet()) {
            String db = entry.getKey();
            Iterator<Map.Entry<String, Table>> iterator = entry.getValue().getTableMap().entrySet().iterator();
            while (iterator.hasNext()) {
                StringBuilder query = new StringBuilder("select * from ");
                Map.Entry<String, Table> tableEntry = iterator.next();
                String tb = tableEntry.getKey();
                query.append(db + "." + tb);
                Table t = tableEntry.getValue();
                Map<String, String> tableFilterMap = t.getFilterMap();
                if (tableFilterMap != null && !tableFilterMap.keySet().contains("NO-FILTER")) {
                    query = query.append(" where ");
                    int count = 0;
                    for (String key : tableFilterMap.keySet()) {
                        count++;
                        String value = tableFilterMap.get(key);
                        if (count != 1) {
                            query.append(" and ");
                        }
                        String condition = key + "=" + "'" + value + "'";
                        query.append(condition);
                    }
                }
                stmt = connection.prepareStatement(query.toString());
                ResultSet rs;
                rs = stmt.executeQuery();
                List<String> colList = tableEntry.getValue().getColList();
                List<String> dataTypeList = tableEntry.getValue().getRawDataTypeList();
                List<ColumnParser> parserList = tableEntry.getValue().getParserList();
                while (rs.next()) {
                    Table table = new Table(db, tb);
                    // System.out.print("|");
                    table.setColList(colList);
                    table.setRawDataTypeList(dataTypeList);
                    table.setParserList(parserList);
                    for (String string : colList) {
                        table.getDataList().add(rs.getObject(string));
                    // System.out.print(string + " : " + rs.getObject(string) + "|");
                    }
                    tableLinkedList.add(table);
                }
                rs.close();
                stmt.close();
            }
        }
        list = tableLinkedList;
    } catch (SQLException e) {
        log.error("fail to poll data, {}", e);
    }
}
Also used : Table(org.apache.rocketmq.connect.jdbc.schema.Table) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) ColumnParser(org.apache.rocketmq.connect.jdbc.schema.column.ColumnParser) Database(org.apache.rocketmq.connect.jdbc.schema.Database) ResultSet(java.sql.ResultSet)

Example 3 with Table

use of org.apache.rocketmq.connect.jdbc.schema.Table in project rocketmq-externals by apache.

the class TimestampIncrementingQuerier method poll.

public void poll() {
    try {
        list.clear();
        Connection conn = dataSource.getConnection();
        for (Map.Entry<String, Database> entry : schema.getDbMap().entrySet()) {
            String db = entry.getKey();
            log.info("{} database is loading", db);
            Iterator<Map.Entry<String, Table>> iterator = entry.getValue().getTableMap().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Table> tableEntry = iterator.next();
                String tb = tableEntry.getKey();
                log.info("{} table is loading", tb);
                name = db + "." + tb;
                storeRecord(name);
                createPreparedStatement(conn);
                ResultSet rs;
                rs = executeQuery();
                List<String> colList = tableEntry.getValue().getColList();
                List<String> DataTypeList = tableEntry.getValue().getRawDataTypeList();
                List<ColumnParser> ParserList = tableEntry.getValue().getParserList();
                while (rs.next()) {
                    Table table = new Table(db, tb);
                    System.out.print("|");
                    table.setColList(colList);
                    table.setRawDataTypeList(DataTypeList);
                    table.setParserList(ParserList);
                    for (String string : colList) {
                        table.getDataList().add(rs.getObject(string));
                        System.out.print(string + " : " + rs.getObject(string) + "|");
                    }
                    incrementingOffset = incrementingOffset > rs.getInt(incrementingColumn) ? incrementingOffset : rs.getInt(incrementingColumn);
                    timestampOffset = timestampOffset > rs.getTimestamp(timestampColumn).getTime() ? timestampOffset : rs.getTimestamp(timestampColumn).getTime();
                    System.out.println(timestampOffset);
                    list.add(table);
                    System.out.println();
                }
                extractRecord(name);
                incrementingOffset = 0L;
                timestampOffset = 0L;
            }
        }
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Also used : Table(org.apache.rocketmq.connect.jdbc.schema.Table) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ColumnParser(org.apache.rocketmq.connect.jdbc.schema.column.ColumnParser) Database(org.apache.rocketmq.connect.jdbc.schema.Database) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Table (org.apache.rocketmq.connect.jdbc.schema.Table)3 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Database (org.apache.rocketmq.connect.jdbc.schema.Database)2 ColumnParser (org.apache.rocketmq.connect.jdbc.schema.column.ColumnParser)2 JSONObject (com.alibaba.fastjson.JSONObject)1 DataEntryBuilder (io.openmessaging.connector.api.data.DataEntryBuilder)1 Field (io.openmessaging.connector.api.data.Field)1 Schema (io.openmessaging.connector.api.data.Schema)1 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1