dede kyuti
dede kyuti

Reputation: 21

GridDB_Python - unable to get row count on griddb table

I'm trying to write and read large data on griddb using python, and I'll try it first for small data. But when I wanted to calculate the row count, I failed to get it, even though I had tried several variations. Here's my code snippet:

import griddb_python

def create_griddb_connection():
    factory = griddb_python.StoreFactory.get_instance()
    return factory.get_store(host='127.0.0.1', port=10001, cluster_name='myCluster', username='admin', password='admin')

def get_container(store, container_name):
    desc = griddb_python.ContainerInfo(
        name=container_name,
        column_info_list=[
            ["id", griddb_python.Type.INTEGER],
            ["data", griddb_python.Type.STRING]
        ],
        type=griddb_python.ContainerType.COLLECTION
    )
    container = store.put_container(desc)
    return container

def write_data(container, num_records=1000):
    for i in range(num_records):
        data = [i, f"data_{i}"]
        container.put(data)

    print(f"Write {num_records} data completed")

def read_data(container, container_name):
    sqlSelect = f"SELECT * FROM {container_name} limit 1"
    query = container.query(sqlSelect)
    result = query.fetch()
    if result.has_next():
        row = result.next()
        print (f"First row data: [{row[0]}-{row[1]}]")
    else:
        print("No rows")

    sqlSelect = f"SELECT COUNT(*) FROM {container_name}"
    query = container.query(sqlSelect)
    result = query.fetch()
    if result.has_next():
        row = result.next()
        #print (f"Row count:{row[0]}")
        #print (f"Row count:{row.getvalue(0)}")
        #print (f"Row count:{row.get(0)}")
    else:
        print("No rows")

    result.close()
    query.close()

def main():
    store = create_griddb_connection()    
    container_name = "test_container"

    container = get_container(store, container_name)
    write_data(container, num_records=1000)
    read_data(container, container_name)

    container.close()
    store.close()

if __name__ == "__main__":
    main()

When I executed it without "SELECT COUNT", it is successful with the output:

Write 1000 data completed
First row data: [0-data_0]

But when I uncommented the print command on "SELECT COUNT", it produces error with the following error message:

print (f"Row count:{row[0]}") produces error: 'AggregationResult' object is not subscriptable

print (f"Row count:{row.getvalue(0)}") produces error: 'AggregationResult' object has no attribute 'getvalue'

print (f"Row count:{row.get(0)}") produces error: griddb_python.GSException

When I detailed the error message, with this code snippet:

try:
    row = result.next()
    print (f"Row count:{row.get(0)}")
except Exception as e:
    for i in range(e.get_error_stack_size()):
        print("[", i, "]")
        print(e.get_error_code(i))
        print(e.get_message(i))

It produces an error message: [140037:CC_NO_SUCH_ELEMENT]

Has anyone experienced something similar?

Upvotes: 0

Views: 28

Answers (0)

Related Questions