Search in sources :

Example 76 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.

the class Neo4jSchemaForm method generateSchema.

private void generateSchema() throws NoSQLExtractSchemaException {
    if (metadataTable == null) {
        return;
    }
    String cypher = metadataTable.getAdditionalProperties().get(INeo4jConstants.CYPHER);
    if (cypher == null) {
        return;
    }
    IMetadataProvider metadataProvider = NoSQLRepositoryFactory.getInstance().getMetadataProvider(getConnection().getDbType());
    if (metadataProvider == null) {
        return;
    }
    List<MetadataColumn> columns = metadataProvider.extractColumns(getConnection(), cypher);
    metadataTable.getColumns().clear();
    metadataTable.getColumns().addAll(columns);
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) IMetadataProvider(org.talend.repository.nosql.metadata.IMetadataProvider)

Example 77 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.

the class MongoDBMetadataProvider method extractColumns.

@Override
public List<MetadataColumn> extractColumns(NoSQLConnection connection, INoSQLSchemaNode node) throws NoSQLExtractSchemaException {
    List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
    if (connection == null || node == null) {
        return metadataColumns;
    }
    try {
        if (IMongoConstants.COLLECTION.equals(node.getNodeType())) {
            String dbName = null;
            INoSQLSchemaNode parent = node.getParent();
            if (parent != null && IMongoConstants.DATABASE.equals(parent.getNodeType())) {
                dbName = parent.getName();
            } else {
                dbName = ConnectionContextHelper.getParamValueOffContext(connection, connection.getAttributes().get(IMongoDBAttributes.DATABASE));
            }
            if (dbName == null) {
                return metadataColumns;
            }
            String collectionName = node.getName();
            if (MongoDBConnectionUtil.isUpgradeLatestVersion(connection)) {
                metadataColumns.addAll(extractTheColumns4Upgrade(connection, dbName, collectionName));
            } else {
                metadataColumns.addAll(extractTheColumns(connection, dbName, collectionName));
            }
        }
    } catch (Exception e) {
        throw new NoSQLExtractSchemaException(e);
    }
    return metadataColumns;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) INoSQLSchemaNode(org.talend.repository.nosql.model.INoSQLSchemaNode) ArrayList(java.util.ArrayList) NoSQLExtractSchemaException(org.talend.repository.nosql.exceptions.NoSQLExtractSchemaException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLExtractSchemaException(org.talend.repository.nosql.exceptions.NoSQLExtractSchemaException)

Example 78 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.

the class MongoDBMetadataProvider method extractTheColumns4Upgrade.

private List<MetadataColumn> extractTheColumns4Upgrade(NoSQLConnection connection, String dbName, String collectionName) throws NoSQLExtractSchemaException {
    List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
    try {
        Object db = MongoDBConnectionUtil.getDB(connection, dbName);
        if (db == null) {
            return metadataColumns;
        }
        ClassLoader classLoader = NoSQLClassLoaderFactory.getClassLoader(connection);
        // $NON-NLS-1$
        Class<?> dbObjectClasszz = Class.forName("com.mongodb.DBObject", true, classLoader);
        // $NON-NLS-1$
        Class<?> documentClasszz = Class.forName("org.bson.Document", true, classLoader);
        String documentClassName = documentClasszz.getName();
        List<String> existColumnNames = new ArrayList<String>();
        // $NON-NLS-1$
        Object dbCollection = NoSQLReflection.invokeMethod(db, "getCollection", new Object[] { collectionName });
        Set<String> indexColNames = new HashSet<String>();
        // $NON-NLS-1$
        Iterable<Object> indexIter = (Iterable<Object>) NoSQLReflection.invokeMethod(dbCollection, "listIndexes", new Object[] { dbObjectClasszz });
        Iterator<Object> indexIterator = indexIter.iterator();
        while (indexIterator.hasNext()) {
            Object index = indexIterator.next();
            // $NON-NLS-1$//$NON-NLS-2$
            Object keyObj = NoSQLReflection.invokeMethod(index, "get", new Object[] { "key" });
            if (keyObj != null) {
                // $NON-NLS-1$
                Set<String> indexKeys = (Set<String>) NoSQLReflection.invokeMethod(keyObj, "keySet", new Object[0]);
                indexColNames.addAll(indexKeys);
            }
        }
        int rowNum = 0;
        // $NON-NLS-1$
        Iterable<Object> dbCursorIter = (Iterable<Object>) NoSQLReflection.invokeMethod(dbCollection, "find");
        Iterator<Object> dbCursorIterator = dbCursorIter.iterator();
        while (dbCursorIterator.hasNext()) {
            if (rowNum > COUNT_ROWS) {
                break;
            }
            Object dbObject = dbCursorIterator.next();
            // $NON-NLS-1$
            Set<String> columnNames = (Set<String>) NoSQLReflection.invokeMethod(dbObject, "keySet");
            for (String colName : columnNames) {
                colName = MetadataToolHelper.validateValue(colName);
                if (existColumnNames.contains(colName)) {
                    continue;
                }
                MetadataColumn column = ConnectionFactory.eINSTANCE.createMetadataColumn();
                column.setName(colName);
                column.setLabel(colName);
                // $NON-NLS-1$
                Object value = NoSQLReflection.invokeMethod(dbObject, "get", new Object[] { colName }, Object.class);
                JavaType javaType = null;
                if (value != null) {
                    if (!documentClassName.equals(value.getClass().getName())) {
                        javaType = JavaTypesManager.getJavaTypeFromName(value.getClass().getSimpleName());
                    } else {
                        javaType = JavaTypesManager.OBJECT;
                    }
                }
                if (javaType == null) {
                    javaType = JavaTypesManager.STRING;
                }
                column.setTalendType(javaType.getId());
                if (indexColNames.contains(colName)) {
                    column.setKey(true);
                    column.setNullable(false);
                }
                metadataColumns.add(column);
                existColumnNames.add(colName);
            }
            rowNum++;
        }
    } catch (Exception e) {
        throw new NoSQLExtractSchemaException(e);
    }
    return metadataColumns;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLExtractSchemaException(org.talend.repository.nosql.exceptions.NoSQLExtractSchemaException) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) JavaType(org.talend.core.model.metadata.types.JavaType) NoSQLExtractSchemaException(org.talend.repository.nosql.exceptions.NoSQLExtractSchemaException) HashSet(java.util.HashSet)

Example 79 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.

the class MongoDBMetadataProvider method extractColumns.

@Override
public List<MetadataColumn> extractColumns(NoSQLConnection connection, String tableName) throws NoSQLExtractSchemaException {
    List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
    if (connection == null || tableName == null) {
        return metadataColumns;
    }
    try {
        String dbName = NoSQLSchemaUtil.getSchemaNameByTableLabel(connection, tableName);
        dbName = ConnectionContextHelper.getParamValueOffContext(connection, dbName);
        if (MongoDBConnectionUtil.isUpgradeLatestVersion(connection)) {
            metadataColumns.addAll(extractTheColumns4Upgrade(connection, dbName, tableName));
        } else {
            metadataColumns.addAll(extractTheColumns(connection, dbName, tableName));
        }
    } catch (Exception e) {
        throw new NoSQLExtractSchemaException(e);
    }
    return metadataColumns;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ArrayList(java.util.ArrayList) NoSQLExtractSchemaException(org.talend.repository.nosql.exceptions.NoSQLExtractSchemaException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLExtractSchemaException(org.talend.repository.nosql.exceptions.NoSQLExtractSchemaException)

Example 80 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tbd-studio-se by Talend.

the class Neo4jMetadataProvider method getTheColumns.

private List<MetadataColumn> getTheColumns(NoSQLConnection connection, ClassLoader classLoader, String cypher) throws NoSQLReflectionException, ClassNotFoundException, NoSQLServerException {
    List<MetadataColumn> metadataColumns = new ArrayList<MetadataColumn>();
    String usename = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.USERNAME));
    String password = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.PASSWORD));
    String serverUrl = StringUtils.trimToEmpty(connection.getAttributes().get(INeo4jAttributes.SERVER_URL));
    if (connection.isContextMode()) {
        ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
        if (contextType != null) {
            usename = ContextParameterUtils.getOriginalValue(contextType, usename);
            password = ContextParameterUtils.getOriginalValue(contextType, password);
            serverUrl = ContextParameterUtils.getOriginalValue(contextType, serverUrl);
        }
    } else {
        password = connection.getValue(password, false);
    }
    Object basic = NoSQLReflection.invokeStaticMethod("org.neo4j.driver.v1.AuthTokens", "basic", new Object[] { usename, password }, classLoader, String.class, String.class);
    Object driver = NoSQLReflection.invokeStaticMethod("org.neo4j.driver.v1.GraphDatabase", "driver", new Object[] { serverUrl, basic }, classLoader, String.class, Class.forName("org.neo4j.driver.v1.AuthToken", true, classLoader));
    Object session = NoSQLReflection.invokeMethod(driver, "session");
    Iterator<Map<String, Object>> resultIterator = (Iterator<Map<String, Object>>) NoSQLReflection.invokeMethod(session, "run", new Object[] { cypher }, String.class);
    List<String> columnLabels = new ArrayList<String>();
    int rowNum = 0;
    while (resultIterator.hasNext()) {
        if (rowNum > COUNT_ROWS) {
            break;
        }
        rowNum++;
        Object record = resultIterator.next();
        Map<String, Object> row = (Map<String, Object>) NoSQLReflection.invokeMethod(record, "asMap");
        for (Entry<String, Object> column : row.entrySet()) {
            String key = column.getKey();
            Object value = column.getValue();
            if (StringUtils.isEmpty(key) || value == null) {
                continue;
            }
            addMetadataColumns(classLoader, key, value, metadataColumns, columnLabels);
        }
    }
    return metadataColumns;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) ArrayList(java.util.ArrayList) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) Iterator(java.util.Iterator) Map(java.util.Map)

Aggregations

MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)203 ArrayList (java.util.ArrayList)98 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)51 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)37 TdColumn (org.talend.cwm.relational.TdColumn)28 List (java.util.List)24 HashMap (java.util.HashMap)20 Test (org.junit.Test)15 TaggedValue (orgomg.cwm.objectmodel.core.TaggedValue)14 EList (org.eclipse.emf.common.util.EList)12 Indicator (org.talend.dataquality.indicators.Indicator)12 IRepositoryNode (org.talend.repository.model.IRepositoryNode)12 File (java.io.File)11 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 NoSQLExtractSchemaException (org.talend.repository.nosql.exceptions.NoSQLExtractSchemaException)10 Map (java.util.Map)9 DelimitedFileConnection (org.talend.core.model.metadata.builder.connection.DelimitedFileConnection)9 RepositoryNode (org.talend.repository.model.RepositoryNode)9 Iterator (java.util.Iterator)8 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)8