0

I am experimenting with pygrametl, trying to get data from one table in a database (source) into another table in a destination database.

The source table has the following schema:

CREATE TABLE `sdata` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fname` varchar(255) DEFAULT NULL,
  `descr` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=' ';

The destination table uses the following schema:

CREATE TABLE `dtable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dname` varchar(255) DEFAULT NULL,
  `ddescr` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In python I have the following code:

import pygrametl
from pygrametl.datasources import SQLSource, CSVSource
from pygrametl.tables import Dimension, FactTable, SlowlyChangingDimension
import pymysql

sourceDatabase = pymysql.connect(host='localhost', user='root', password='pass', database='source')
destDatabase = pymysql.connect(host='localhost', user='root', password='pass', database='dest')

dw_conn_wrapper = pygrametl.ConnectionWrapper(connection=destDatabase)

sql = "SELECT fname, descr from sdata"

name_mapping = 'fname', 'descr'

source = SQLSource(connection=sourceDatabase, query=sql, names=name_mapping)

destDimension = Dimension(
    name='dtable',
    key='id',
    attributes=['dname', 'ddescr'])

for row in source:
    print(row)
    destDimension.insert(row)

dw_conn_wrapper.commit()
dw_conn_wrapper.close()

sourceDatabase.close()

The error I am getting is as following:

Traceback (most recent call last): File ".\testex.py", line 35, in sourceDimension.insert(row) File "Python\Python36-32\lib\site-packages\pygrametl\tables.py", line 357, in insert self.targetconnection.execute(self.insertsql, row, namemapping) File "Python\Python36-32\lib\site-packages\pygrametl__init__.py", line 663, in execute self.__cursor.execute(stmt, arguments) File "Python\Python36-32\lib\site-packages\pymysql\cursors.py", line 164, in execute query = self.mogrify(query, args) File "Python\Python36-32\lib\site-packages\pymysql\cursors.py", line 143, in mogrify query = query % self._escape_args(args, conn) KeyError: 'dname'

4

1 回答 1

1

您的属性应该在查询产生的数据中。您可以使用:

name_mapping = 'dname', 'ddescr'
attributes=['dname', 'ddescr'])

或者:

name_mapping = 'fname', 'descr'
attributes=['fname', 'descr'])
于 2017-10-22T20:35:19.633 回答