RSS

Author Archives: Amol Matkar

About Amol Matkar

Working in Joomla and Wordpress.

How to fetch data from AWS Athena using PHP

// https://aws.amazon.com/documentation/sdk-for-php/

 

<?php
require_once ‘aws/aws-autoloader.php’;

use Aws\Athena\AthenaClient;

class CLSAthena {
public $Client = ”;

function createAthenaObject($version, $region, $key, $secret){
try {

$this->Client = AthenaClient::factory(array(
‘version’ => $version,
‘region’ => $region,
‘credentials’ => array(
‘key’ => $key,
‘secret’ => $secret,
)
));

}
catch (AthenaException $e) {
// Catch an S3 specific exception.
echo $e->getMessage();
} catch (AwsException $e) {
// This catches the more generic AwsException. You can grab information
// from the exception using methods of the exception object.
echo $e->getAwsRequestId() . “\n”;
echo $e->getAwsErrorType() . “\n”;
echo $e->getAwsErrorCode() . “\n”;
}

}

function getData($db_name, $table_name, $query = ”, $result_logs)
{
try {

$result1 = $this->Client->StartQueryExecution(
array(
“QueryExecutionContext”=> array( “Database”=> $db_name ),
“QueryString”=> $query,
“ResultConfiguration”=> array(
“EncryptionConfiguration”=> array(“EncryptionOption”=> “SSE_S3”),
“OutputLocation”=> “s3://”.$result_logs
)
)
);

$QueryExecutionId = $result1->get(‘QueryExecutionId’);

$this->waitForQueryToComplete($QueryExecutionId);

$result1 = $this->Client->GetQueryResults(array(
‘QueryExecutionId’ => $QueryExecutionId, // REQUIRED
‘MaxResults’ => 500
));

$data = $result1->get(‘ResultSet’);
$res = $data[‘Rows’];

while (true) {

if($result1->get(‘NextToken’) == null)
{
break;
}

$result1 = $this->Client->GetQueryResults(array(
‘QueryExecutionId’ => $QueryExecutionId, // REQUIRED
‘NextToken’ => $result1->get(‘NextToken’),
‘MaxResults’ => 500
));

$data = $result1->get(‘ResultSet’);
$res = array_merge($res, $data[‘Rows’]);;
}

$resData = $this->processResultRows($res);
return $resData;
}
catch (AthenaException $e) {
// Catch an S3 specific exception.
echo $e->getMessage();
} catch (AwsException $e) {
// This catches the more generic AwsException. You can grab information
// from the exception using methods of the exception object.
echo $e->getAwsRequestId() . “\n”;
echo $e->getAwsErrorType() . “\n”;
echo $e->getAwsErrorCode() . “\n”;
}
}

/*
* function to wait for fetch result from query
*/
function waitForQueryToComplete($QueryExecutionId)
{
while(1)
{
$result = $this->Client->getQueryExecution(array(‘QueryExecutionId’ => $QueryExecutionId));
$res = $result->toArray();

//echo $res[‘QueryExecution’][‘Status’][‘State’].'<br/>’;
if($res[‘QueryExecution’][‘Status’][‘State’]==’FAILED’)
{
echo ‘Query Failed’;
die;
}
else if($res[‘QueryExecution’][‘Status’][‘State’]==’CANCELED’)
{
echo ‘Query was cancelled’;
die;
}
else if($res[‘QueryExecution’][‘Status’][‘State’]==’SUCCEEDED’)
{
break; // break while loop
}

}
}

/*
* function to process data
*/
function processResultRows($res)
{
$result = array();
$resul_array = array();

// echo ‘@@@Count: ‘.count($res).'<br/>’;

for($i=0; $i <= count($res); $i++)
{
for($n=0; $n < count($res[$i][‘Data’]); $n++)
{
if($i==0)
$result[] = $res[$i][‘Data’][$n][‘VarCharValue’];
else
{
$resul_array[$i][$result[$n]] = $res[$i][‘Data’][$n][‘VarCharValue’];
}
}
}

// echo ‘resul_array_cnt: ‘.count($resul_array).'<br/>’;
return $resul_array;
}
}

Advertisements
 
Leave a comment

Posted by on July 20, 2017 in Athena, AWS

 

Tags: , , ,

How to check mod_rewrite ingine is installed or not

using phpinfo(); –> find mod_rewrite

In order to use mod_rewrite you can type the following command in the terminal:

a2enmod rewrite

service apache2 restart

 
Leave a comment

Posted by on December 14, 2015 in Apache

 

Sort array on multiple columns

<?php

$sorted = array_orderby($data, ‘col1’, SORT_ASC, ‘col2’, SORT_ASC);

function array_orderby() {
$args = func_get_args();
$data = array_shift( $args );

if ( ! is_array( $data ) ) {
return array();
}

$multisort_params = array();
foreach ( $args as $n => $field ) {
if ( is_string( $field ) ) {
$tmp = array();
foreach ( $data as $row ) {
$tmp[] = $row[ $field ];
}
$args[ $n ] = $tmp;
}
$multisort_params[] = &$args[ $n ];
}

$multisort_params[] = &$data;

call_user_func_array( ‘array_multisort’, $multisort_params );
return end( $multisort_params );
}

?>

 
Leave a comment

Posted by on October 11, 2014 in Core PHP

 

Tags: ,

How to refresh a parent window when Joomla modal popup box is closed?

In your anchor tag please add below code:

rel=”{size: {x: popupwidth, y: popupheight}, handler:’iframe’,onClose:function(){var js =window.location.reload();}}”

 
Leave a comment

Posted by on September 19, 2014 in Joomla

 

How to get all selected checkboxes VALUES in jquery

var checkedValues = $(‘input:checkbox:checked’).map(function() {
return this.value;
}).get();

 
Leave a comment

Posted by on September 12, 2014 in jQuery

 

How to Select / Deselect All Checkboxes using jQuery

jQuery('#checkAll').click(function(){    
    jQuery(':checkbox.checkItem').prop('checked',this.checked);
});
 
Leave a comment

Posted by on June 20, 2014 in jQuery

 

php text to speech with google api

<?php
// Convert Words (text) to Speech (MP3)
// ————————————
$word= “Your text here”;
// Google Translate API cannot handle strings > 100 characters
$word = substr($word, 0, 100);

// Replace the non-alphanumeric characters
// The spaces in the sentence are replaced with the Plus symbol
$word = urlencode($word);

// If the MP3 file exists, do not create a new request
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, ‘http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=&#8217;.$word);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the mp3 file
$mp3 = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

file_put_contents(‘temp.mp3’, $mp3);
?>

 
Leave a comment

Posted by on October 28, 2013 in Core PHP

 

Tags: ,