【问题标题】:Reading Datasource metadata using apache metamodel使用 apache 元模型读取数据源元数据
【发布时间】:2015-07-03 07:28:01
【问题描述】:

我想读取数据源的元数据。无论是数据库还是文件。要求是显示可用表的集合和每个表的列。我也想知道外键等的细节。我知道这应该可以使用 JDBC API。但我想知道 Apache Metamodel 是否以可用于所有类型数据源的抽象方式支持这一点。

太好了,如果您可以分享任何示例。

…… 拉姆

【问题讨论】:

    标签: java apache-metamodel


    【解决方案1】:

    可以在 Apache Metamodel 中探索架构、表和列。 DataContext 有一个名为“information_schema”的模式,您可以看到有关数据上下文的元数据。找这个 sn-p 作为例子:

        // Prepare a data context based on plain old Java objects
        List<TableDataProvider<?>> tableDataProviders = new ArrayList<TableDataProvider<?>>();
        SimpleTableDef tableDef1 = new SimpleTableDef("snippetTableName1", new String[] {"id", "name"});
        tableDataProviders.add(new ArrayTableDataProvider(tableDef1,
                new ArrayList<Object[]>()));
        PojoDataContext dataContext = new PojoDataContext("snippetSchemaName", tableDataProviders);
    
        // Prints a schema tree
        for (Schema schema : dataContext.getSchemas()) {
            System.out.println("Schema: " + schema.getName());
            for (Table table : schema.getTables()) {
                System.out.println("\t Table: " + table.getName());
                for (Column column : table.getColumns()) {
                    System.out.println("\t\t Column: " + column.getName() + " of type: " + column.getType());
                }
            }
        }
    

    这应该打印出来:

    Schema: information_schema
     Table: tables
         Column: name of type: VARCHAR
         Column: type of type: VARCHAR
         Column: num_columns of type: INTEGER
         Column: remarks of type: VARCHAR
     Table: columns
         Column: name of type: VARCHAR
         Column: type of type: VARCHAR
         Column: native_type of type: VARCHAR
         Column: size of type: INTEGER
         Column: nullable of type: BOOLEAN
         Column: indexed of type: BOOLEAN
         Column: table of type: VARCHAR
         Column: remarks of type: VARCHAR
     Table: relationships
         Column: primary_table of type: VARCHAR
         Column: primary_column of type: VARCHAR
         Column: foreign_table of type: VARCHAR
         Column: foreign_column of type: VARCHAR
    Schema: snippetSchemaName
         Table: snippetTableName1
             Column: id of type: VARCHAR
             Column: name of type: VARCHAR
    

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 2022-11-21
      相关资源
      最近更新 更多