0

我是 Scriptella 的新手,我完全陷入了以下问题。

我有给定的访问表。我正在使用我编写的程序获取表模式。输出文件 ( tableschema.xml) 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<table>
    <column>ID</column>
    <datatype>COUNTER</datatype>
    <column>FirstName</column>
    <datatype>VARCHAR</datatype>
    <column>LastName</column>
    <datatype>VARCHAR</datatype>
    <column>Salary</column>
    <datatype>CURRENCY</datatype>
    <column>SSN</column>
    <datatype>INTEGER</datatype>
</table>

然后,使用 Scriptella,我需要创建一个新的 PostgreSQL 数据库(如果可能。如果不可能,我们可以假设该数据库已经创建)。比我需要使用提供的 XML 文件创建一个新表(必需)并将所有数据从访问表复制到 PostgreSQL 表。

我实现了从访问中提取数据。我实现了从 XML 文件中提取信息。

我被困在 Postgres 中创建数据库和表。看来,CREATE DATABASE只是CREATE TABLE不通过 Scriptella 工作。

etl.xml我的文件草稿:

<!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd">
<etl>
    <connection id="db1" url="jdbc:odbc:TestDB" driver="scriptella.driver.odbc.Driver" user="Dzmitry" password="a"/>

    <connection id="db2" url="jdbc:odbc:PostgreSQLconnector" driver="scriptella.driver.odbc.Driver" classpath="postgresql.jar" user="Dzmitry" password="a"/>

    <connection id="script" driver="script"/>
    <connection id="java" driver="scriptella.driver.janino.Driver"/>
    <connection id="log" driver="text"/>

    <!--  doesn't work

    <script connection-id="db2" new-tx="true" if="create_databases">
            CREATE DATABASE testDB;
    </script>


    -->


    <query connection-id="db1">
        SELECT * FROM GenInfo;

        <script connection-id="script">
            java.lang.System.out.println("Processing row number " + rownum + " ");
        </script>
        <script connection-id="log">
            ${FirstName}, ${LastName}, ${Salary}, ${SSN}
        </script>
    </query>

    <query connection-id="java">
        import javax.xml.parsers.*;
        import javax.xml.xpath.*;
        import java.io.*;
        import org.w3c.dom.Document;
        import org.xml.sax.*;

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();

        File file = new File("tableschema.xml");
        Document document = docBuilder.parse(file);

        XPathFactory xpFactory = XPathFactory.newInstance();
        XPath xPath = xpFactory.newXPath();

        int numberOfColumns = Integer.parseInt(xPath.evaluate("count(/table/column)", document));


        for(int i = 1; i &lt;= numberOfColumns; i++){ // does not take less than sign
            System.out.print("Column name " + i + ": " + xPath.evaluate("/table/column[" + i + "]", document));
            System.out.println("\tData type " + i + ": " + xPath.evaluate("/table/datatype[" + i + "]", document));
        }

    </query>
</etl>

有人可以帮助我吗?
问候, 兹米特里。

4

1 回答 1

1

在您的脚本中,您使用 ODBC 驱动程序来访问 Postgres:

driver="scriptella.driver.odbc.Driver"

我建议使用文档中解释的 JDBC 驱动程序:

<connection driver="postgresql" url="jdbc:postgresql://localhost:5432/DATABASENAME" user="username" password="password">
</connection>

此外,您可以省略 new-tx="true" 属性,我认为在您的场景中使用它没有意义。所以从像这样简单的事情开始:

<connection connection-id="db2" driver="postgresql" url="jdbc:postgresql://localhost:5432/DATABASENAME" user="Dzmitry" password="a" classpath="postgresql.jar">
</connection>

<script connection-id="db2">
    CREATE TABLE distributors (
        did     integer PRIMARY KEY,
        name    varchar(40)
    );
</script>

在此示例中,我展示了如何为现有数据库 DATABASENAME 创建表。如果您想创建另一个数据库,脚本会更复杂,但在我提供更多详细信息之前,我需要您确认您现在能够在现有数据库中创建表。

为了创建一个数据库,首先你必须连接到一个现有的,比如 template1(它应该是预先安装的)。并从那里创建另一个数据库。然后使用单独的连接执行其他脚本。请注意,创建表脚本的连接必须启用lazy-init属性,否则会为不存在的数据库抛出错误:

<!-- Connection for just for creating a database -->
<connection connection-id="db2_init" driver="postgresql" url="jdbc:postgresql://localhost:5432/template1" user="postgres" password="postgres" classpath="postgresql.jar">
</connection>

<!-- Connection for DDL and data statements. Needs to be lazy-init=true -->
<connection connection-id="db2_tables" driver="postgresql" lazy-init="true" url="jdbc:postgresql://localhost:5432/testDB" user="Dzmitry" password="a" classpath="postgresql.jar">
</connection>

<script connection-id="db2_init" if="create_databases">
    CREATE DATABASE testDB;
</script>
<script connection-id="db2_tables" if="create_schema">
    CREATE TABLE distributors (
        did     integer PRIMARY KEY,
        name    varchar(40)
    );
</script>
于 2013-01-25T09:28:37.617 回答