use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.
the class TableDaoImpl method getByUris.
@Override
public Map<String, List<QualifiedName>> getByUris(final String sourceName, final List<String> uris, final boolean prefixSearch) {
final StringBuilder builder = new StringBuilder(SQL_GET_TABLE_NAMES_BY_URIS);
if (prefixSearch) {
builder.append(" and ( 1=0");
uris.forEach(uri -> builder.append(" or uri like '").append(uri).append("%'"));
builder.append(")");
} else {
builder.append(" and uri in (:uris)");
}
final Query query = em.get().createNativeQuery(builder.toString());
query.setParameter("sourceName", sourceName);
if (!prefixSearch) {
query.setParameter("uris", uris);
}
final List<Object[]> result = query.getResultList();
return result.stream().collect(Collectors.groupingBy(o -> (String) o[2], Collectors.mapping(o -> QualifiedName.ofTable(sourceName, (String) o[0], (String) o[1]), Collectors.toList())));
}
use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.
the class MetacatV1Resource method renameTable.
@Override
public void renameTable(final String catalogName, final String databaseName, final String tableName, final String newTableName) {
final QualifiedName oldName = requestWrapper.qualifyName(() -> QualifiedName.ofTable(catalogName, databaseName, tableName));
final QualifiedName newName = requestWrapper.qualifyName(() -> QualifiedName.ofTable(catalogName, databaseName, newTableName));
requestWrapper.processRequest(oldName, "renameTable", () -> {
tableService.rename(oldName, newName, false);
return null;
});
}
use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.
the class MetacatV1Resource method updateTable.
@Override
public TableDto updateTable(final String catalogName, final String databaseName, final String tableName, final TableDto table) {
final QualifiedName name = requestWrapper.qualifyName(() -> QualifiedName.ofTable(catalogName, databaseName, tableName));
return requestWrapper.processRequest(name, "updateTable", () -> {
Preconditions.checkArgument(table != null, "Table cannot be null");
Preconditions.checkArgument(tableName != null && !tableName.isEmpty(), "table name is required");
Preconditions.checkArgument(table.getName() != null && tableName.equalsIgnoreCase(table.getName().getTableName()), "Table name does not match the name in the table");
return tableService.updateAndReturn(name, table);
});
}
use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.
the class MysqlUserMetadataService method searchByOwners.
@Override
public List<QualifiedName> searchByOwners(final Set<String> owners) {
final List<QualifiedName> result = Lists.newArrayList();
final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATA_NAMES);
final List<Object> paramList = Lists.newArrayList();
query.append(" where 1=0");
owners.forEach(s -> {
query.append(" or data like ?");
paramList.add("%\"userId\":\"" + s.trim() + "\"%");
});
final Object[] params = new Object[paramList.size()];
final Connection connection = DBUtil.getReadConnection(poolingDataSource);
try {
// Handler for reading the result set
final ResultSetHandler<Void> handler = rs -> {
while (rs.next()) {
final String definitionName = rs.getString("name");
result.add(QualifiedName.fromString(definitionName, false));
}
return null;
};
new QueryRunner().query(connection, query.toString(), handler, paramList.toArray(params));
} catch (SQLException e) {
log.error("Sql exception", e);
throw new UserMetadataServiceException("Failed to get definition data", e);
} finally {
DBUtil.closeReadConnection(connection);
}
return result;
}
use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.
the class MysqlUserMetadataService method saveMetadatas.
@Override
public void saveMetadatas(final String user, final List<? extends HasMetadata> metadatas, final boolean merge) {
try {
final Connection conn = poolingDataSource.getConnection();
try {
@SuppressWarnings("unchecked") final List<List<HasMetadata>> subLists = Lists.partition((List<HasMetadata>) metadatas, config.getUserMetadataMaxInClauseItems());
for (List<HasMetadata> hasMetadatas : subLists) {
final List<String> uris = Lists.newArrayList();
final List<QualifiedName> names = Lists.newArrayList();
// Get the names and uris
final List<HasDefinitionMetadata> definitionMetadatas = Lists.newArrayList();
final List<HasDataMetadata> dataMetadatas = Lists.newArrayList();
hasMetadatas.stream().forEach(hasMetadata -> {
if (hasMetadata instanceof HasDefinitionMetadata) {
final HasDefinitionMetadata oDef = (HasDefinitionMetadata) hasMetadata;
names.add(oDef.getDefinitionName());
if (oDef.getDefinitionMetadata() != null) {
definitionMetadatas.add(oDef);
}
}
if (hasMetadata instanceof HasDataMetadata) {
final HasDataMetadata oData = (HasDataMetadata) hasMetadata;
if (oData.isDataExternal() && oData.getDataMetadata() != null && oData.getDataMetadata().size() > 0) {
uris.add(oData.getDataUri());
dataMetadatas.add(oData);
}
}
});
if (!definitionMetadatas.isEmpty() || !dataMetadatas.isEmpty()) {
// Get the existing metadata based on the names and uris
final Map<String, ObjectNode> definitionMap = getDefinitionMetadataMap(names);
final Map<String, ObjectNode> dataMap = getDataMetadataMap(uris);
// Curate the list of existing and new metadatas
final List<Object[]> insertDefinitionMetadatas = Lists.newArrayList();
final List<Object[]> updateDefinitionMetadatas = Lists.newArrayList();
final List<Object[]> insertDataMetadatas = Lists.newArrayList();
final List<Object[]> updateDataMetadatas = Lists.newArrayList();
definitionMetadatas.stream().forEach(oDef -> {
final QualifiedName qualifiedName = oDef.getDefinitionName();
if (qualifiedName != null && oDef.getDefinitionMetadata() != null && oDef.getDefinitionMetadata().size() != 0) {
final String name = qualifiedName.toString();
final ObjectNode oNode = definitionMap.get(name);
if (oNode == null) {
insertDefinitionMetadatas.add(new Object[] { metacatJson.toJsonString(oDef.getDefinitionMetadata()), user, user, name });
} else {
metacatJson.mergeIntoPrimary(oNode, oDef.getDefinitionMetadata());
updateDefinitionMetadatas.add(new Object[] { metacatJson.toJsonString(oNode), user, name });
}
}
});
dataMetadatas.stream().forEach(oData -> {
final String uri = oData.getDataUri();
final ObjectNode oNode = dataMap.get(uri);
if (oData.getDataMetadata() != null && oData.getDataMetadata().size() != 0) {
if (oNode == null) {
insertDataMetadatas.add(new Object[] { metacatJson.toJsonString(oData.getDataMetadata()), user, user, uri });
} else {
metacatJson.mergeIntoPrimary(oNode, oData.getDataMetadata());
updateDataMetadatas.add(new Object[] { metacatJson.toJsonString(oNode), user, uri });
}
}
});
//Now run the queries
final QueryRunner runner = new QueryRunner();
if (!insertDefinitionMetadatas.isEmpty()) {
runner.batch(conn, SQL.INSERT_DEFINITION_METADATA, insertDefinitionMetadatas.toArray(new Object[insertDefinitionMetadatas.size()][4]));
}
if (!updateDefinitionMetadatas.isEmpty()) {
runner.batch(conn, SQL.UPDATE_DEFINITION_METADATA, updateDefinitionMetadatas.toArray(new Object[updateDefinitionMetadatas.size()][3]));
}
if (!insertDataMetadatas.isEmpty()) {
runner.batch(conn, SQL.INSERT_DATA_METADATA, insertDataMetadatas.toArray(new Object[insertDataMetadatas.size()][4]));
}
if (!updateDataMetadatas.isEmpty()) {
runner.batch(conn, SQL.UPDATE_DATA_METADATA, updateDataMetadatas.toArray(new Object[updateDataMetadatas.size()][3]));
}
}
}
conn.commit();
} catch (SQLException e) {
conn.rollback();
throw e;
} finally {
conn.close();
}
} catch (SQLException e) {
log.error("Sql exception", e);
throw new UserMetadataServiceException("Failed to save metadata", e);
}
}
Aggregations