在phoenix:python中插入多列
我创建了phoenix表,如下所示:在phoenix:python中插入多列,python,hbase,phoenix,Python,Hbase,Phoenix,我创建了phoenix表,如下所示: create table movement_record( id varchar not null, latitude float, longitude float, location varchar, duration float, start_time varchar not null, end_time varchar not null, CONSTRAINT pk PRIMARY KEY (id, start_time, end_time) );
create table movement_record(
id varchar not null,
latitude float,
longitude float,
location varchar,
duration float,
start_time varchar not null,
end_time varchar not null,
CONSTRAINT pk PRIMARY KEY (id, start_time, end_time)
);
我想把清单写在上面的表格里。列表列表(final_output
)数据变量为[[string,float,float,string,float,datetime,datetime],-],-]
我尝试使用以下代码在Phoenix中编写数据:
_phoenixdb = phoenixdb
conn = _phoenixdb.connect(connection_string, autocommit=True)
cursor = conn.cursor()
for row in final_output:
id=row[0]
latitude=row[1]
longitude=row[2]
ignition_on=row[3]
location=row[4]
duration=row[5]
start_time=row[6]
end_time=row[7]
sql = "UPSERT INTO movement_record VALUES (?,?,?,?,?,?,?,?)", (truck_id,latitude,longitude,ignition_on,location,duration,start_time,end_time)
cursor.execute(sql)
我得到一个错误:
Traceback (most recent call last):
File "main.py", line 61, in output_to_phoenix
cursor.execute(sql)
File "/usr/local/lib/python2.7/dist-packages/phoenixdb/cursor.py", line 201, in execute
operation, first_frame_max_size=self.itersize)
File "/usr/local/lib/python2.7/dist-packages/phoenixdb/avatica.py", line 399, in prepare_and_execute
request.sql = sql
TypeError: ('UPSERT INTO movement_record VALUES (?,?,?,?,?,?,?,?)', (u'SRT1', -23.333999633789062, has type tuple, but expected one of: bytes, unicode
这看起来是显而易见的解决方案,但我无法找出我在这里遗漏了什么。我们将不胜感激。谢谢。好的,第一个参数
cursor.execute
是一个字符串,第二个是一个参数元组。我建议如下更改代码:
sql = "UPSERT INTO movement_record VALUES (?,?,?,?,?,?,?,?)"
params = (truck_id,latitude,longitude,ignition_on,location,duration,start_time,end_time)
cursor.execute(sql, params)
如果要保留预构造的参数元组,可以执行以下操作:
cursor.execute(*sql)
这将展开元组并将项目作为单个参数传递。感谢Lukas,我使用了第二个选项。