Search in sources :

Example 1 with ColumnParser

use of org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser in project rocketmq-externals by apache.

the class Querier method poll.

public void poll() {
    try {
        LinkedList<Table> tableLinkedList = new LinkedList<>();
        for (Map.Entry<String, Database> entry : schema.getDbMap().entrySet()) {
            String dbName = entry.getKey();
            Iterator<Map.Entry<String, Table>> iterator = entry.getValue().getTableMap().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Table> tableEntry = iterator.next();
                String tableName = tableEntry.getKey();
                Table table = tableEntry.getValue();
                Map<String, String> tableFilterMap = table.getFilterMap();
                Select selectFrom = QueryBuilder.selectFrom(dbName, tableName).all();
                if (tableFilterMap != null && !tableFilterMap.keySet().contains("NO-FILTER")) {
                    for (String key : tableFilterMap.keySet()) {
                        String value = tableFilterMap.get(key);
                        selectFrom.whereColumn(key).isEqualTo(QueryBuilder.literal(value));
                    }
                }
                SimpleStatement stmt;
                boolean finishUpdate = false;
                log.info("trying to execute sql query,{}", selectFrom.asCql());
                ResultSet result = null;
                while (!cqlSession.isClosed() && !finishUpdate) {
                    stmt = selectFrom.build();
                    result = cqlSession.execute(stmt);
                    if (result.wasApplied()) {
                        log.info("query columns success, executed cql query {}", selectFrom.asCql());
                    }
                    finishUpdate = true;
                }
                List<String> colList = tableEntry.getValue().getColList();
                List<String> dataTypeList = tableEntry.getValue().getRawDataTypeList();
                List<ColumnParser> parserList = tableEntry.getValue().getParserList();
                for (Row row : result) {
                    Table tableWithData = new Table(dbName, tableName);
                    tableWithData.setColList(colList);
                    tableWithData.setRawDataTypeList(dataTypeList);
                    tableWithData.setParserList(parserList);
                    for (String col : colList) {
                        tableWithData.getDataList().add(row.getObject(col));
                    }
                    tableLinkedList.add(tableWithData);
                }
            }
        }
        list = tableLinkedList;
    } catch (Exception e) {
        log.error("fail to poll data, {}", e);
    }
}
Also used : Table(org.apache.rocketmq.connect.cassandra.schema.Table) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) LinkedList(java.util.LinkedList) ColumnParser(org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser) Database(org.apache.rocketmq.connect.cassandra.schema.Database) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ColumnParser

use of org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser in project rocketmq-externals by apache.

the class Database method init.

public void init() {
    Select selectFrom = QueryBuilder.selectFrom(SCHEMA_SYSTEM_SCHEMA, TABLE_COLUMNS).all().whereColumn(COLUMN_KEYSPACE_NAME).isEqualTo(QueryBuilder.literal(name));
    SimpleStatement stmt;
    boolean finishUpdate = false;
    LOGGER.info("trying to execute sql query,{}", selectFrom.asCql());
    ResultSet result = null;
    try {
        while (!cqlSession.isClosed() && !finishUpdate) {
            stmt = selectFrom.build();
            result = cqlSession.execute(stmt);
            if (result.wasApplied()) {
                LOGGER.info("query columns success, executed cql query {}", selectFrom.asCql());
            }
            finishUpdate = true;
        }
        for (Row row : result) {
            String tableName = row.getString(COLUMN_TABLE_NAME);
            String columnName = row.getString(COLUMN_COLUMN_NAME);
            String columnType = row.getString(COLUMN_TYPE);
            ColumnParser columnParser = ColumnParser.getColumnParser(columnType, columnType, GENERAL_CHARSET);
            if (!tableWhiteList.contains(tableName)) {
                continue;
            }
            if (!tableMap.containsKey(tableName)) {
                addTable(tableName);
            }
            Table table = tableMap.get(tableName);
            table.addCol(columnName);
            table.addParser(columnParser);
            table.addRawDataType(columnType);
            table.setFilterMap(tableFilterMap.get(tableName));
        }
    } catch (Exception e) {
        LOGGER.error("init cassandra Schema failure,{}", e);
    }
}
Also used : ColumnParser(org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row)

Aggregations

ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 Select (com.datastax.oss.driver.api.querybuilder.select.Select)2 ColumnParser (org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Database (org.apache.rocketmq.connect.cassandra.schema.Database)1 Table (org.apache.rocketmq.connect.cassandra.schema.Table)1