def add_data_to_doc(docid, data, time): ensure_doc(docid) e = Entity() e.PartitionKey = docid e.RowKey = "{:015}".format(int(time)) e.charcount = data["charcount"] table_service.insert_or_replace_entity(table_name, e.PartitionKey, e.RowKey, e)
def test_batch_update(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_update' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) entity = self.ts.get_entity(self.table_name, '001', 'batch_update') self.assertEqual(3, entity.test3) entity.test2 = 'value1' self.ts.begin_batch() self.ts.update_entity(self.table_name, '001', 'batch_update', entity) self.ts.commit_batch() entity = self.ts.get_entity(self.table_name, '001', 'batch_update') # Assert self.assertEqual('value1', entity.test2)
def test_query_entities_large(self): # Arrange self._create_table(self.table_name) total_entities_count = 1000 entities_per_batch = 50 for j in range(total_entities_count // entities_per_batch): self.ts.begin_batch() for i in range(entities_per_batch): entity = Entity() entity.PartitionKey = 'large' entity.RowKey = 'batch{0}-item{1}'.format(j, i) entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'hello world;' * 100 entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) self.ts.commit_batch() # Act start_time = datetime.now() resp = self.ts.query_entities(self.table_name) elapsed_time = datetime.now() - start_time # Assert print('query_entities took {0} secs.'.format(elapsed_time.total_seconds())) # azure allocates 5 seconds to execute a query # if it runs slowly, it will return fewer results and make the test fail self.assertEqual(len(resp), total_entities_count)
def test_batch_insert_merge(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_or_merge_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_insert_merge') # Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4)
def test_batch_inserts(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') self.tc.begin_batch() for i in range(100): entity.RowKey = str(i) self.tc.insert_entity(self.table_name, entity) self.tc.commit_batch() entities = self.tc.query_entities(self.table_name, "PartitionKey eq 'batch_inserts'", '') # Assert self.assertIsNotNone(entities) self.assertEqual(100, len(entities))
def sanity_insert_entity(self): resp = self.tc.insert_entity(TABLE_NO_DELETE, {'PartitionKey':'Lastname', 'RowKey':'Firstname', 'age':39, 'sex':'male', 'birthday':datetime(1973,10,04)}) self.assertEquals(resp, None) entity = Entity() entity.PartitionKey = 'Lastname' entity.RowKey = 'Firstname1' entity.age = 39 entity.Birthday = EntityProperty('Edm.Int64', 20) resp = self.tc.insert_entity(TABLE_NO_DELETE, entity) self.assertEquals(resp, None)
def test_batch_delete(self): #Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_delete' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.test_table, entity) entity = self.tc.get_entity(self.test_table, '001', 'batch_delete') #self.assertEqual(3, entity.test3) self.tc.begin_batch() self.tc.delete_entity(self.test_table, '001', 'batch_delete') self.tc.commit_batch()
def test_batch_insert(self): #Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_entity(self.test_table, entity) self.tc.commit_batch() #Assert result = self.tc.get_entity(self.test_table, '001', 'batch_insert') self.assertIsNotNone(result)
def sendData(temp, hum): reading = Entity() reading.PartitionKey = Ort if hostname == "raspberry1": reading.RowKey = DatumUhrzeit+"B" else: reading.RowKey = DatumUhrzeit+"B+" reading.Datum = Datum reading.Uhrzeit = Uhrzeit reading.Temperatur = round(temp, 2) #reading.TemperaturStr = str('{0:0.2f}'.format(temp)).replace(".",",") reading.Luftfeuchte = round(hum, 2) #reading.LuftfeuchteStr = str('{0:0.2f}'.format(hum)).replace(".",",") reading.Sensor = Sensor reading.Kommentar = Kommentar try: if check_internet_available(): print "internet tut" table_service.insert_entity(table_name = table_name,entity = reading) return True except Exception, e: return False
def saveApplication(application): app = Entity() app.PartitionKey = application['partition'] #uiquely identifies a partition of entities app.RowKey = str(next(unique_sequence)) # app.RowKey = "kjlhajkdlhfjhasdnfasdkjflnasdf" app.package_type = application['apptype'] app.name = application['name'] app.reigon = application['reigon'] app.port = str(services.get_port()) app.priority = priority app.requests = str(0) app.cost = str(0) user = user_services.get_user_by_email(application['partition']) #get the current user if user is not None: app_count = int(user.app_count) #get app count an convert to int try: if app_count >= 0 : #if there is a valid amount of apps ts.insert_entity(table,app) #add new app print "new app created" app_count+=1 #increase the count user.app_count = str(app_count) #convert count back to str if user_services.update_user(application['partition'], user): #update the user count print "user app count updated" print "notifying that a new app was created..." temp = { "partition" : app.PartitionKey, "rowkey" : app.RowKey, "package_type" : app.package_type, "status" : "created" } notifying.notify_client("http://146.148.74.93:3000/update_package", temp) print " success" return app_count else : return -1 # return True except Exception, e: return -1
def createUser(new_user): user = Entity() user.PartitionKey = partition user.RowKey = new_user["email"] user.password = new_user["password"] user.firstname = new_user["firstname"] user.lastname = new_user["lastname"] user.priority = priority user.app_count = str(0) try: ts.insert_entity(table,user) return True except Exception, e: print e return False
def test_batch_insert_merge(self): #Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_or_merge_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() entity = self.tc.get_entity(self.test_table, '001', 'batch_insert_merge') #Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4)
def insert_presence(self, p): """ Uploads value to azure table storage """ t = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) task = Entity() task.PartitionKey = self._partition task.RowKey = t task.users_arrived = ','.join(map(str, p.users_arrived)) task.users_left = ','.join(map(str, p.users_left)) self._table_service.insert_entity(self._partition, task)
def test_batch_inserts(self): #Act entity = Entity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') self.tc.begin_batch() for i in range(100): entity.RowKey = str(i) self.tc.insert_entity(self.test_table, entity) self.tc.commit_batch() entities = self.tc.query_entities(self.test_table, "PartitionKey eq 'batch_inserts'", '') #Assert self.assertIsNotNone(entities); self.assertEqual(100, len(entities))
def sanity_insert_entity(self): resp = self.tc.insert_entity( TABLE_NO_DELETE, { 'PartitionKey': 'Lastname', 'RowKey': 'Firstname', 'age': 39, 'sex': 'male', 'birthday': datetime(1973, 10, 04) }) self.assertEquals(resp, None) entity = Entity() entity.PartitionKey = 'Lastname' entity.RowKey = 'Firstname1' entity.age = 39 entity.Birthday = EntityProperty('Edm.Int64', 20) resp = self.tc.insert_entity(TABLE_NO_DELETE, entity) self.assertEquals(resp, None)
def test_batch_all_operations_together(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-2' self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-3' self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-4' self.ts.insert_entity(self.table_name, entity) self.ts.begin_batch() entity.RowKey = 'batch_all_operations_together' self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-1' self.ts.delete_entity( self.table_name, entity.PartitionKey, entity.RowKey) entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 self.ts.update_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 self.ts.merge_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 self.ts.insert_or_replace_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-5' self.ts.insert_or_merge_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) self.ts.commit_batch() # Assert entities = self.ts.query_entities( self.table_name, "PartitionKey eq '003'", '') self.assertEqual(5, len(entities))
my_account = 'pipractice' my_key = 'BJNu+LXFAAoaSYm8GRNmaRcBvyM6OySC9BgKc8fr03Z9myqzVXUHtsJNpsfD7uI9a6PPi1tZwUMvkx9C2z/4Zg==' table_service = TableService(account_name=my_account, account_key=my_key) table_service.create_table('accel') def analog_read(channel): r = spi.xfer2([1, (8 + channel) << 4, 0]) adc_out = ((r[1]&3) << 8) + r[2] return adc_out while True: x = analog_read(0) y = analog_read(1) z = analog_read(2) accel = Entity() accel.PartitionKey = 'accelValue' accel.RowKey = '1' accel.x = x accel.y = y accel.z = z table_service.insert_or_replace_entity('accel', 'accelValue', '1', accel) print("X=%d\tY=%d\tZ=%d" %(x, y, z)) #accelFrom = table_service.get_entity('accel', 'accelValue', '1') #print(accelFrom.x) time.sleep(1)
def test_batch_negative(self): #Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_negative_1' entity.test = 1 self.tc.insert_entity(self.test_table, entity) entity.test = 2 entity.RowKey = 'batch_negative_2' self.tc.insert_entity(self.test_table, entity) entity.test = 3 entity.RowKey = 'batch_negative_3' self.tc.insert_entity(self.test_table, entity) entity.test = -2 self.tc.update_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) try: self.tc.begin_batch() entity.RowKey = 'batch_negative_1' self.tc.update_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) self.tc.merge_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) self.fail('Should raise WindowsAzueError exception') self.tc.commit_batch() except: self.tc.cancel_batch() pass try: self.tc.begin_batch() entity.PartitionKey = '001' entity.RowKey = 'batch_negative_1' self.tc.update_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) entity.PartitionKey = '002' entity.RowKey = 'batch_negative_1' self.tc.insert_entity(self.test_table, entity) self.fail('Should raise WindowsAzueError exception') self.tc.commit_batch() except: self.tc.cancel_batch() pass
def test_batch_all_operations_together(self): #Act entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.test_table, entity) entity.RowKey = 'batch_all_operations_together-2' self.tc.insert_entity(self.test_table, entity) entity.RowKey = 'batch_all_operations_together-3' self.tc.insert_entity(self.test_table, entity) entity.RowKey = 'batch_all_operations_together-4' self.tc.insert_entity(self.test_table, entity) self.tc.begin_batch() entity.RowKey = 'batch_all_operations_together' self.tc.insert_entity(self.test_table, entity) entity.RowKey = 'batch_all_operations_together-1' self.tc.delete_entity(self.test_table, entity.PartitionKey, entity.RowKey) entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 self.tc.update_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 self.tc.merge_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 self.tc.insert_or_replace_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-5' self.tc.insert_or_merge_entity(self.test_table, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() #Assert entities = self.tc.query_entities(self.test_table, "PartitionKey eq '003'", '') self.assertEqual(5, len(entities))
def _create_default_entity_class(self, partition, row): ''' Creates a class-based entity with fixed values, using all of the supported data types. ''' entity = Entity() entity.PartitionKey = partition entity.RowKey = row entity.age = 39 entity.sex = 'male' entity.married = True entity.deceased = False entity.optional = None entity.ratio = 3.1 entity.large = 9333111000 entity.Birthday = datetime(1973, 10, 4) entity.birthday = datetime(1970, 10, 4) entity.binary = None entity.other = EntityProperty('Edm.Int64', 20) entity.clsid = EntityProperty( 'Edm.Guid', 'c9da6455-213d-42c9-9a79-3e9149a57833') return entity
start_date_string = date_string.split(" ")[0] + ' ' + \ date_string.split(" ")[1] + ' ' + date_string.split(" ")[-1] end_date_string = date_string.split(" ")[0] + ' ' + \ date_string.split(" ")[3] + ' ' + date_string.split(" ")[-1] start_date = datetime.strptime(start_date_string, '%b %d %Y') end_date = datetime.strptime(end_date_string, '%b %d %Y') #Comment out azure SQL part as we decide to use Table Storage #cursor.execute('INSERT INTO [dbo].[Stores] \ # ([store_name],[start_date],[end_date],[description],[description_cn]) \ # values (?,?,?,?,?)', \ # store_name,start_date,end_date,description,description_cn) #cnxn.commit() #Insert data to Table Storage task = Entity() task.PartitionKey = 'Outlets' task.RowKey = store_name task.StartDate = start_date task.EndDate = end_date task.Description = description task.Description_CN = description_cn table_service.insert_entity('Outlets', task) print(store_name) print(start_date) print(end_date) print(description) print(description_cn) elif len(date_string.split(" ")) == 6: