記錄一個坑,python連Impala,插入數據到Kudu,會有特殊字符問題,冒號,涉及到時間格式與ip地址等場景
>>> >>>from impala.dbapi import connect
>>> >>>host='xx.xx.xx.xx'
>>> >>>conn = connect(host=host, port=25001, timeout=3600)
>>> >>>cursor = conn.cursor()
>>> >>>sql = "Insert INTO temp_table (access_time,news_id,guid,account_id) values(%s,%s,%s,%s)"
>>> >>>para = ('2018-09-13 18:47:47',558952,'fsd','fsdfsd')
>>> >>>cursor = conn.cursor()
ERROR:
impala.error.HiveServer2Error: AnalysisException: Syntax error in line 1:
...lues('2018-09-13 18'fsdfsd'7'fsdfsd'7',558952,'fsd','f...
Encountered: STRING LITERAL
Expected: CROSS, FROM, FULL, GROUP, HAVING, INNER, JOIN, LEFT, LIMIT, OFFSET, ON, ORDER, RIGHT, STRAIGHT_JOIN, TABLESAMPLE, UNION, USING, WHERE, COMMA
CAUSED BY: Exception: Syntax error
大家可以看到impala解析的時候,將時間格式解析成: '2018-09-13 18'fsdfsd'7'fsdfsd'7'
>>> >>> sql = "Upsert INTO temp_table (access_time,news_id,guid,account_id) values('{0}',{1},'{2}','{3}')".format('2018-09-13 18:47:47','558952','fsd','fsdfsd')
sql="Upsert INTO temp_table (access_time,news_id,guid,account_id) values('2018-09-13 18:47:47',558952,fsd,fsdfsd)"
>>> >>> cursor.execute(sql)
>>> >>> ok