Access to collection data in python

Hello I want to get the name of all tables in a cllection, but I do not know to do it.
I write this line
Tablas = RS.Collection.list(workspace = WorkSpace)
for tabla in Tablas:
print(type(tabla))
print(tabla)

tabla is <class ‘rockset.collection.Collection’>
I want to get the key name
For example
{‘client’: <rockset.client.Client object at 0x7f313071e950>, ‘workspace’: ‘sandbox’, ‘name’: ‘Transactions’, ‘dropped’: False, …}

When I write print(tabla[‘name’]) this is the error
<class ‘rockset.collection.Collection’>
Traceback (most recent call last):

print(tabla[‘name’])
TypeError: ‘Collection’ object is not subscriptable

How can I get data o transform rockset.collection.Collection in json object to choose all values of this data?

1 Like

A possible solution is:

def ListaColecciones(Credenciales, WorkSpace):
    '''
        Dadas unas credenciales válidas y un workspace (espquema de BBDD) válido
        devolvemos la lista de tablas que tiene ese esquema
    '''
    import json    
    from rockset import Client, exception
    RS = ClienteRD(Credenciales)
    ListaTablas = []
    try: 
        Tablas = RS.Collection.list(workspace = WorkSpace)
        for tabla in Tablas:
            # Convierto el objeto de Rockset en un diccionario de datos, usando un string como ayuda
            str_data = str(tabla).replace("'", '"')
            str_data = str_data.replace("False", 'false')
            str_data = str_data.replace("True", 'true')
            str_data = str_data.replace(": None", ": null")
            Aux = str_data.split(',') # Parto la cadena de caracteres y quito el primer elemento
            i = 0
            while i < len(Aux):
                if i == 0:
                    AuxString = '{'
                else:
                    if i == 1:
                        AuxString = AuxString + Aux[i]
                    else:
                        AuxString = AuxString + ',' + Aux[i]
                i = i + 1                
            json_object = json.loads(AuxString)
            if "name" in json_object:
                ListaTablas.append(json_object['name'])            
    except:
        print('No he podido conectar')
    return ListaTablas
1 Like
def ClienteRD(Credenciales):
    '''
        Devuelvo un cliente RS
    '''
    from rockset import Client, exception
    rs = Client(api_key=Credenciales['Clave_Api'], api_server=Credenciales['ServidorRockSet'])
    # Esto no valida nada contra la máquina
    return rs

Hi @jrios ! Touching base- Did you still need help with this?

Hello I think that it is a issue. I can not convert a rockset.collection.Collection data object in json directly.

I can use this code to correct it but I think it should be possible to use is table[‘name’]

Thanks a lot.

Thanks @jrios for this suggestion. Let me share this with the team.

Hi @jrios,

To get details on collections, we will need to use the describe() method on the collection object. The following should work:

for tabla in Tablas:
   print(tabla.describe()['data']['name'])

For more information on this method, check out the following link:
https://rockset.com/docs/client/python/python-reference.html#describe-an-existing-collection