In: Computer Science
"How can I connect my hadoop database or mysql database server to my d3 visual?"
connecting MYSQL database server to d3 visual:
First way:
Note:d3 requires json file as input
The following is a php script that you should be able to save somewhere as a file (let's say you call it 'getdata.php') accessible from your HTML file with your D3 code in it. When called it will return data from your MySQL database in a json format (so long as the database server isn't outside your domain);
<?php $username = "******"; $password = "******"; $host = "******"; $database="***dbase_name***"; $server = mysql_connect($host, $user, $password); $connection = mysql_select_db($database, $server); $myquery = " query here "; $query = mysql_query($myquery);
if ( ! $myquery ) { echo mysql_error(); die; } $data = array(); for ($x = 0; $x < mysql_num_rows($query); $x++) { $data[] = mysql_fetch_assoc($query); } echo json_encode($data); mysql_close($server); ?>
Obviously you would need to enter appropriate details for username, password, host and database. You would also need to include an appropriate query for your data so that it returned data for 'dateTimeTaken' and 'reading'. Something along the lines of (and this is only a guess);
SELECT `dateTimeTaken`, `reading` FROM `tablename`
Then when you go to read in your json file you would use the following syntax for the code where you would be reading in your json;
d3.json("getdata.php", function(error, data) {
Hopefully that's close to what you're looking for. I've tested it locally and it all seems to work..
And another way is:
Using Python, connect with database and convert the data in json format. Save this in some .json file.
In d3, read this json file as input and use it in visualization.