如何使用Java API从Azure WADPerformanceCounters表中获取特定时间间隔的记录?
尝试了以下代码,但是它给出了表中的所有记录。似乎基于时间戳的过滤器无法正常工作。尝试使用PartitionKey,Timestamp,EventTick和TIMESTAMP列过滤但对所有人都一样。
public static void main(String arg[]){
try
{
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudTableClient tableClient = storageAccount.createCloudTableClient();
CloudTable cloudTable = tableClient.getTableReference("WADPerformanceCountersTable");
Long currTime = System.currentTimeMillis();
Date currentDate = new Date(currTime);
Date endTime = getFormattedTimestamp(currentDate);
System.out.println("endTime:" + endTime);
// calculation of start Time to DB format in UTC
long offsetInMilliseconds = 1000 * 60 * 2;
Date startTime = getFormattedTimestamp(new Date(currentDate
.getTime()
- offsetInMilliseconds));
System.out.println("startTime:" + startTime);
long startPartitionKey = 621355968000000000L + startTime
.getTime() * 10000;
long endPartitionKey = 621355968000000000L + endTime.getTime() * 10000;
//Query using PartitionKey
TableQuery< PerfTableEntity > SQL = TableQuery.from(PerfTableEntity.class).where(
"PartitionKey ge '0" + startPartitionKey + "'").where(
"PartitionKey le '0" + endPartitionKey + "'").where(
"DeploymentId eq '<deplymentid>'").where(
"RoleInstance eq 'WebRole1_IN_0'").where(
"CounterName eq '\\Memory\\Page Faults/sec' or CounterName eq '\\Memory\\Page Reads/sec'");
for (PerfTableEntity pd : cloudTable.execute(SQL)) {
System.out.println("\ncounterName = " +pd.getCounterName() + "= " + pd.getCounterValue() + "||" + pd.getTimestamp());
}
}catch (Exception e){
// Output the stack trace.
e.printStackTrace();
}
}//main
private static Date getFormattedTimestamp(Date date) {
try {
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String datestr = df.format(date);
return df.parse(datestr);
} catch (Exception e) {
return null;
}
}
答案 0 :(得分:1)
使用stringBuilder将0附加到PartitionKey解决了问题。