2017-10-11 84 views
0

我想要构建SQL查询传入spark-redshift阅读器的“查询”选项。我试图使用psycopg2,所以我做这样的事情:如何在没有连接的情况下为postgres(Redshift)生成SQL查询?

from psycopg2 import sql 

query = sql.SQL(
    "select * from {} where event_timestamp < {}" 
).format(
    sql.Identifier("events"), 
    sql.Literal(datetime.now()) 
).as_string() 

但它告诉我,我需要通过上下文(连接或光标)as_string()。我无法做到,因为我没有任何联系。

在这种情况下,我应该使用纯字符串格式进行一些转义吗?

或者有什么方法可以传递一些模拟上下文吗?为什么它需要连接来建立查询字符串? SQL查询是否因连接而改变?

回答

1

我并不熟悉spark,但如果他们没有某种sql支持,我会感到惊讶。另一种选择是像sqlbuilder这样的轻量级包装。

如果你真的想使用psycopg,我建议看看他们如何使用mocks进行单元测试 - psycopg2's ConnectingTestCase

class ConnectingTestCase(unittest.TestCase): 
    """A test case providing connections for tests. 

    A connection for the test is always available as `self.conn`. Others can be 
    created with `self.connect()`. All are closed on tearDown. 

    Subclasses needing to customize setUp and tearDown should remember to call 
    the base class implementations. 
    """ 
相关问题