0

I've started playing around with the Spanner Python client (on appengine flex), but I'm hitting a few walls when trying to reuse the doc examples. One issue I'm facing is trying to read the database:

  • execute_sql() returns empty sets (even with a simple 'SELECT 1' query)

    with database.snapshot() as snapshot:
    QUERY = ('SELECT 1')
    result = snapshot.execute_sql(QUERY)
    for row in result.rows:
        print(row)
    
  • read() returns a "AttributeError: 'list' object has no attribute 'to_pb'" error, whether I

    • try to build the whole keyset as a dictionary

      keyset = {'keys': ['f0_'], 
                'ranges': [{'startClosed': [], 'endClosed': [500]}], 
                'all': False}
      
    • passing value lists directly

      keyset = [0, 1, 2] 
      
  • and an attempt at using the Keyset() constructor tells me that spanner does not have a keyset member AttributeError: 'module' object has no attribute 'keyset'

    keys = ['f0_']
    ranges = spanner.keyset.KeyRange(start_closed=[], end_closed=[500])
    keyset = {
        'keys': keys,
        'ranges': ranges,
        'all': False}
    keyset = spanner.keyset.Keyset(keys=keys, ranges=[range])
    

So I'm a bit stuck, and I'm wondering whether there's something I'm doing wrong or the examples / docs need a bit of spolishing... Any help appreciated :)

Maxim
  • 4,075
  • 1
  • 14
  • 23
  • ok so I've moved a tiny bit forward: was able to use the Keyset / Keyranges constructor, but I get the same result.... – Thibault Lefevre Nov 16 '17 at 14:08
  • moving forward, one step at a time: I could have noticed earlier that session.read() returns a StreamedResultSet (vs. ResultSet as described in the docs). Adding .consume_all() does yield the expected result ! – Thibault Lefevre Nov 16 '17 at 19:33

1 Answers1

1

Sorry for the late response, but you can use the KeyRange and Keyset like so. Also, .consume_all() is likely to be private in the future. You should instead use list to get the answers that you want.

...
range = spanner.KeyRange(start_closed=[START], end_closed=[END])
keyset = spanner.KeySet(ranges=(closed_closed,))
rows = list(snapshot.read(self.TABLE, self.COLUMNS, keyset))
...
chemelnucfin
  • 411
  • 1
  • 4
  • 9