我是DynamoDB的新手。
在我的應用程序中,每個註冊用戶在他的賬戶上都有一些傳感器,這些傳感器會每隔5分鐘向我們的數據庫發送一些數據。如何使用DynamoDB從表中獲取最新的時間戳值PHP
現在我想顯示「如果用戶已經登錄到他的帳戶,我應該需要顯示所有他的傳感器與每個傳感器的最新時間戳值」。
我這樣做了三個查詢,例如「我提取了他的賬戶下的所有設備 - >原始傳感器ID - >每個傳感器的最新值」,因此這個過程需要很長時間才能執行。
而且我的表是這樣的:
用戶:
---------------------
|sno | userId |
---------------------
| 1 | naveenkumar |
|--------------------
| 2 | abc |
|--------------------
| 3 | xyz |
|--------------------
sensor_devices:
---------------------
| id | sensorId |
---------------------
| 1 | sensor1 |
|--------------------
| 2 | sensor2 |
|--------------------
| 3 | sensor3 |
|--------------------
| 4 | sensor4 |
---------------------
| 5 | sensor5 |
---------------------
而且
用戶傳感器:
----------------------
| userId | sensorId |
----------------------
| 1 | 1 |
|---------------------
| 1 | 2 |
|---------------------
| 1 | 4 |
|---------------------
| 2 | 5 |
|---------------------
個
data_tbl:
---------------------------------------------
| sensorId | value | timestamp |
---------------------------------------------
| sensor1 | 4.3 | 2014-01-21 11:21:00|
|--------------------------------------------
| sensor2 | 5.0 | 2014-01-21 11:22:00|
|--------------------------------------------
| sensor3 | 10.0 | 2014-01-21 11:19:00|
|--------------------------------------------
| sensor4 | 6.3 | 2014-01-21 11:25:00|
|--------------------------------------------
| sensor1 | 8.3 | 2014-01-21 11:26:00|
|--------------------------------------------
| sensor2 | 6.0 | 2014-01-21 11:27:00|
|--------------------------------------------
| sensor3 | 9.0 | 2014-01-21 11:24:00|
|--------------------------------------------
| sensor4 | 6.3 | 2014-01-21 11:30:00|
|--------------------------------------------
和最終產出表,會看到在他的主頁的用戶:
---------------------------------------------
| sensorId | value | timestamp |
---------------------------------------------
| sensor1 | 8.3 | 2014-01-21 11:26:00|
|--------------------------------------------
| sensor2 | 6.0 | 2014-01-21 11:27:00|
|--------------------------------------------
| sensor4 | 6.3 | 2014-01-21 11:30:00|
|--------------------------------------------
而且我的查詢看起來像這樣與它採取長的時間來執行,所以請給我提供更好的解決方案:
讓我們假設$ _SESSION ['login_id'] = 1;
<?php
$aws = Aws::factory('config.php');
$client = $aws->get("dynamodb");
$tableName = "data_tbl";
echo "<table border=1>";
echo "<tr>";
echo "<th>Sensor ID</th>";
echo "<th>value</th>";
echo "<th>Timestamp</th>";
echo "</tr>";
//Query to get user devices
$devices = $client->query(array(
"TableName" => "users-sensors",
"KeyConditions" => array(
"userId" => array(
"ComparisonOperator" => ComparisonOperator::EQ,
"AttributeValueList" => array(
array(Type::STRING => $_SESSION['login_id'])
)
)
)
));
foreach ($devices['Items'] AS $key=>$value) {
$id = $value['sensorId']['S'];
//Query to get original sensorId from sensor serial number
$device = $client->query(array(
"TableName" => "sensor_devices",
"KeyConditions" => array(
"id" => array(
"ComparisonOperator" => ComparisonOperator::EQ,
"AttributeValueList" => array(
array(Type::STRING => $id)
)
)
)
));
$dids = $device['Items'][0]['sensorId'][Type::STRING];
//Query to get all latest values of sensors
$response = $client->query(array(
"TableName" => $tableName,
"KeyConditions" => array(
"sensorId" => array(
"ComparisonOperator" => ComparisonOperator::EQ,
"AttributeValueList" => array(
array(Type::STRING => $dids)
)
)
),
"Select" => "SPECIFIC_ATTRIBUTES",
"AttributesToGet" => array("sensorId","value","timestamp"),
"ScanIndexForward" => false,
"Limit" => 1
));
foreach ($response['Items'] AS $key=>$value) {
echo "<tr>";
$link = "graph.php?id=".$id;
echo "<td><a href='$link'>".$value['sensorId']['S']."</a></td>";
echo "<td><a href='$link'>".$value['value']['S']."</a></td>";
$epoch = $value['timestamp']['S'];
$timestamp = date('Y-m-d H:i:s', $epoch);
echo "<td><a href='$link'>".$timestamp."</a></td>";
echo "</tr>";
}
}
echo "</table>";
?>
如果我能得到答案,我會完成我的工作。 謝謝