0

This is the code I am using to get triggers information using schemacrawler utility class in java. I am extracting metadata so i need whole metadata information.

`public class Metadata {

/**
 * Extracts metadata from a database connection using SchemaCrawler tool
 * 
 * @param SourceDetails , contains the connection details for database connection
 * @return a DbMetadata object containing metadata consisting of schema,table,column,and data types.
 */

public DbMetadata MetadataExtract(SourceDetails source) throws IOException {
System.out.println("Line 54.......");
    DbMetadata data = new DbMetadata();
    //StringBuilder sb= new StringBuilder();
    
    final SchemaCrawlerOptions options =
            SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions();
    
    final Catalog catalog = SchemaCrawlerUtility.getCatalog(ConnectionUtil.getConnection(source), options);
    ArrayList<String> colNames = new ArrayList<>();
    ArrayList<String> tableNames =  new ArrayList<>();
    ArrayList<String> dataTypes =  new ArrayList<>();
    String schemaName = source.getName();
    
    String[] url =source.getUrl().split(Pattern.quote("/"));
    for(int i=0; i<url.length; i++) {
        //System.out.println("URL......writeMetaData...."+url[i]);
        }
    String[] URLx = url[0].split(Pattern.quote(":"));
    String DBName= URLx[1];
    System.out.println("DBname..."+DBName);
    Calendar calendar = Calendar.getInstance();
    Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
    String fileName ="File"+timestamp.getDate()+timestamp.getMonth()+timestamp.getYear()+timestamp.getHours()+timestamp.getMinutes()+timestamp.getSeconds()+".xlsx";
    java.io.File file = new java.io.File(fileName);
    Workbook workbook=null;
    Sheet sheet=null;
    Sheet sheet1=null;
    Sheet sheet2=null;
    int i=0,k=0,l;
    if(file.exists())
    {
        System.out.println("inside if condition line 79.....");
        FileInputStream inputStream = new FileInputStream(file);
        
        //Creating workbook from input stream
        workbook = WorkbookFactory.create(inputStream);

        //Reading sheets of excel file
       sheet = workbook.getSheetAt(0);
       sheet1 = workbook.getSheetAt(1); 
       sheet2 = workbook.getSheetAt(2);
        //Getting the count of existing records
       i = sheet.getLastRowNum();
       k = sheet1.getLastRowNum();
       //l = sheet2.getLastRowNum();
       l=2;
       inputStream.close();
      
    }
    else {
        workbook = new XSSFWorkbook();

        
        sheet = workbook.createSheet("Column");
        sheet1= workbook.createSheet("Table");
        sheet2 =workbook.createSheet("Schema");
        Row header1=sheet.createRow(0);
        header1.createCell(0).setCellValue("ColumnName");
        header1.createCell(1).setCellValue("Datatype");
        header1.createCell(2).setCellValue("DefaultValues");
        header1.createCell(3).setCellValue("isPrimaryKey");
        header1.createCell(4).setCellValue("TableName");
        i=1;
        Row header2=sheet1.createRow(0);
        header2.createCell(0).setCellValue("TableName");
        header2.createCell(1).setCellValue("SchemaName");
        header2.createCell(2).setCellValue("DBName");
        k=1;
        Row header3=sheet2.createRow(0);
        header3.createCell(0).setCellValue("SchemaName");
        header3.createCell(1).setCellValue("DBName");
        l=1;
    }
    
    for (final Schema schema : catalog.getSchemas())
    {
        
          
        Map<String,ArrayList<String>> tableColumnNames=new LinkedHashMap<String,ArrayList<String>>();
        if (schemaName.equals(schema.toString())){
    
            
                  
              for (Table table : catalog.getTables(schema))
              {
                 
                  
                  System.out.println("Triggers_______"+table.getTriggers());
                 
                    for(schemacrawler.schema.Trigger trigger : table.getTriggers()) {
                        String triggerName= trigger.getFullName();
                        System.out.println("Triggers...."+ triggerName);
                    }
                System.out.print("o--> " + table);
                tableNames.add(table.getName());
                String tableName=table.getName();
                //colNames.add("----------------------------");
                //dataTypes.add("----------------------------");
                ArrayList<String> columnNames=new ArrayList<String>();
                
                    for (Column column : table.getColumns())
                    {
                        
                        System.out.println("o--> " + column + " (" + column.getColumnDataType().getJavaSqlType()+ ")");
                        
                        colNames.add(column.getFullName().replace(schemaName + ".", "")+" = "+column.getDefaultValue()+"."+column.isPartOfPrimaryKey());
                        dataTypes.add(column.getFullName().replace(schemaName + ".", "") + " = " + column.getColumnDataType().toString().toLowerCase());
                        
                        columnNames.add(column.getFullName().replace(schemaName + ".", "").replace(tableName+".","" )+"."+column.getColumnDataType()+"."+column.getDefaultValue()+"."+column.isPartOfPrimaryKey());
                
                    }
                    
                        tableColumnNames.put(tableName, columnNames);
                        
                }
              
              
             System.out.println("Table Column Names...."+tableColumnNames);
             
             /** Column Sheet   */
             for (Map.Entry<String, ArrayList<String>> map: tableColumnNames.entrySet()) {
                System.out.println("106 line no key: " + map.getKey() + " value: " + map.getValue());
                 
                 
                 String key=map.getKey(); 
                 ArrayList<String> value=map.getValue();
                 

                 for(String str:value)
                 {
                     StringTokenizer stz=new StringTokenizer(str,".");
                     String o=stz.nextToken();
                     String t=stz.nextToken();
                     String st=stz.nextToken();
                     String pk=stz.nextToken();
                     int  j=0;
                     Row row = sheet.createRow(i);
                     Cell cell = row.createCell(j);
                        cell.setCellValue(o);
                        j++;
                        row.createCell(j).setCellValue(t);
                        j++;
                        row.createCell(j).setCellValue(st);
                        j++;
                        row.createCell(j).setCellValue(pk);
                        j++;
                        row.createCell(j).setCellValue(key);
                         i++;
                         
                         
                 }  
                    
    }
            
              /** Table Sheet   */           
             for (final Table table : catalog.getTables(schema))
              {
                 System.out.println("Line no 176....."+table.getName());
                    
                      int m=0; 
                      Row row1 = sheet1.createRow(k);
                      
                      Cell cell1 = row1.createCell(m); 
                      cell1.setCellValue(table.getName());
                      m++;
                      row1.createCell(m).setCellValue(schema.getFullName()); 
                      m++;
                      row1.createCell(m).setCellValue(DBName); 
                      k++;
                    
              }
              /** Schema Sheet  */
              System.out.println("Line no 232...."+schema.getFullName());
              int n=0;
              Row row2= sheet2.createRow(l);
              Cell cell2 = row2.createCell(n);
              cell2.setCellValue(schema.getFullName());
              n++;
              row2.createCell(n).setCellValue(DBName); 
             
              
             
                
              /** writing file  */
                FileOutputStream fos = new FileOutputStream(file);
                workbook.write(fos);
                workbook.close();
                
                data.setColNames(colNames);
                data.setSchemaName(schemaName);
                data.setTableNames(tableNames);
                data.setDataTypes(dataTypes);
                
                return data;
                
            }
            
        
    }
    return null;
    
}`

I am expecting triggers data from table in java code using schemacrawlerutility, I have already triggers created in my database

1 Answers1

0
  1. Please use the maximum info-level. See the example code for how to do this.
  2. Please increase the logging level if you need to send logs as suggested on the SchemaCrawler website
  3. Please make sure that the SchemaCrawler plugin jars are on your CLASSPATH for the databases that you are working with.

Sualeh Fatehi SchemaCrawler

Sualeh Fatehi
  • 4,700
  • 2
  • 24
  • 28