1. 程式人生 > >pyodps 獲取表資料,向表寫資料,行記錄Record報錯

pyodps 獲取表資料,向表寫資料,行記錄Record報錯

阿里雲pyodps文件 http://pyodps.readthedocs.io/zh_CN/latest/base-tables-zh.html

獲取表資料,向表寫資料,行記錄Record報錯

報錯資訊:

>>> with mytable.open_writer(partition='pt=test') as writer:
...     writer.write([1,2])  
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/contextlib.py", line 16, in __enter__
    return self.gen.next()
  File "/usr/lib/python2.6/site-packages/odps/models/table.py", line 403, in open_writer
    upload_id=upload_id, **kw)
  File "/usr/lib/python2.6/site-packages/odps/tunnel/tabletunnel/tabletunnel.py", line 54, in create_upload_session
    compress_option=compress_option)
  File "/usr/lib/python2.6/site-packages/odps/tunnel/tabletunnel/uploadsession.py", line 57, in __init__
    self._init()
  File "/usr/lib/python2.6/site-packages/odps/tunnel/tabletunnel/uploadsession.py", line 71, in _init
    resp = self._client.post(url, {}, params=params, headers=headers)
  File "/usr/lib/python2.6/site-packages/odps/rest.py", line 120, in post
    return self.request(url, 'post', data=data, **kwargs)
  File "/usr/lib/python2.6/site-packages/odps/rest.py", line 112, in request
    errors.throw_if_parsable(res)
  File "/usr/lib/python2.6/site-packages/odps/errors.py", line 78, in throw_if_parsable
    raise NoSuchObject('No such object.')
odps.errors.NoSuchObject: No such object.

解決:

找不到分割槽,也就是mytable表沒有test分割槽

可以先新增分割槽

mytable.create_partition('pt=test')

有test分割槽後就能寫如資料了

>>> with mytable.open_writer(partition='pt=test') as writer:
...     records = [[111],                
...                 [222],
...               [333],
...               [444]]
...     writer.write(records)

報錯資訊:

>>> with mytable.open_writer(partition='pt=test') as writer:
...     records=[1,2,3,4]                                   
...     writer.writer(records)                              
... 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/lib64/python2.6/contextlib.py", line 34, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python2.6/site-packages/odps/models/table.py", line 477, in open_writer
    yield RecordWriter()
  File "<stdin>", line 3, in <module>
AttributeError: 'RecordWriter' object has no attribute 'writer'
>>>   

解決:
writer.write(records)中第二個writer多個r,如下
>>> 
>>> with mytable.open_writer(partition='pt=test') as writer:
...     records = [[111],                
...                 [222],
...               [333],
...               [444]]
...     writer.write(records)
...