Search in sources :

Example 11 with TableNode

use of org.talend.core.model.metadata.builder.database.TableNode 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)

Aggregations

TableNode (org.talend.core.model.metadata.builder.database.TableNode)11 TdTable (org.talend.cwm.relational.TdTable)8 ArrayList (java.util.ArrayList)6 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)6 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)6 TaggedValue (orgomg.cwm.objectmodel.core.TaggedValue)5 List (java.util.List)3 TdColumn (org.talend.cwm.relational.TdColumn)2 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)2 Catalog (orgomg.cwm.resource.relational.Catalog)2 IDesignerCoreService (org.talend.designer.core.IDesignerCoreService)1