It may be simple question ,i am fresh to php,amazon
I want to do batch put to my SDB database. I download s php-sdb sdk from amazon and did it successfully.
$bPut = $sdb->batch_put_attributes($domineName, array(
'ItemOne' => array(
'Company' => 'a',
),
'ItemTwo' => array(
'Company' => 'a',
),
'ItemThree' => array(
'Company' => 'a',
)
), true, null);
its works fine.Now I want to make the array (the second parameter) dynamic ie I want to make itemOne,ItemTwo,ItemThree ... from SDB database , I did it using $key = implode($row['test']);
But I am fail to create a multidimensional array successfully>how can I do it.Please help
This:
$items['ItemOne']['Company'] = 'a';
$items['ItemTwo']['Company'] = 'a';
$items['ItemThree']['Company'] = 'a';
will give you this:
array(
'ItemOne' => array(
'Company' => 'a'
),
'ItemTwo' => array(
'Company' => 'a'
),
'ItemThree' => array(
'Company' => 'a'
)
);
And then you can use it like so:
$bPut = $sdb->batch_put_attributes($domineName, $items), true, null);
You can view and manage your uploaded data using SDB Explorer. In new upcoming version SDB Explorer will support bulk upload. You will be able to upload your large data in parallel threads.
http://www.sdbexplorer.com/
Related
is there a away to translate values in php using either google api translate or any other api...
<?php
// 1.- Query to get information
// 2.- build array with that query
// Example array from query
$data = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa'
),
);
// Now that the array has been build, lets make a translation
// Which I have no idea how to do that but the final array should be
$final = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde',
'name_en' => 'Shoes',
'color_en' => 'Green'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa',
'name_en' => 'House',
'color_en' => 'Pink'
),
);
is this process possible or am I just dreaming?
I have very little knowledge on how exactly Goolge API works since I only use the Google Translate widget and the translation is after you present the information but in this case we need to make a translation before presenting the information...
Google translate API is a paid service. You need to get a api key from google api services :
google translate API
After that, you can make a curl to google api after getting your results from query :
sample url for curl :
https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff
You will get the results as JSON object,do json_decode and add results to your array.
All,
I am attempting to migrate roughly 6GB of Mongo data that is comprised of hundreds of collections to DynamoDB. I have written some scripts using the AWS PHP SDK and am able to port over very small collections but when I try ones that have more than 20k documents (still a very small collection all things considered) it either takes an outrageous amount of time or quietly fails.
Does anyone have some tips/tricks for taking data from Mongo (or any other NoSQL DB) and migrating it to Dynamo, or any other NoSQL DB. I feel like this should be relatively easy because the documents are extremely flat/simple.
Any thoughts/suggestions would be much appreciated!
Thanks!
header.php
<?
require './aws-autoloader.php';
require './MongoGet.php';
set_time_limit(0);
use \Aws\DynamoDb\DynamoDbClient;
$client = \Aws\DynamoDb\DynamoDbClient::factory(array(
'key' => 'MY_KEY',
'secret' => 'MY_SECRET',
'region' => 'MY_REGION',
'base_url' => 'http://localhost:8000'
));
$collection = "AccumulatorGasPressure4093_raw";
function nEcho($str) {
echo "{$str}<br>\n";
}
echo "<pre>";
test-store.php
<?
include('test-header.php');
nEcho("Creating table(s)...");
// create test table
$client->createTable(array(
'TableName' => $collection,
'AttributeDefinitions' => array(
array(
'AttributeName' => 'id',
'AttributeType' => 'N'
),
array(
'AttributeName' => 'count',
'AttributeType' => 'N'
)
),
'KeySchema' => array(
array(
'AttributeName' => 'id',
'KeyType' => 'HASH'
),
array(
'AttributeName' => 'count',
'KeyType' => 'RANGED'
)
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 20
)
));
$result = $client->describeTable(array(
'TableName' => $collection
));
nEcho("Done creating table...");
nEcho("Getting data from Mongo...");
// instantiate class and get data
$mGet = new MongoGet();
$results = $mGet->getData($collection);
nEcho ("Done retrieving Mongo data...");
nEcho ("Inserting data...");
$i = 0;
foreach($results as $result) {
$insertResult = $client->putItem(array(
'TableName' => $collection,
'Item' => $client->formatAttributes(array(
'id' => $i,
'date' => $result['date'],
'value' => $result['value'],
'count' => $i
)),
'ReturnConsumedCapacity' => 'TOTAL'
));
$i++;
}
nEcho("Done Inserting, script ending...");
I suspect that you are being throttled by DynamoDB, especially if your tables' throughputs are low. The SDK retries the requests, up to 11 times per request, but eventually, the requests fail, which should throw an exception.
You should take a look at the WriteRequestBatch object. This object is basically a queue of items that get sent in batches, but any items that fail to transfer are re-queued automatically. Should provide a more robust solution for what you are doing.
I had asked a question here a while back about setting up database populated dropdowns for SugarCRM. I received a really good answer and, after more php studies and a dev instance running, I decided to give it a shot. The instructions I followed can be found here. After I run the repair and rebuild, I would expect to see the custom field in my Fields list under the module in studio, but have not been able to find it. The module is named Makers (a1_makers as a database table). For good orders sake, there were no errors when I repaired/rebuilt after saving the files. Per the instructions, I first created a php file with a custom function to query the database (custom/Extension/application/Ext/Utils/getMakers.php):
<?php
function getMakers() {
static $makers = null;
if (!$makers){
global $db;
$query = "SELECT id, name FROM a1_maker";
$result = $db->query($query, false);
$accounts = array();
$accounts[''] = '';
while (($row = $db->fetchByAssoc($result)) !=null) {
$accounts[$row['id']] = $row['name'];
}
}
return $makers;
}
?>
Then, I set 'function' field in Vardefs to point to the function (custom/Extension/modules/Maker/Ext/Vardefs/makers_template.php):
<?php
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'len' => '100',
'comment' => 'List of makers populated from the database',
);
?>
Unfortunately, there are no errors and the repair/rebuild runs fine. I am just unable to see the custom field when I go into studio. Can anyone please help point out what I may be doing wrong?
I would recommend checking existence of newly created field 'list_of_makers' in cache/modules/Maker/Makervardefs.php file. If new field definition exists in that file, try add 'studio' => 'visible' to custom/Extension/modules/Maker/Ext/Vardefs/makers_template.php to get something like this:
<?php
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'studio' => 'visible'
'len' => '100',
'comment' => 'List of makers populated from the database',
);
Try to edit your custom/modules/Maker/metadata/editviewdefs.php manually and insert field definition by hand in proper place if everything above won't work.
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'studio' => 'visible'
'len' => '100',
'comment' => 'List of makers populated from the database',
'studio' => array(
'listview' => true,
'detailview' => true,
'editview' => true
),
);
I have a CakePHP model, let's call it Thing which has an associated model called ItemView. ItemView represents one page view of the Thing item. I want to display how many times Thing has been viewed, so I do the following in my view:
<?php echo count($thing['ItemView']); ?>
This works, however as time goes on the result set of this query is going to get huge, as it's currently being returned like so:
array(
'Thing' => array(
'id' => '1',
'thing' => 'something'
),
'ItemView' => array(
(int) 0 => array(
'id' => '1',
'thing_id' => 1,
'created' => '2013-09-21 19:25:39',
'ip_address' => '127.0.0.1'
),
(int) 1 => array(
'id' => '1',
'thing_id' => 1,
'created' => '2013-09-21 19:25:41',
'ip_address' => '127.0.0.1'
),
// etc...
)
)
How can I adapt the model find() to retrieve something like so:
array(
'Thing' => array(
'id' => '1',
'thing' => 'something',
'views' => 2
)
)
without loading the entire ItemView relation into memory?
Thanks!
So it's pretty straight forward, we can make use of countercache - Cake does the counting for you whenever a record is added into/deleted fromItemView:
Nothing to change in your Thing.php model
Add a new INT column views in your things table.
In your ItemView.php model, add counterCache like this:
public $belongsTo = array(
'Thing' => array(
'counterCache' => 'views'
)
);
Then next time when you do addition/deletion via ItemView, Cake will automatically recalculate the counting and cache into views for you, so the next time when you do the query, you also need to make sure you specify recursive = -1 as what #Paco Car has suggested in his answer:
$this->Thing->recursive = -1;
$this->Thing->find(...); //this will returns array of Thing + the field "views"
// --- OR ---
$this->Thing->find(array(
'conditions' => array(
//... your usual conditions here
),
//... fields, order... etc
//this will make sure the recursive applies to this call, once only.
'recursive' => -1
);
I'm trying to build an array and while doing so, I was wondering if it makes any difference in how you create such array. Things I have in mind are performance, maintainability, readability,...
Declaration 1:
$data = array(
'my_array' => array(
'table' => array(
'group' => t('My group'),
'join' => array(
'commerce_product' => array(
'left_field' => 'sku',
'field' => 'artc',
),
),
);
Declaration 2:
$data['my_array']['table']['group'] = 'My group';
$data['my_array']['table']['join']['commerce_product'] = array(
'left_field' => 'sku',
'field' => 'artc',);
Because for some reason, my Drupal site accepts the first declaration but not the second. Since it's about creating arrays in PHP I don't think it has something to do with Drupal, rather with the way the array is created...
The feature of automatically creating an array with the second syntax (assigning a value to a key in a non-existent variable) is a more recent feature than using the array syntax.
Check the PHP version installed on your two servers.