本文整理匯總了Python中azure.storage.TableService.query_entities方法的典型用法代碼示例。如果您正苦於以下問題:Python TableService.query_entities方法的具體用法?Python TableService.query_entities怎麽用?Python TableService.query_entities使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類azure.storage.TableService
的用法示例。
在下文中一共展示了TableService.query_entities方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getStorageMetrics
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
def getStorageMetrics(account, key, hostBase, table, startKey, endKey):
try:
waagent.Log("Retrieve storage metrics data.")
tableService = TableService(account_name = account,
account_key = key,
host_base = hostBase)
ofilter = ("PartitionKey ge '{0}' and PartitionKey lt '{1}'"
"").format(startKey, endKey)
oselect = ("TotalRequests,TotalIngress,TotalEgress,AverageE2ELatency,"
"AverageServerLatency,RowKey")
metrics = tableService.query_entities(table, ofilter, oselect)
waagent.Log("{0} records returned.".format(len(metrics)))
return metrics
except Exception, e:
waagent.Error(("Failed to retrieve storage metrics data: {0} {1}"
"").format(printable(e), traceback.format_exc()))
AddExtensionEvent(message=FAILED_TO_RETRIEVE_STORAGE_DATA)
return None
示例2: __init__
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
class AzureDataServices:
_partition = 'presence'
def __init__(self, table):
self._partition = table
with open('azure.txt') as f:
lines = f.readlines()
acc = lines[0].strip()
key = lines[1].strip()
self._table_service = TableService(account_name=acc, account_key=key)
def create_table(self):
"""
Creates azure storage table
"""
self._table_service.create_table(self._partition)
def insert_data(self, task):
"""
Insert the object to azure
"""
t = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
task.PartitionKey = self._partition
task.RowKey = t
self._table_service.insert_entity(self._partition, task)
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 get_presence(self):
tasks = self._table_service.query_entities(self._partition, "PartitionKey eq 'presence'")
return tasks
示例3: getAzureDiagnosticMemoryData
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
def getAzureDiagnosticMemoryData(accountName, accountKey, hostBase,
startKey, endKey, hostname):
try:
waagent.Log("Retrieve diagnostic data: Memory")
table = "LinuxPerfMemVer1v0"
tableService = TableService(account_name = accountName,
account_key = accountKey,
host_base = hostBase)
ofilter = ("PartitionKey ge '{0}' and PartitionKey lt '{1}' "
"and Host eq '{2}'").format(startKey, endKey, hostname)
oselect = ("PercentAvailableMemory,Host")
data = tableService.query_entities(table, ofilter, oselect, 1)
if data is None or len(data) == 0:
return None
memoryPercent = 100 - float(data[0].PercentAvailableMemory)
return memoryPercent
except Exception, e:
waagent.Error(("Failed to retrieve diagnostic data(Memory): {0} {1}"
"").format(printable(e), traceback.format_exc()))
AddExtensionEvent(message=FAILED_TO_RETRIEVE_MDS_DATA)
return None
示例4: __init__
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
class CustomerTable:
def __init__(self):
self.table_service = TableService(account_name='portalvhdspbrd34f2fnbl',account_key='y48JkXg+VcHQRCgsylJf4xV4Fd0AuJNkQKSwGhAR+BppHnFhkI+UHPOS/oYaTo0rqFCGQkEBW+thNFZNB9W8yg==')
def insert(self,ID,Name,Country,City):
task = {'PartitionKey': 'Customer',
'RowKey': ID,
'Name' : Name ,
'Country' : Country,
'City' : City}
try:
self.table_service.insert_entity('Customer', task)
except:
print"azure.WindowsAzureConflictError: Customer Conflict"
def listAll(self):
tasks = self.table_service.query_entities('Customer', "PartitionKey eq 'Customer'")
i=0
for task in tasks:
i=i+1
print("Name: %s, Country: %s, City: %s") %(task.Name,task.Country,task.City)
print("Total Customer: %s") %(i)
示例5: TableServiceTest
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
#.........這裏部分代碼省略.........
# Act
resp = self.ts.get_entity(self.table_name, 'MyPartition', '1')
# Assert
self.assertEqual(resp.PartitionKey, 'MyPartition')
self.assertEqual(resp.RowKey, '1')
self._assert_default_entity(resp)
def test_get_entity_not_existing(self):
# Arrange
self._create_table(self.table_name)
# Act
with self.assertRaises(WindowsAzureError):
self.ts.get_entity(self.table_name, 'MyPartition', '1')
# Assert
def test_get_entity_with_select(self):
# Arrange
self._create_table_with_default_entities(self.table_name, 1)
# Act
resp = self.ts.get_entity(
self.table_name, 'MyPartition', '1', 'age,sex')
# Assert
self.assertEqual(resp.age, 39)
self.assertEqual(resp.sex, 'male')
self.assertFalse(hasattr(resp, "birthday"))
self.assertFalse(hasattr(resp, "married"))
self.assertFalse(hasattr(resp, "deceased"))
def test_query_entities(self):
# Arrange
self._create_table_with_default_entities(self.table_name, 2)
# Act
resp = self.ts.query_entities(self.table_name)
# Assert
self.assertEqual(len(resp), 2)
for entity in resp:
self.assertEqual(entity.PartitionKey, 'MyPartition')
self._assert_default_entity(entity)
self.assertEqual(resp[0].RowKey, '1')
self.assertEqual(resp[1].RowKey, '2')
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)
示例6: Azure
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
#.........這裏部分代碼省略.........
self.msg_send(dict)
except:
logging.critical(_("Message creation failure"))
#helper
def get_time(self, timestamp):
try:
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
except:
return ""
def get_timestamp_now(self):
return time.time()
#table
def tbl_update(self, name, p, r, d):
try:
d["updatetime"] = str(self.get_timestamp_now())
self.tbl.create_table(name)
self.tbl.insert_or_merge_entity(name, p, r, d)
except Exception as e:
logging.critical("Error update_tbl {0}".format(e))
def tbl_row_query(self, name, q, n=1000, next_partition_key_=None, next_row_key_=None):
try:
self.tbl.create_table(name)
rows = self.tbl.query_entities(name, filter=q, top=n,
next_partition_key=next_partition_key_,
next_row_key=next_row_key_)
return rows
except Exception as e:
return None
def entity2dict(self, e):
try:
keys = dir(e)
d = {}
for key in keys:
d[key] = getattr(e, key, "")
return d
except:
return None
def update_net(self):
try:
info = Info().get_all()
self.tbl_update(self.tbl_name, self.tbl_net, info["hostname"], info)
except:
logging.warning("Error update_net")
def wrap_text(self, text, max_width):
if text is not None:
from textwrap import wrap
return '\n'.join(wrap(text, max_width))
else:
return ""
示例7: __init__
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
class OrderTable:
def __init__(self):
self.table_service = TableService(account_name='portalvhdspbrd34f2fnbl',
account_key='y48JkXg+VcHQRCgsylJf4xV4Fd0AuJNkQKSwGhAR+BppHnFhkI+UHPOS/oYaTo0rqFCGQkEBW+thNFZNB9W8yg==')
def insert(self,ID,CustomerID,ProductID,Datetime,TotalPrice):
task = {'PartitionKey': 'Order',
'RowKey': ID,
'CustomerID' : CustomerID,
'ProductID' : ProductID,
'Datetime' : Datetime,
'TotalPrice' : TotalPrice}
try:
self.table_service.insert_entity('Order', task)
except:
print"azure.WindowsAzureConflictError: Order Conflict"
def listAll(self):
task1 = self.table_service.query_entities('Order', None, None, 1000)
task2 = self.table_service.query_entities('Order', None, None, 1000,
task1.x_ms_continuation['NextPartitionKey'],
task1.x_ms_continuation['NextRowKey'])
task3 = self.table_service.query_entities('Order', None, None, 1000,
task2.x_ms_continuation['NextPartitionKey'],
task2.x_ms_continuation['NextRowKey'])
task4 = self.table_service.query_entities('Order', None, None, 1000,
task3.x_ms_continuation['NextPartitionKey'],
task3.x_ms_continuation['NextRowKey'])
'''
task5 = self.table_service.query_entities('Order', None, None, 1000,
task4.x_ms_continuation['NextPartitionKey'],
task4.x_ms_continuation['NextRowKey'])
task6 = self.table_service.query_entities('Order', None, None, 1000,
task5.x_ms_continuation['NextPartitionKey'],
task5.x_ms_continuation['NextRowKey'])
task7 = self.table_service.query_entities('Order', None, None, 1000,
task6.x_ms_continuation['NextPartitionKey'],
task6.x_ms_continuation['NextRowKey'])
task8 = self.table_service.query_entities('Order', None, None, 1000,
task7.x_ms_continuation['NextPartitionKey'],
task7.x_ms_continuation['NextRowKey'])
task9 = self.table_service.query_entities('Order', None, None, 1000,
task8.x_ms_continuation['NextPartitionKey'],
task8.x_ms_continuation['NextRowKey'])
task10 = self.table_service.query_entities('Order', None, None, 1000,
task9.x_ms_continuation['NextPartitionKey'],
task9.x_ms_continuation['NextRowKey'])
'''
i=0
for task in task1:
i=i+1
'''
print("ID: %s, CustomerID: %s, ProductID: %s, Datetime: %s, TotalPrice: %s") %(task.RowKey,
task.CustomerID,task.ProductID,task.Datetime,task.TotalPrice)
'''
print(task.TotalPrice)
for task in task2:
i=i+1
print(task.TotalPrice)
print("Total Order: %s") %(i)
def TableInfo(self):
info=self.table_service.query_tables()
for i in info:
print(i.name)
示例8: TableServiceTest
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
#.........這裏部分代碼省略.........
# Act
resp = self.tc.get_entity(self.table_name, 'MyPartition', '1')
# Assert
self.assertEquals(resp.PartitionKey, 'MyPartition')
self.assertEquals(resp.RowKey, '1')
self._assert_default_entity(resp)
def test_get_entity_not_existing(self):
# Arrange
self._create_table(self.table_name)
# Act
with self.assertRaises(WindowsAzureError):
self.tc.get_entity(self.table_name, 'MyPartition', '1')
# Assert
def test_get_entity_with_select(self):
# Arrange
self._create_table_with_default_entities(self.table_name, 1)
# Act
resp = self.tc.get_entity(self.table_name, 'MyPartition', '1', 'age,sex')
# Assert
self.assertEquals(resp.age, 39)
self.assertEquals(resp.sex, 'male')
self.assertFalse(hasattr(resp, "birthday"))
self.assertFalse(hasattr(resp, "married"))
self.assertFalse(hasattr(resp, "deceased"))
def test_query_entities(self):
# Arrange
self._create_table_with_default_entities(self.table_name, 2)
# Act
resp = self.tc.query_entities(self.table_name)
# Assert
self.assertEquals(len(resp), 2)
for entity in resp:
self.assertEquals(entity.PartitionKey, 'MyPartition')
self._assert_default_entity(entity)
self.assertEquals(resp[0].RowKey, '1')
self.assertEquals(resp[1].RowKey, '2')
def test_query_entities_with_filter(self):
# Arrange
self._create_table_with_default_entities(self.table_name, 2)
self.tc.insert_entity(self.table_name, self._create_default_entity_dict('MyOtherPartition', '3'))
# Act
resp = self.tc.query_entities(self.table_name, "PartitionKey eq 'MyPartition'")
# Assert
self.assertEquals(len(resp), 2)
for entity in resp:
self.assertEquals(entity.PartitionKey, 'MyPartition')
self._assert_default_entity(entity)
def test_query_entities_with_select(self):
# Arrange
self._create_table_with_default_entities(self.table_name, 2)
示例9: Repository
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
class Repository(object):
"""Azure Table Storage repository."""
def __init__(self, settings):
"""Initializes the repository with the specified settings dict.
Required settings are:
- STORAGE_NAME
- STORAGE_KEY
- STORAGE_TABLE_POLL
- STORAGE_TABLE_CHOICE
"""
self.name = 'Azure Table Storage'
self.storage_name = settings['STORAGE_NAME']
self.storage_key = settings['STORAGE_KEY']
self.poll_table = settings['STORAGE_TABLE_POLL']
self.choice_table = settings['STORAGE_TABLE_CHOICE']
self.svc = TableService(self.storage_name, self.storage_key)
self.svc.create_table(self.poll_table)
self.svc.create_table(self.choice_table)
def get_polls(self):
"""Returns all the polls from the repository."""
poll_entities = self.svc.query_entities(self.poll_table)
polls = [_poll_from_entity(entity) for entity in poll_entities]
return polls
def get_poll(self, poll_key):
"""Returns a poll from the repository."""
try:
partition, row = _key_to_partition_and_row(poll_key)
poll_entity = self.svc.get_entity(self.poll_table, partition, row)
choice_entities = self.svc.query_entities(
self.choice_table,
"PollPartitionKey eq '{0}' and PollRowKey eq '{1}'" \
.format(partition, row)
)
poll = _poll_from_entity(poll_entity)
poll.choices = [_choice_from_entity(choice_entity)
for choice_entity in choice_entities]
return poll
except WindowsAzureMissingResourceError:
raise PollNotFound()
def increment_vote(self, poll_key, choice_key):
"""Increment the choice vote count for the specified poll."""
try:
partition, row = _key_to_partition_and_row(choice_key)
entity = self.svc.get_entity(self.choice_table, partition, row)
entity.Votes += 1
self.svc.update_entity(self.choice_table, partition, row, entity)
except WindowsAzureMissingResourceError:
raise PollNotFound()
def add_sample_polls(self):
"""Adds a set of polls from data stored in a samples.json file."""
poll_partition = '2014'
poll_row = 0
choice_partition = '2014'
choice_row = 0
for sample_poll in _load_samples_json():
poll_entity = {
'PartitionKey': poll_partition,
'RowKey': str(poll_row),
'Text': sample_poll['text'],
}
self.svc.insert_entity(self.poll_table, poll_entity)
for sample_choice in sample_poll['choices']:
choice_entity = {
'PartitionKey': choice_partition,
'RowKey': str(choice_row),
'Text': sample_choice,
'Votes': 0,
'PollPartitionKey': poll_partition,
'PollRowKey': str(poll_row),
}
self.svc.insert_entity(self.choice_table, choice_entity)
choice_row += 1
poll_row += 1
示例10: __init__
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
class AzureDataServices:
_partition = 'presence'
def __init__(self, table):
if os.environ.get("raspberry") is None:
table += 'Test'
print table
self._partition = table
with open('azure.txt') as f:
lines = f.readlines()
acc = lines[0].strip()
key = lines[1].strip()
self._table_service = TableService(account_name=acc, account_key=key)
def create_table(self):
"""
Creates azure storage table
"""
self._table_service.create_table(self._partition)
def insert_data(self, task):
"""
Insert the object to azure
"""
self.insert_data_with_key(task, None)
def insert_data_with_key(self, task, row_key):
"""
Insert the object to azure
"""
task.PartitionKey = self._partition
if row_key:
t = row_key
else:
t = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
task.RowKey = t
self._table_service.insert_entity(self._partition, task)
def update_or_insert(self, task, row_key):
"""
Update or insert the object to azure
"""
task.PartitionKey = self._partition
task.RowKey = row_key
try:
self._table_service.update_entity(self._partition, self._partition,row_key, task)
except azure.WindowsAzureMissingResourceError:
self._table_service.insert_entity(self._partition, task)
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 get_presence(self):
tasks = self._table_service.query_entities(self._partition, "PartitionKey eq 'presence'")
return tasks
示例11: __init__
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
class Iterator:
def __init__(self,table):
self.DataCollector = []
self.i=0
self.Table=table
self.table_service = TableService(account_name='portalvhdspbrd34f2fnbl',
account_key='y48JkXg+VcHQRCgsylJf4xV4Fd0AuJNkQKSwGhAR+BppHnFhkI+UHPOS/oYaTo0rqFCGQkEBW+thNFZNB9W8yg==')
self.task = self.table_service.query_entities(self.Table, None, None, 100)
if table=="Customer":
self.i=self.i+100
print(self.i)
for line in self.task:
self.DataCollector.append({'ID': line.RowKey,
'Name' : line.Name ,
'Country' : line.Country,
'City' : line.City})
self.iteratorCustomer(self.task)
if table=="Order":
self.i=self.i+100
print(self.i)
for line in self.task:
self.DataCollector.append({'ID': line.RowKey,
'CustomerID' : line.CustomerID,
'ProductID' : line.ProductID,
'Datetime' : line.Datetime,
'TotalPrice' : line.TotalPrice})
self.iteratorOrder(self.task)
def iteratorCustomer(self,task):
try:
data = self.table_service.query_entities(self.Table, None, None, 100,
task.x_ms_continuation['NextPartitionKey'],
task.x_ms_continuation['NextRowKey'])
self.i=self.i+len(data)
print(self.i)
for line in data:
self.DataCollector.append({'ID': line.RowKey,
'Name' : line.Name ,
'Country' : line.Country,
'City' : line.City})
self.iteratorCustomer(data)
except:
pass
def iteratorOrder(self,task):
try:
data = self.table_service.query_entities(self.Table, None, None, 100,
task.x_ms_continuation['NextPartitionKey'],
task.x_ms_continuation['NextRowKey'])
self.i=self.i+len(data)
print(self.i)
for line in data:
self.DataCollector.append({'ID': line.RowKey,
'CustomerID' : line.CustomerID,
'ProductID' : line.ProductID,
'Datetime' : line.Datetime,
'TotalPrice' : line.TotalPrice})
self.iteratorOrder(data)
except:
pass
def getDatas(self):
return self.DataCollector
示例12: AzureGaugeDatastore
# 需要導入模塊: from azure.storage import TableService [as 別名]
# 或者: from azure.storage.TableService import query_entities [as 別名]
class AzureGaugeDatastore(GaugeDatastore):
"""Stores gauge data in Azure Table storage. Utilizes table storage
as follows:
PartitionKey: gauge name
RowKey: date key (e.g. day string)
data: stored data for date key
Timestamp: last time data updated
Replaces existing rows (PK+RK) with upsert queries. Has a limitation of
returning max 1000 rows in a query. This suffices for now.
"""
table_service = None
table_name = None
def __init__(self, account_name, account_key, table_name):
self.table_name = table_name
self.table_service = TableService(account_name, account_key)
def save_data(self, gauge_name, date_key, data):
"""Saves data to the table row of date_key (replaces if exists)
"""
entity = {'PartitionKey': gauge_name, 'RowKey': date_key, 'data': data}
self.table_service.insert_or_replace_entity(self.table_name,
gauge_name, date_key,
entity)
def get_gauge_data(self, gauge_name, min_date_key=None, max_date_key=None):
"""Retrieves all gauge data, returns unsorted.
If min_date_key is specified, returns records with
date_key >= min_date_key
If max_date_key is specified, returns records with
date_key < max_date_key
IMPORTANT NOTE: Azure Table REST API returns first (oldest) 1000 rows
in API call. Unless we add RowKey>min_date_key filter, after 1000
rows (e.g. after 1000 days of recording) no fresh results will be
returned.
"""
query = "PartitionKey eq '{0}'".format(gauge_name)
if min_date_key:
query = "{0} and RowKey ge '{1}'".format(query, min_date_key)
if max_date_key:
query = "{0} and RowKey lt '{1}'".format(query, max_date_key)
rows = self.table_service.query_entities(self.table_name, filter=query)
if rows:
return [make_record(record.RowKey, record.data) for record in rows]
def get_data(self, gauge_name, date_key):
"""Retrieves gauge data for a specific date key (e.g. day)
"""
rec = self.table_service.get_entity(self.table_name, gauge_name,
date_key)
if not rec:
return None
return helpers.make_record(rec.RowKey, rec.data)