use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.
the class HiveConnectorFastPartitionService method getPartitionCount.
/**
* Number of partitions for the given table.
*
* @param tableName tableName
* @return Number of partitions
*/
@Override
public int getPartitionCount(@Nonnull @NonNull final ConnectorContext requestContext, @Nonnull @NonNull final QualifiedName tableName) {
final long start = registry.clock().monotonicTime();
final Map<String, String> tags = new HashMap<String, String>();
tags.put("request", HiveMetrics.getPartitionCount.name());
final Integer result;
final DataSource dataSource = DataSourceManager.get().get(catalogName);
try (Connection conn = dataSource.getConnection()) {
// Handler for reading the result set
final ResultSetHandler<Integer> handler = rs -> {
int count = 0;
while (rs.next()) {
count = rs.getInt("count");
}
return count;
};
result = new QueryRunner().query(conn, SQL_GET_PARTITION_COUNT, handler, tableName.getDatabaseName(), tableName.getTableName());
} catch (SQLException e) {
throw new ConnectorException("getPartitionCount", e);
} finally {
final long duration = registry.clock().monotonicTime() - start;
log.debug("### Time taken to complete getPartitionCount is {} ms", duration);
this.registry.timer(requestTimerId.withTags(tags)).record(duration, TimeUnit.MILLISECONDS);
}
return result;
}
use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.
the class HiveConnectorFastTableService method exists.
/**
* {@inheritDoc}.
*/
@Override
public boolean exists(@Nonnull final ConnectorContext requestContext, @Nonnull final QualifiedName name) {
final long start = registry.clock().monotonicTime();
final Map<String, String> tags = new HashMap<String, String>();
tags.put("request", HiveMetrics.exists.name());
boolean result = false;
// Get data source
final DataSource dataSource = DataSourceManager.get().get(catalogName);
try (Connection conn = dataSource.getConnection()) {
final Object qResult = new QueryRunner().query(conn, SQL_EXIST_TABLE_BY_NAME, new ScalarHandler(1), name.getDatabaseName(), name.getTableName());
if (qResult != null) {
result = true;
}
} catch (SQLException e) {
throw Throwables.propagate(e);
} finally {
final long duration = registry.clock().monotonicTime() - start;
log.debug("### Time taken to complete exists is {} ms", duration);
this.registry.timer(requestTimerId.withTags(tags)).record(duration, TimeUnit.MILLISECONDS);
}
return result;
}
use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.
the class MySqlTagService method insertTagItemTags.
private void insertTagItemTags(final Long id, final Set<String> tags, final Connection conn) throws SQLException {
final Object[][] params = new Object[tags.size()][];
final Iterator<String> iter = tags.iterator();
int index = 0;
while (iter.hasNext()) {
params[index++] = ImmutableList.of(id, iter.next()).toArray();
}
new QueryRunner().batch(conn, SQL_INSERT_TAG_ITEM_TAGS, params);
}
use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.
the class MySqlTagService method delete.
@Override
public Void delete(final QualifiedName name, final boolean updateUserMetadata) {
try {
final Connection conn = getDataSource().getConnection();
try {
new QueryRunner().update(conn, SQL_DELETE_TAG_ITEM_TAGS_BY_NAME, name.toString());
new QueryRunner().update(conn, SQL_DELETE_TAG_ITEM, name.toString());
if (updateUserMetadata) {
// Set the tags in user metadata
final Map<String, Set<String>> data = Maps.newHashMap();
data.put(NAME_TAGS, Sets.newHashSet());
userMetadataService.saveDefinitionMetadata(name, "admin", Optional.of(metacatJson.toJsonObject(data)), true);
}
conn.commit();
} catch (SQLException e) {
conn.rollback();
throw e;
} finally {
conn.close();
}
} catch (SQLException e) {
final String message = String.format("Failed to delete all tags for name %s", name);
log.error(message, e);
throw new UserMetadataServiceException(message, e);
}
return null;
}
use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.
the class MySqlTagService method list.
/**
* Returns the list of <code>QualifiedName</code> of items that are tagged by the
* given <code>includeTags</code> and do not contain the given <code>excludeTags</code>.
*
* @param includeTags include items that contain tags
* @param excludeTags include items that do not contain tags
* @param sourceName catalog/source name
* @param databaseName database name
* @param tableName table name
* @return list of qualified names of the items
*/
@Override
public List<QualifiedName> list(final Set<String> includeTags, final Set<String> excludeTags, final String sourceName, final String databaseName, final String tableName) {
Set<String> includedNames = Sets.newHashSet();
final Set<String> excludedNames = Sets.newHashSet();
final Connection connection = DBUtil.getReadConnection(getDataSource());
try {
final QueryRunner runner = new QueryRunner();
final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
//Includes
String query = String.format(QUERY_SEARCH, "in ('" + Joiner.on("','").skipNulls().join(includeTags) + "')");
final Object[] params = { includeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
includedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), params));
if (!excludeTags.isEmpty()) {
//Excludes
query = String.format(QUERY_SEARCH, "in ('" + Joiner.on("','").skipNulls().join(excludeTags) + "')");
final Object[] eParams = { excludeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
excludedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), eParams));
}
} catch (SQLException e) {
final String message = String.format("Failed getting the list of qualified names for tags %s", includeTags);
log.error(message, e);
throw new UserMetadataServiceException(message, e);
} finally {
DBUtil.closeReadConnection(connection);
}
if (!excludeTags.isEmpty()) {
includedNames = Sets.difference(includedNames, excludedNames);
}
return includedNames.stream().map(s -> QualifiedName.fromString(s, false)).collect(Collectors.toList());
}
Aggregations