Search in sources :

Example 1 with ResultSetMapper

use of org.skife.jdbi.v2.tweak.ResultSetMapper in project druid by druid-io.

the class SQLMetadataSegmentManager method poll.

@Override
public void poll() {
    try {
        if (!started) {
            return;
        }
        ConcurrentHashMap<String, DruidDataSource> newDataSources = new ConcurrentHashMap<String, DruidDataSource>();
        log.debug("Starting polling of segment table");
        // some databases such as PostgreSQL require auto-commit turned off
        // to stream results back, enabling transactions disables auto-commit
        //
        // setting connection to read-only will allow some database such as MySQL
        // to automatically use read-only transaction mode, further optimizing the query
        final List<DataSegment> segments = connector.inReadOnlyTransaction(new TransactionCallback<List<DataSegment>>() {

            @Override
            public List<DataSegment> inTransaction(Handle handle, TransactionStatus status) throws Exception {
                return handle.createQuery(String.format("SELECT payload FROM %s WHERE used=true", getSegmentsTable())).setFetchSize(connector.getStreamingFetchSize()).map(new ResultSetMapper<DataSegment>() {

                    @Override
                    public DataSegment map(int index, ResultSet r, StatementContext ctx) throws SQLException {
                        try {
                            return DATA_SEGMENT_INTERNER.intern(jsonMapper.readValue(r.getBytes("payload"), DataSegment.class));
                        } catch (IOException e) {
                            log.makeAlert(e, "Failed to read segment from db.");
                            return null;
                        }
                    }
                }).list();
            }
        });
        if (segments == null || segments.isEmpty()) {
            log.warn("No segments found in the database!");
            return;
        }
        final Collection<DataSegment> segmentsFinal = Collections2.filter(segments, Predicates.notNull());
        log.info("Polled and found %,d segments in the database", segments.size());
        for (final DataSegment segment : segmentsFinal) {
            String datasourceName = segment.getDataSource();
            DruidDataSource dataSource = newDataSources.get(datasourceName);
            if (dataSource == null) {
                dataSource = new DruidDataSource(datasourceName, ImmutableMap.of("created", new DateTime().toString()));
                Object shouldBeNull = newDataSources.put(datasourceName, dataSource);
                if (shouldBeNull != null) {
                    log.warn("Just put key[%s] into dataSources and what was there wasn't null!?  It was[%s]", datasourceName, shouldBeNull);
                }
            }
            if (!dataSource.getSegments().contains(segment)) {
                dataSource.addSegment(segment.getIdentifier(), segment);
            }
        }
        synchronized (lock) {
            if (started) {
                dataSources.set(newDataSources);
            }
        }
    } catch (Exception e) {
        log.makeAlert(e, "Problem polling DB.").emit();
    }
}
Also used : SQLException(java.sql.SQLException) TransactionStatus(org.skife.jdbi.v2.TransactionStatus) IOException(java.io.IOException) DruidDataSource(io.druid.client.DruidDataSource) DataSegment(io.druid.timeline.DataSegment) SQLException(java.sql.SQLException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) Handle(org.skife.jdbi.v2.Handle) StatementContext(org.skife.jdbi.v2.StatementContext) ResultSet(java.sql.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ResultSetMapper

use of org.skife.jdbi.v2.tweak.ResultSetMapper in project Rosetta by HubSpot.

the class RosettaMapperFactory method mapperFor.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public ResultSetMapper mapperFor(Class rawType, StatementContext ctx) {
    ObjectMapper objectMapper = RosettaObjectMapperOverride.resolve(ctx);
    final Type genericType;
    if (ctx.getSqlObjectMethod() == null) {
        genericType = rawType;
    } else {
        genericType = determineGenericReturnType(rawType, ctx.getSqlObjectMethod().getGenericReturnType());
    }
    final RosettaMapper mapper = new RosettaMapper(genericType, objectMapper, extractTableName(ctx.getRewrittenSql()));
    return new ResultSetMapper() {

        @Override
        public Object map(int index, ResultSet r, StatementContext ctx) throws SQLException {
            return mapper.mapRow(r);
        }
    };
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ResultSetMapper(org.skife.jdbi.v2.tweak.ResultSetMapper) ResultSet(java.sql.ResultSet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RosettaMapper(com.hubspot.rosetta.RosettaMapper) StatementContext(org.skife.jdbi.v2.StatementContext)

Example 3 with ResultSetMapper

use of org.skife.jdbi.v2.tweak.ResultSetMapper in project irontest by zheng-wang.

the class DBTeststepRunner method run.

protected BasicTeststepRun run(Teststep teststep) throws Exception {
    BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
    DBAPIResponse response = new DBAPIResponse();
    String request = (String) teststep.getRequest();
    Endpoint endpoint = teststep.getEndpoint();
    DBI jdbi = new DBI(endpoint.getUrl(), endpoint.getUsername(), getDecryptedEndpointPassword());
    // get SQL statements (trimmed and without comments) and JDBI script object
    List<String> statements = IronTestUtils.getStatements(request);
    sanityCheckTheStatements(statements);
    Handle handle = jdbi.open();
    if (SQLStatementType.isSelectStatement(statements.get(0))) {
        // the request is a select statement
        RetainingColumnOrderResultSetMapper resultSetMapper = new RetainingColumnOrderResultSetMapper();
        // use statements.get(0) instead of the raw request, as Oracle does not support trailing semicolon in select statement
        Query<Map<String, Object>> query = handle.createQuery(statements.get(0)).map(resultSetMapper);
        // obtain columnNames in case the query returns no row
        final List<String> columnNames = new ArrayList<String>();
        query.addStatementCustomizer(new BaseStatementCustomizer() {

            public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException {
                ResultSetMetaData metaData = stmt.getMetaData();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    columnNames.add(metaData.getColumnLabel(i).toLowerCase());
                }
            }
        });
        // limit the number of returned rows
        List<Map<String, Object>> rows = query.list(5000);
        response.setColumnNames(columnNames);
        response.setRowsJSON(jacksonObjectMapper.writeValueAsString(rows));
    } else {
        // the request is one or more non-select statements
        Script script = handle.createScript(request);
        int[] returnValues = script.execute();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < returnValues.length; i++) {
            String statementType = SQLStatementType.getByStatement(statements.get(i)).toString();
            sb.append(returnValues[i]).append(" row(s) ").append(statementType.toLowerCase()).append(statementType.endsWith("E") ? "d" : "ed").append("\n");
            response.setStatementExecutionResults(sb.toString());
        }
    }
    handle.close();
    basicTeststepRun.setResponse(response);
    return basicTeststepRun;
}
Also used : BaseStatementCustomizer(org.skife.jdbi.v2.tweak.BaseStatementCustomizer) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Endpoint(io.irontest.models.endpoint.Endpoint) ResultSetMetaData(java.sql.ResultSetMetaData) Endpoint(io.irontest.models.endpoint.Endpoint) Map(java.util.Map)

Example 4 with ResultSetMapper

use of org.skife.jdbi.v2.tweak.ResultSetMapper in project druid by druid-io.

the class JDBCExtractionNamespaceCacheFactory method populateCache.

@Override
@Nullable
public CacheScheduler.VersionedCache populateCache(final JDBCExtractionNamespace namespace, final CacheScheduler.EntryImpl<JDBCExtractionNamespace> entryId, final String lastVersion, final CacheScheduler scheduler) {
    final long lastCheck = lastVersion == null ? JodaUtils.MIN_INSTANT : Long.parseLong(lastVersion);
    final Long lastDBUpdate = lastUpdates(entryId, namespace);
    if (lastDBUpdate != null && lastDBUpdate <= lastCheck) {
        return null;
    }
    final long dbQueryStart = System.currentTimeMillis();
    final DBI dbi = ensureDBI(entryId, namespace);
    final String table = namespace.getTable();
    final String valueColumn = namespace.getValueColumn();
    final String keyColumn = namespace.getKeyColumn();
    LOG.debug("Updating %s", entryId);
    final List<Pair<String, String>> pairs = dbi.withHandle(new HandleCallback<List<Pair<String, String>>>() {

        @Override
        public List<Pair<String, String>> withHandle(Handle handle) throws Exception {
            final String query;
            query = String.format("SELECT %s, %s FROM %s", keyColumn, valueColumn, table);
            return handle.createQuery(query).map(new ResultSetMapper<Pair<String, String>>() {

                @Override
                public Pair<String, String> map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException {
                    return new Pair<>(r.getString(keyColumn), r.getString(valueColumn));
                }
            }).list();
        }
    });
    final String newVersion;
    if (lastDBUpdate != null) {
        newVersion = lastDBUpdate.toString();
    } else {
        newVersion = String.format("%d", dbQueryStart);
    }
    final CacheScheduler.VersionedCache versionedCache = scheduler.createVersionedCache(entryId, newVersion);
    try {
        final Map<String, String> cache = versionedCache.getCache();
        for (Pair<String, String> pair : pairs) {
            cache.put(pair.lhs, pair.rhs);
        }
        LOG.info("Finished loading %d values for %s", cache.size(), entryId);
        return versionedCache;
    } catch (Throwable t) {
        try {
            versionedCache.close();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        throw t;
    }
}
Also used : SQLException(java.sql.SQLException) DBI(org.skife.jdbi.v2.DBI) SQLException(java.sql.SQLException) Handle(org.skife.jdbi.v2.Handle) StatementContext(org.skife.jdbi.v2.StatementContext) ResultSet(java.sql.ResultSet) List(java.util.List) CacheScheduler(io.druid.server.lookup.namespace.cache.CacheScheduler) Pair(io.druid.java.util.common.Pair) Nullable(javax.annotation.Nullable)

Example 5 with ResultSetMapper

use of org.skife.jdbi.v2.tweak.ResultSetMapper in project SimpleFlatMapper by arnaudroger.

the class SfmResultSetMapperFactory method mapperFor.

@SuppressWarnings("unchecked")
@Override
public ResultSetMapper mapperFor(Class aClass, StatementContext statementContext) {
    ResultSetMapper mapper = cache.get(aClass);
    if (mapper == null) {
        Mapper<ResultSet, ?> resultSetMapper = mapperFactory.newInstance(aClass);
        mapper = toResultSetMapper(resultSetMapper);
        ResultSetMapper<?> cachedMapper = cache.putIfAbsent(aClass, mapper);
        if (cachedMapper != null) {
            mapper = cachedMapper;
        }
    }
    return mapper;
}
Also used : ResultSetMapper(org.skife.jdbi.v2.tweak.ResultSetMapper) ResultSet(java.sql.ResultSet)

Aggregations

ResultSet (java.sql.ResultSet)4 ResultSetMapper (org.skife.jdbi.v2.tweak.ResultSetMapper)4 SQLException (java.sql.SQLException)3 StatementContext (org.skife.jdbi.v2.StatementContext)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Handle (org.skife.jdbi.v2.Handle)2 ResultSetMapperFactory (org.skife.jdbi.v2.ResultSetMapperFactory)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RosettaMapper (com.hubspot.rosetta.RosettaMapper)1 DruidDataSource (io.druid.client.DruidDataSource)1 Pair (io.druid.java.util.common.Pair)1 CacheScheduler (io.druid.server.lookup.namespace.cache.CacheScheduler)1 DataSegment (io.druid.timeline.DataSegment)1 Endpoint (io.irontest.models.endpoint.Endpoint)1 IOException (java.io.IOException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSetMetaData (java.sql.ResultSetMetaData)1