Search in sources :

Example 46 with IDesignerCoreService

use of org.talend.designer.core.IDesignerCoreService in project tbd-studio-se by Talend.

the class MapRDBMetadataProvider method returnMetadataColumnsFromTable.

@Override
public List<TdColumn> returnMetadataColumnsFromTable(String tableName, IMetadataConnection metadataConnection) {
    List<TdColumn> toReturn = new ArrayList<TdColumn>();
    ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        Object hAdmin = getAdmin(metadataConnection);
        // $NON-NLS-1$
        Object config = ReflectionUtils.invokeMethod(hAdmin, "getConfiguration", new Object[0]);
        // $NON-NLS-1$
        Object scan = Class.forName("org.apache.hadoop.hbase.client.Scan", true, classLoader).newInstance();
        Object table = ReflectionUtils.newInstance("org.apache.hadoop.hbase.client.HTable", classLoader, new Object[] { // $NON-NLS-1$
        config, tableName });
        String schema = metadataConnection.getSchema();
        if (StringUtils.isNotEmpty(schema)) {
            // $NON-NLS-1$
            ReflectionUtils.invokeMethod(scan, "addFamily", new Object[] { schema.getBytes() });
        }
        // Limit
        int count = 0;
        int limit = -1;
        if (GlobalServiceRegister.getDefault().isServiceRegistered(IDesignerCoreService.class)) {
            IDesignerCoreService designerService = (IDesignerCoreService) GlobalServiceRegister.getDefault().getService(IDesignerCoreService.class);
            limit = designerService.getHBaseOrMaprDBScanLimit();
        }
        List<String> columnExsit = new ArrayList<String>();
        // $NON-NLS-1$
        Object resultSetscanner = ReflectionUtils.invokeMethod(table, "getScanner", new Object[] { scan });
        // $NON-NLS-1$
        Object result = ReflectionUtils.invokeMethod(resultSetscanner, "next", new Object[0]);
        while (result != null) {
            // $NON-NLS-1$
            List<Object> list = (List<Object>) ReflectionUtils.invokeMethod(result, "list", new Object[0]);
            if (list != null) {
                /**
                 * in hbase data will stored as keyValue,but the kv.getKeyString() which equal
                 * "kv.getFamily()+kv.getQualifier()+kv.getRow()" can specific a metadata,so need to trim the
                 * duplicated kv.getQualifier()+kv.getRow()
                 */
                for (Object kv : list) {
                    // $NON-NLS-1$
                    byte[] qualifier = (byte[]) ReflectionUtils.invokeMethod(kv, "getQualifier", new Object[0]);
                    String columnName = (String) // $NON-NLS-1$
                    ReflectionUtils.invokeStaticMethod(// $NON-NLS-1$
                    "org.apache.hadoop.hbase.util.Bytes", classLoader, "toStringBinary", // $NON-NLS-1$
                    new Object[] { qualifier });
                    if (StringUtils.isEmpty(columnName)) {
                        continue;
                    }
                    // $NON-NLS-1$
                    byte[] family = (byte[]) ReflectionUtils.invokeMethod(kv, "getFamily", new Object[0]);
                    String familyName = (String) // $NON-NLS-1$
                    ReflectionUtils.invokeStaticMethod(// $NON-NLS-1$
                    "org.apache.hadoop.hbase.util.Bytes", classLoader, "toStringBinary", // $NON-NLS-1$
                    new Object[] { family });
                    if (columnName != null) {
                        // $NON-NLS-1$
                        String exsistColumn = columnName + "_" + familyName;
                        if (!columnExsit.contains(exsistColumn)) {
                            TdColumn column = RelationalFactory.eINSTANCE.createTdColumn();
                            column.setLabel(columnName);
                            column.setName(columnName);
                            // hbase no type ,just byte[],need to cast the type ourself
                            // $NON-NLS-1$
                            column.setTalendType("id_String");
                            TaggedValue tv = TaggedValueHelper.createTaggedValue(COLUMN_FAMILY, familyName);
                            column.getTaggedValue().add(tv);
                            toReturn.add(column);
                            columnExsit.add(exsistColumn);
                        }
                    }
                }
            }
            count++;
            if (count == limit) {
                break;
            }
            // $NON-NLS-1$
            result = ReflectionUtils.invokeMethod(resultSetscanner, "next", new Object[0]);
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
        // $NON-NLS-1$
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error encountered when retrieving schema.", e);
        // $NON-NLS-1$
        ErrorDialog errorDialog = new ErrorDialog(null, "Error", null, status, IStatus.ERROR);
        errorDialog.open();
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
    }
    return toReturn;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ConnectionStatus(org.talend.core.repository.model.connection.ConnectionStatus) TaggedValue(orgomg.cwm.objectmodel.core.TaggedValue) ArrayList(java.util.ArrayList) ErrorDialog(org.eclipse.jface.dialogs.ErrorDialog) TdColumn(org.talend.cwm.relational.TdColumn) List(java.util.List) ArrayList(java.util.ArrayList) IDesignerCoreService(org.talend.designer.core.IDesignerCoreService)

Example 47 with IDesignerCoreService

use of org.talend.designer.core.IDesignerCoreService in project tbd-studio-se by Talend.

the class MapRDBMetadataProvider method executeInRunnable.

/**
 * run method in Runnable will execute this *
 */
@Override
public void executeInRunnable(IMetadataConnection metadataConnection, Object currentNode, DatabaseConnection dbconn) {
    Object hAdmin = getAdmin(metadataConnection);
    Object config = null;
    MetadataTable metadataTable = null;
    TableNode columnNode = null;
    ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        if (hAdmin != null) {
            // $NON-NLS-1$
            config = ReflectionUtils.invokeMethod(hAdmin, "getConfiguration", new Object[0]);
        }
        if (currentNode instanceof TableNode) {
            columnNode = (TableNode) currentNode;
        }
        if (columnNode == null) {
            return;
        }
        TableNode columnFamilyNode = columnNode.getParent();
        TableNode tableNode = null;
        String tableName = null;
        String columnFamilyName = null;
        if (columnFamilyNode != null) {
            columnFamilyName = columnFamilyNode.getValue();
            tableNode = columnFamilyNode.getParent();
            if (tableNode != null) {
                tableName = tableNode.getValue();
                metadataTable = (MetadataTable) tableNode.getTable();
            }
        }
        if (columnNode != null && columnNode.getType() == TableNode.COLUMN) {
            // $NON-NLS-1$
            Object scan = Class.forName("org.apache.hadoop.hbase.client.Scan", true, classLoader).newInstance();
            Object table = ReflectionUtils.newInstance("org.apache.hadoop.hbase.client.HTable", classLoader, new Object[] { // $NON-NLS-1$
            config, tableName });
            // $NON-NLS-1$
            ReflectionUtils.invokeMethod(scan, "addFamily", new Object[] { columnFamilyName.getBytes() });
            // Limit
            int count = 0;
            int limit = -1;
            if (GlobalServiceRegister.getDefault().isServiceRegistered(IDesignerCoreService.class)) {
                IDesignerCoreService designerService = (IDesignerCoreService) GlobalServiceRegister.getDefault().getService(IDesignerCoreService.class);
                limit = designerService.getHBaseOrMaprDBScanLimit();
            }
            List<String> columnNameExsit = new ArrayList<String>();
            // $NON-NLS-1$
            Object resultSetscanner = ReflectionUtils.invokeMethod(table, "getScanner", new Object[] { scan });
            // $NON-NLS-1$
            Object result = ReflectionUtils.invokeMethod(resultSetscanner, "next", new Object[0]);
            while (result != null) {
                // $NON-NLS-1$
                List<Object> list = (List<Object>) ReflectionUtils.invokeMethod(result, "list", new Object[0]);
                if (list != null) {
                    for (Object kv : list) {
                        // $NON-NLS-1$
                        byte[] family = (byte[]) ReflectionUtils.invokeMethod(kv, "getFamily", new Object[0]);
                        String familyName = (String) // $NON-NLS-1$
                        ReflectionUtils.invokeStaticMethod(// $NON-NLS-1$
                        "org.apache.hadoop.hbase.util.Bytes", classLoader, "toStringBinary", // $NON-NLS-1$
                        new Object[] { family });
                        if (familyName.equals(columnFamilyNode.getValue())) {
                            // $NON-NLS-1$
                            byte[] qualifier = (byte[]) ReflectionUtils.invokeMethod(kv, "getQualifier", new Object[0]);
                            String columnName = (String) ReflectionUtils.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
                            "org.apache.hadoop.hbase.util.Bytes", // $NON-NLS-1$ //$NON-NLS-2$
                            classLoader, // $NON-NLS-1$ //$NON-NLS-2$
                            "toStringBinary", new Object[] { qualifier });
                            if (columnName != null && columnName.equals(columnNode.getValue()) && !columnNameExsit.contains(columnName)) {
                                String originalColumnName = columnName;
                                columnName = getUniqueColumnName(getColumnLabels(metadataTable), columnName);
                                TdColumn column = RelationalFactory.eINSTANCE.createTdColumn();
                                column.setLabel(columnName);
                                column.setName(originalColumnName);
                                column.setOriginalField(originalColumnName);
                                // $NON-NLS-1$
                                column.setTalendType("id_String");
                                TaggedValue tv = TaggedValueHelper.createTaggedValue(COLUMN_FAMILY, columnFamilyName);
                                column.getTaggedValue().add(tv);
                                List<MetadataColumn> columns = metadataTable.getColumns();
                                columns.add(column);
                                columnNameExsit.add(columnName);
                                List<Catalog> catalogs = ConnectionHelper.getCatalogs(dbconn);
                                Catalog catalogToWrite = null;
                                for (Catalog c : catalogs) {
                                    if (c.getName() != null && c.getName().equals(getDefaultCatalogName())) {
                                        catalogToWrite = c;
                                        break;
                                    }
                                }
                                if (catalogToWrite != null) {
                                    boolean findTable = false;
                                    List exsitTables = CatalogHelper.getTables(catalogToWrite);
                                    for (Object obj : exsitTables) {
                                        if (obj instanceof TdTable) {
                                            TdTable tb = (TdTable) obj;
                                            if (tableName != null && tb.getLabel().equals(tableName)) {
                                                List<MetadataColumn> columnsExsit = tb.getColumns();
                                                /*
                                                     * can add into column list directly because it will delete the same
                                                     * name column when deselect
                                                     */
                                                columnsExsit.add(column);
                                                findTable = true;
                                            }
                                        }
                                    }
                                    if (!findTable) {
                                        if (metadataTable.getId() == null) {
                                            IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
                                            metadataTable.setId(factory.getNextId());
                                        }
                                        PackageHelper.addMetadataTable(metadataTable, catalogToWrite);
                                    }
                                }
                            }
                        }
                    }
                }
                count++;
                if (count == limit) {
                    break;
                }
                // $NON-NLS-1$
                result = ReflectionUtils.invokeMethod(resultSetscanner, "next", new Object[0]);
            }
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
    }
}
Also used : TdTable(org.talend.cwm.relational.TdTable) TaggedValue(orgomg.cwm.objectmodel.core.TaggedValue) ArrayList(java.util.ArrayList) Catalog(orgomg.cwm.resource.relational.Catalog) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) TdColumn(org.talend.cwm.relational.TdColumn) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) TableNode(org.talend.core.model.metadata.builder.database.TableNode) List(java.util.List) ArrayList(java.util.ArrayList) IDesignerCoreService(org.talend.designer.core.IDesignerCoreService) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory)

Example 48 with IDesignerCoreService

use of org.talend.designer.core.IDesignerCoreService in project tbd-studio-se by Talend.

the class HBaseMetadataProvider method testConnection.

@Override
public ConnectionStatus testConnection(IMetadataConnection metadataConnection) {
    classLoader = HBaseClassLoaderFactory.getClassLoader(metadataConnection);
    ConnectionStatus connectionStatus = new ConnectionStatus();
    connectionStatus.setResult(false);
    ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        Object config = getConfiguration(metadataConnection);
        Callable<Object> callable = checkHBaseAvailable(config);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Object> future = executor.submit(callable);
        try {
            int timeout = 15;
            if (GlobalServiceRegister.getDefault().isServiceRegistered(IDesignerCoreService.class)) {
                IDesignerCoreService designerService = GlobalServiceRegister.getDefault().getService(IDesignerCoreService.class);
                timeout = designerService.getDBConnectionTimeout();
            }
            future.get(timeout, TimeUnit.SECONDS);
            connectionStatus.setResult(true);
        } catch (Exception e) {
            future.cancel(true);
            connectionStatus.setResult(false);
            connectionStatus.setMessageException(ExceptionUtils.getFullStackTrace(e));
            ExceptionHandler.process(e);
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
        connectionStatus.setResult(false);
        connectionStatus.setMessageException(ExceptionUtils.getFullStackTrace(e));
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
    }
    return connectionStatus;
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) IDesignerCoreService(org.talend.designer.core.IDesignerCoreService) ConnectionStatus(org.talend.core.repository.model.connection.ConnectionStatus)

Example 49 with IDesignerCoreService

use of org.talend.designer.core.IDesignerCoreService in project tbd-studio-se by Talend.

the class HBaseMetadataProvider method returnMetadataColumnsFromTable.

@Override
public List<TdColumn> returnMetadataColumnsFromTable(String tableName, IMetadataConnection metadataConnection) {
    List<TdColumn> toReturn = new ArrayList<TdColumn>();
    ClassLoader oldClassLoaderLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        Object hConnection = getRealConnection(metadataConnection);
        Object config = getConfiguration(hConnection);
        Object scan = createScan();
        Object table = getTable(hConnection, tableName, classLoader);
        String schema = metadataConnection.getSchema();
        if (StringUtils.isNotEmpty(schema)) {
            addFamily(scan, schema.getBytes());
        }
        // Limit
        int count = 0;
        int limit = -1;
        if (GlobalServiceRegister.getDefault().isServiceRegistered(IDesignerCoreService.class)) {
            IDesignerCoreService designerService = GlobalServiceRegister.getDefault().getService(IDesignerCoreService.class);
            limit = designerService.getHBaseOrMaprDBScanLimit();
        }
        List<String> existingColumns = new ArrayList<String>();
        Object scanner = getResultScanner(table, scan);
        Object result = nextItem(scanner);
        while (result != null) {
            List<Object> list = listCells(result);
            if (list != null) {
                /**
                 * in hbase data will stored as keyValue,but the kv.getKeyString() which equal
                 * "kv.getFamily()+kv.getQualifier()+kv.getRow()" can specific a metadata,so need to trim the
                 * duplicated kv.getQualifier()+kv.getRow()
                 */
                for (Object cell : list) {
                    String columnName = getQualifier(cell);
                    if (StringUtils.isEmpty(columnName)) {
                        continue;
                    }
                    String familyName = getFamily(cell);
                    if (columnName != null) {
                        // $NON-NLS-1$
                        String exsistColumn = columnName + "_" + familyName;
                        if (!existingColumns.contains(exsistColumn)) {
                            TdColumn column = createDatabaseColumn(columnName, familyName);
                            toReturn.add(column);
                            existingColumns.add(exsistColumn);
                        }
                    }
                }
            }
            count++;
            if (count == limit) {
                break;
            }
            result = nextItem(scanner);
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
        // $NON-NLS-1$
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error encountered when retrieving schema.", e);
        // $NON-NLS-1$
        ErrorDialog errorDialog = new ErrorDialog(null, "Error", null, status, IStatus.ERROR);
        errorDialog.open();
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoaderLoader);
    }
    return toReturn;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ConnectionStatus(org.talend.core.repository.model.connection.ConnectionStatus) ArrayList(java.util.ArrayList) ErrorDialog(org.eclipse.jface.dialogs.ErrorDialog) TdColumn(org.talend.cwm.relational.TdColumn) IDesignerCoreService(org.talend.designer.core.IDesignerCoreService)

Example 50 with IDesignerCoreService

use of org.talend.designer.core.IDesignerCoreService in project tbd-studio-se by Talend.

the class StandardHCInfoForm method filterTypes.

private Map<EHadoopServiceType, HadoopServiceProperties> filterTypes(Map<EHadoopServiceType, HadoopServiceProperties> serviceTypeToProperties) {
    Map<EHadoopServiceType, HadoopServiceProperties> filteredTypes = serviceTypeToProperties;
    IDesignerCoreService designerCoreService = CoreRuntimePlugin.getInstance().getDesignerCoreService();
    // $NON-NLS-1$
    INode node = designerCoreService.getRefrenceNode("tMRConfiguration", ComponentCategory.CATEGORY_4_MAPREDUCE.getName());
    if (node == null) {
        filteredTypes.remove(EHadoopServiceType.JOBTRACKER);
        filteredTypes.remove(EHadoopServiceType.RESOURCE_MANAGER);
    }
    return filteredTypes;
}
Also used : INode(org.talend.core.model.process.INode) EHadoopServiceType(org.talend.designer.hdfsbrowse.hadoop.service.EHadoopServiceType) HadoopServiceProperties(org.talend.designer.hdfsbrowse.hadoop.service.HadoopServiceProperties) IDesignerCoreService(org.talend.designer.core.IDesignerCoreService)

Aggregations

IDesignerCoreService (org.talend.designer.core.IDesignerCoreService)52 IProcess (org.talend.core.model.process.IProcess)27 ProcessItem (org.talend.core.model.properties.ProcessItem)22 ArrayList (java.util.ArrayList)16 INode (org.talend.core.model.process.INode)13 Item (org.talend.core.model.properties.Item)13 PersistenceException (org.talend.commons.exception.PersistenceException)11 List (java.util.List)10 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)9 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)9 IOException (java.io.IOException)8 IProcess2 (org.talend.core.model.process.IProcess2)8 HashSet (java.util.HashSet)6 IFile (org.eclipse.core.resources.IFile)6 IElementParameter (org.talend.core.model.process.IElementParameter)6 Process (org.talend.designer.core.ui.editor.process.Process)6 IProcessor (org.talend.designer.runprocess.IProcessor)6 IRunProcessService (org.talend.designer.runprocess.IRunProcessService)6 HashMap (java.util.HashMap)5 IEditorReference (org.eclipse.ui.IEditorReference)5