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();
}
}
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);
}
};
}
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;
}
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;
}
}
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;
}
Aggregations