我刚刚开始在10.13.2上使用TabPy。我使用了conda pymysql包(pymysql和PyMySQL-0.8.0-py2.evg-info),并将它们放在anaconda的site包中,这样Tableau就可以连接到数据库,检索数据集,并保存在计算字段中。
我最初尝试了mysql.connector,就像我在PyCharm和CLI中所做的那样,但TabPy使用的是anaconda,它没有用于mysql的站点包。
无论如何,它最初连接到TabPy服务器,该服务器返回:
An error occurred while communicating with the Predictive Service.但是,紧随其后的是下面两行:
Error processing script
TypeError : %d format: a number is required, not str我已经对前面提到的错误做了一些挖掘,我尝试过的所有东西都产生了相同的错误。我找到了解决方案,但最后我又犯了一个错误。
SCRIPT_REAL("
import pymysql
db = pymysql.connect(
host='localhost',
port='9999',
user='usern',
passwd='passw',
db='someDb'
)
cur = db.cursor()
t1 = int(0)
t2 = datetime.datetime(2018, 2, 1)
sqlStr = 'select distinct APPL_ID, APPL_SUBMIT_DT from APPL_APP where APPL_ACTIVE_FLAG > %d and APPL_SUBMIT_DT >= %d' % (t1, t2)
cur.execute()
for row in cur.fetchall():
print row
db.close()
",
COUNT([Appl Id])
)我不明白为什么脚本会返回这样的错误,直到我在PyCharm中运行它。我的端口号需要是一个数字而不是一个字符串。
import pymysql
db = pymysql.connect(
host='localhost',
port=9999,
user='usern',
passwd='passw',
db='someDb'
)
cur = db.cursor()
t1 = int(0)
t2 = (2018-02-01)
sqlStr = 'select distinct APPL_ID, APPL_SUBMIT_DT from APPL_APP where APPL_ACTIVE_FLAG > %d and APPL_SUBMIT_DT >= %d' % (t1, t2)
cur.execute(sqlStr)
for row in cur.fetchall():
print row
db.close()当然,虽然我可以看到所有数据都是通过TabPy服务器在我的终端中返回的,但它以以下方式结束:
(3957423, datetime.datetime(2018, 2, 27, 15, 30, 16))
(3957424, datetime.datetime(2018, 2, 27, 15, 31))
(3957425, datetime.datetime(2018, 2, 27, 15, 31, 4))
(3957426, datetime.datetime(2018, 2, 27, 15, 31, 55))
(3957428, datetime.datetime(2018, 2, 27, 15, 32, 17))
(3957429, datetime.datetime(2018, 2, 27, 15, 32, 18))
None
ERROR:__main__:{"info": null, "ERROR": "Error running script. No return value"}那件事怎么可能?这里有明确的数据。
发布于 2018-03-01 19:18:51
要让您的脚本在Tableau中工作,您需要使用python命令return something,其中的内容是包含适当返回类型的列表。否则,这些值可能存在于python中,但是Tableau看不到它们。
在这种情况下,您需要使用如下代码构建一个列表:
ReturnValues=[]
for row in cur.fetchall():
ReturnValues.append(row)
return ReturnValues然后,完整的行列表将被发送回Tableau。不过,您可能仍然会遇到问题,因为Tableau需要一个特定大小的列表,该列表与发送到python的输入列表匹配。您没有这样的输入,这可能会导致问题。
https://stackoverflow.com/questions/49018461
复制相似问题