def remove_tasks_by_attribute(self, tablename, attribute_name, attribute_value): logging.debug( 'remove_tasks_by_attribute: tablename = {0} attribute_name = {1} attribute_value = {2}' .format(tablename, attribute_name, attribute_value)) try: dynamo = boto.connect_dynamodb( aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key) if self.tableexists(tablename): table = dynamo.get_table(tablename) results = table.scan( scan_filter={ attribute_name: condition.EQ(attribute_value) }) for result in results: result.delete() return True else: logging.debug( 'exiting removetask with error : table doesn\'t exists') return False except Exception, e: logging.error('exiting removetask with error {0}'.format(str(e)))
def test_scan(): conn = boto.connect_dynamodb() table = create_table(conn) item_data = { 'Body': 'http://url_to_lolcat.gif', 'SentBy': 'User A', 'ReceivedTime': '12/9/2011 11:36:03 PM', } item = table.new_item( hash_key='the-key', range_key='456', attrs=item_data, ) item.put() item = table.new_item( hash_key='the-key', range_key='123', attrs=item_data, ) item.put() item_data = { 'Body': 'http://url_to_lolcat.gif', 'SentBy': 'User B', 'ReceivedTime': '12/9/2011 11:36:03 PM', 'Ids': set([1, 2, 3]), 'PK': 7, } item = table.new_item( hash_key='the-key', range_key='789', attrs=item_data, ) item.put() results = table.scan() results.response['Items'].should.have.length_of(3) results = table.scan(scan_filter={'SentBy': condition.EQ('User B')}) results.response['Items'].should.have.length_of(1) results = table.scan(scan_filter={'Body': condition.BEGINS_WITH('http')}) results.response['Items'].should.have.length_of(3) results = table.scan(scan_filter={'Ids': condition.CONTAINS(2)}) results.response['Items'].should.have.length_of(1) results = table.scan(scan_filter={'Ids': condition.NOT_NULL()}) results.response['Items'].should.have.length_of(1) results = table.scan(scan_filter={'Ids': condition.NULL()}) results.response['Items'].should.have.length_of(2) results = table.scan(scan_filter={'PK': condition.BETWEEN(8, 9)}) results.response['Items'].should.have.length_of(0) results = table.scan(scan_filter={'PK': condition.BETWEEN(5, 8)}) results.response['Items'].should.have.length_of(1)
def test_scan(): conn = boto.connect_dynamodb() table = create_table(conn) item_data = { "Body": "http://url_to_lolcat.gif", "SentBy": "User A", "ReceivedTime": "12/9/2011 11:36:03 PM", } item = table.new_item(hash_key="the-key", range_key="456", attrs=item_data) item.put() item = table.new_item(hash_key="the-key", range_key="123", attrs=item_data) item.put() item_data = { "Body": "http://url_to_lolcat.gif", "SentBy": "User B", "ReceivedTime": "12/9/2011 11:36:03 PM", "Ids": set([1, 2, 3]), "PK": 7, } item = table.new_item(hash_key="the-key", range_key="789", attrs=item_data) item.put() results = table.scan() results.response["Items"].should.have.length_of(3) results = table.scan(scan_filter={"SentBy": condition.EQ("User B")}) results.response["Items"].should.have.length_of(1) results = table.scan(scan_filter={"Body": condition.BEGINS_WITH("http")}) results.response["Items"].should.have.length_of(3) results = table.scan(scan_filter={"Ids": condition.CONTAINS(2)}) results.response["Items"].should.have.length_of(1) results = table.scan(scan_filter={"Ids": condition.NOT_NULL()}) results.response["Items"].should.have.length_of(1) results = table.scan(scan_filter={"Ids": condition.NULL()}) results.response["Items"].should.have.length_of(2) results = table.scan(scan_filter={"PK": condition.BETWEEN(8, 9)}) results.response["Items"].should.have.length_of(0) results = table.scan(scan_filter={"PK": condition.BETWEEN(5, 8)}) results.response["Items"].should.have.length_of(1)
def getEntry(self, attribute_name=str(), attribute_value=str(), table_name=str()): try: dynamo = boto.connect_dynamodb( aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key) if not self.tableexists(table_name): self.createtable(table_name) table = dynamo.get_table(table_name) results = table.scan( scan_filter={attribute_name: condition.EQ(attribute_value)}) return results except Exception, e: logging.error('exiting getEntry with error {0}'.format(str(e))) return None
def main(): db = boto.connect_dynamodb() raw_input( "I am now going to create two tables called zipcodes and zipcodes2. Press enter to continue." ) schema1 = db.create_schema(hash_key_name='zip codes', hash_key_proto_value=str) schema2 = db.create_schema(hash_key_name='town', hash_key_proto_value=str, range_key_name='person', range_key_proto_value=str) try: table1 = createTable('zipcodes', schema1) except: pass try: table2 = createTable('zipcodes2', schema2) except: pass for x in db.list_tables(): print db.get_table(x) table1 = db.get_table('zipcodes') table2 = db.get_table('zipcodes2') raw_input( "Here is a description of each table in my account. First table 1 then table 2." ) for x in db.list_tables(): print "" for a, b in db.describe_table(x)['Table'].items(): print a, b raw_input("I am now going to add the data from the text file online.\n") text = urllib2.urlopen( "https://s3.amazonaws.com/depasquale/datasets/zipcodes.txt") for x in range(0, 20): y = text.readline().split(',') zip = y[0].replace('"', '') #print zip; long = float(y[1].replace('"', '')) lat = float(y[2].replace('"', '')) town = y[3].replace('"', '') city = y[4].replace('"', '') lName = y[5].replace('"', '') fName = y[6].replace('"', '') item_data = { 'long': long, 'lat': lat, 'town': town, 'city': city, 'lName': lName, 'fName': fName, } item = table1.new_item(hash_key=zip, attrs=item_data) #print item item.put() #for z in range(0,len(y)): # if (z!=2 or z!=1): # print y[z].replace('"','') # else: # print y[z] raw_input("First table loaded. Time to load the second!") #Seconds table text2 = urllib2.urlopen( "https://s3.amazonaws.com/depasquale/datasets/zipcodes.txt") for x in range(0, 40): y = text2.readline().split(',') zip = y[0].replace('"', '') long = float(y[1].replace('"', '')) lat = float(y[2].replace('"', '')) town = y[3].replace('"', '') city = y[4].replace('"', '') lName = y[5].replace('"', '') fName = y[6].replace('"', '') item_data = { 'long': long, 'lat': lat, 'town': town, 'lName': lName, 'fName': fName, } item = table2.new_item(hash_key=town, range_key=zip, attrs=item_data) item.put() #for z in range(0,len(y)): # if (z!=2 or z!=1): # print y[z].replace('"','') # else: # print y[z] raw_input("Done loading the second table...\n") raw_input( "Here are the results for Table 1 Hash Key only table zips > 00610:") print "" result = table1.scan(scan_filter={'zip codes': condition.GT("00610")}) for x in result: print x print "" raw_input("Table 2 query: City name is ARECIBO:") print "" result = table2.query(hash_key="ARECIBO") for x in result: print x print "" raw_input("Table 2 query: City name greater than ARECIBO:") print "" result = table2.scan(scan_filter={'town': condition.GT("ARECIBO")}) for x in result: print x print "" zipToDelete = raw_input( "Let's delete a record. Enter a record with a zip to delete from table1: " ) toDelete = table1.scan( scan_filter={'zip codes': condition.EQ(zipToDelete)}) for x in toDelete: print x raw_input("The tupple above is going to be removed after enter") expected_value = {'zip codes': zipToDelete} table1.get_item(zipToDelete).delete() raw_input("The Vale should be deleted. Go check!") zipToDelete = raw_input( "Let's change a record. Enter the zip of the record to change: ") field = raw_input("enter the name of the field to change: ") changeTo = raw_input("What would you like to change it too?: ") ourItem = table1.get_item(hash_key=zipToDelete) ourItem[field] = changeTo ourItem.put() raw_input( "Value changed. I am going to change the throughput for both of the tables." ) #try: # table1.update_throughput(2,2); #except: # print "error" raw_input("Table1 throughput changed") try: table2.update_throughput(3, 3) except: print "error" raw_input("Table2 throughput changed") raw_input("I am now going to delete both tables. Press enter to delete.") try: table1.delete() table2.delete() except: pass
# Copy your AWS secret access key value below: AWS_SECRET_ACCESS_KEY = 'your_secret_access_key' # Set the name of your AWS cloud thermometer table below: TABLE_NAME = 'Temperatures' # Set to the name of the Id value to delete. All rows which have this ID will be deleted! ID_TO_DELETE = 'id_value_to_delete' # Connect to the table. conn = boto.dynamodb.connect_to_region( REGION, aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_ACCESS_KEY) temperatures = conn.get_table(TABLE_NAME) # Print a warning print 'About to delete all rows with Id {0} from table {1} in {2}.'.format( ID_TO_DELETE, TABLE_NAME, REGION) print 'Are you sure? (y/n)' response = raw_input().upper() if response != 'Y' and response != 'YES': print 'OK, not deleting anything!' quit() # Scan the table and delete all items which match the provided ID print 'Deleting rows...' count = 0 for row in temperatures.scan(scan_filter={'Id': condition.EQ(ID_TO_DELETE)}): count += 1 row.delete() print 'Deleted {0} rows.'.format(count)
import boto from boto import dynamodb from boto.dynamodb import condition # Usamos las credenciales del env, para mostrar como no necesitamos explicitarlo en el codigo conn = boto.connect_dynamodb() table = conn.get_table("asistentes_pycon_ar") print "--- get" print table.get_item(hash_key='gutes', range_key='30') print "--- scan" for row in table.scan(scan_filter={"edad": condition.EQ("30")}): print row print "--- query" for row in table.query("gutes", range_key_condition=condition.EQ("30")): print row