mardi 1 septembre 2015

Output table contents with limit and filter

I'm finding the boto dynamoDB documentation lacking almost completely of examples.

In Python, I simply want to output the contents of a table with a limit of a number of records, say 500 of the latest ones, from a certain date.

Here is what I have...

import boto.dynamodb
import sys

#----------PUBLIC VARIABLES--------------------------#

connection = boto.dynamodb.connect_to_region(
    'us-east-1',
    aws_access_key_id='somekey',
    aws_secret_access_key='somesecretkey')

#----------------------------------------------------#

def info():
    print('#########################_TABLE_NAMES_#########################')
    #get and print list of tables
    tablenames = connection.list_tables()
    for table in tablenames:
        print('DynamoDB table: %s' % table)
        #print(connection.describe_table(table))
    print('###############################################################' + '\n')

def main():
    print('###########################_RESULTS_###########################')   
    scan = myTable.scan(scan_filter=None, attributes_to_get=['SomeField'])
    results = []
    for x in scan:
        results.append(x['SomeField'])
    print('###############################################################' + '\n')  
def writeError(error):
    try:
        f = open("error.txt", "w")
        try:
            f.write(error) # Write a string to a file
        finally:
            f.close()
    except IOError:
        print "WriteError - Error!"

if __name__ == '__main__':
    try:
        info()
        main()
    except:
        writeError("Unexpected error:" + str(sys.exc_info()))
        print "Error"

The table I have hasn't got any custom indexes so I'd be looking for something pretty basic as an example.

I'm sorry I don't have a better attempt, but I've researched and not found a lot to go on.




Aucun commentaire:

Enregistrer un commentaire