Looping off of MySQL result and building custom array - php

The following code is building the proper structure of array that I want, but it's only doing it for one result even though the MYSQL result is about 65 items.
I need the 'type' and 'features' levels to be static, and then start the loop within the features level (so every mysql result would build an item in within the features level)
The following is only loading the last result from the query:
$db->setQuery($query);
$item=$db->loadObjectList();
echo $db->getErrorMsg();
//dump($item);
$stores = [
'type' => 'FeatureCollection',
'features' => [
foreach ($item as $i) {
[
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [
$i->lat,
$i->lng
]
],
'properties' => [
'storeImage' => $i->logo, // you need to convert this into an actual URL
'storeName' => $i->name,
'phoneFormatted' => $i->phone, // this needs formatting aswell
'address' => $i->address,
'city' => $i->city,
'country' => $i->country,
'postalCode' => $i->zip,
'state' => $i->state
]
]
}
]
];
How can I properly loop the mysql query to build my array in the correct way?

Use array_map() to generate a new array from another array.
$item=$db->loadObjectList();
$stores = [
'type' => 'FeatureCollection',
'features' => array_map(function($i) {
return [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [
$i->lat,
$i->lng
]
],
'properties' => [
'storeImage' => $i->logo, // you need to convert this into an actual URL
'storeName' => $i->name,
'phoneFormatted' => $i->phone, // this needs formatting aswell
'address' => $i->address,
'city' => $i->city,
'country' => $i->country,
'postalCode' => $i->zip,
'state' => $i->state
]
];
}, $item)
];

Related

Writing record with MeasureValues error: Please use measureValue to send the data

I am writing to Timestream a list of dimensions and a list of measureValues but I keep getting this error:
"Message":"measureValues (list) not supported for BOOL, DOUBLE, VARCHAR and BIGINT data types. Please use measureValue to send the data."
Here is my code:
$dimensions= [];
$dimensions[] = [
'Dimensions' => [
[
'DimensionValueType' => 'VARCHAR',
'Name' => 'id',
'Value' => '123456',
],
],
'MeasureValues' => [
[
'Name' => 'remark',
'Type' => 'VARCHAR',
'Value' => 'Some test text',
],
]
];
$query = [
'CommonAttributes' => [
'MeasureName' => 'table_cnt',
'MeasureValue' => 'table_cnt',
'MeasureValueType' => 'VARCHAR',
'Time' => '1651501311000',
'TimeUnit' => 'MILLISECONDS',
'Version' => 1,
],
'DatabaseName' => 'mydb',
'Records' => $dimensions,
'TableName' => 'table',
];
$db->WriteRecords($query);
According to AWS documentation here (Parameter Syntax) it clearly shows that the supported data types are "DOUBLE|BIGINT|VARCHAR|BOOLEAN|TIMESTAMP|MULTI". If you check a bit down below under "MeasureValues" bulletpoint, it says the opposite: "This is only allowed for type MULTI." . Eitherway, I did try to change the type to MULTI but it still throws the same error.
As you're expecting to use the format
[
'Name' => 'remark',
'Type' => 'VARCHAR',
'Value' => 'Some test text',
],
as input to measures, you need to declare the MeasureValueType as 'MULTI' and not send a MeasureValue on the body of the request. In your example, the final request would be like this:
$dimensions= [];
$dimensions[] = [
'Dimensions' => [
[
'DimensionValueType' => 'VARCHAR',
'Name' => 'id',
'Value' => '123456',
],
],
'MeasureValues' => [
[
'Name' => 'remark',
'Type' => 'VARCHAR',
'Value' => 'Some test text',
],
]
];
$query = [
'CommonAttributes' => [
'MeasureName' => 'table_cnt',
// Removed MeasureValue from here as it's MULTI
'MeasureValueType' => 'MULTI', // <- changed from 'VARCHAR' to 'MULTI'
'Time' => '1651501311000',
'TimeUnit' => 'MILLISECONDS',
'Version' => 1,
],
'DatabaseName' => 'mydb',
'Records' => $dimensions,
'TableName' => 'table',
];
$db->WriteRecords($query);
As a reference, I would suggest you to try it first without the CommonAttributes, as you're inserting only one record, like I demonstrate on this article: https://du7.medium.com/aws-timestream-multi-measures-71b41c089af4

Invalid argument supplied for foreach() when i try to print value

This is my array
[
'field_test1' => [
'field_test2' => [ 'value' => 'Yes' ,'action' => 'visible']
],
'field_test3' => [
'field_test4' => [ 'value' => '2' ,'action' => 'visible']
]
'body' => [
'field_test2' => [ 'value' => 'No', 'action' => 'visible']
'field_test4' => [ 'value' => '1', 'action' => 'visible']
]
]
When i try to loop through each element i am getting error like invalid argument passed for foreach;
My code is
foreach ($myArray as $key => $value) {
echo $key;
}
what should i do??
You forgot the comma after your second array within your array. Before the one with the 'body' key. Try using a decent IDE like PhpStorm, it will highlight the errors in your syntax for you, making the search for common errors easy.
$myArray =
[
'field_test1' => [
'field_test2' => [ 'value' => 'Yes' ,'action' => 'visible']
],
'field_test3' => [
'field_test4' => [ 'value' => '2' ,'action' => 'visible']
],
'body' => [
'field_test2' => [ 'value' => 'No', 'action' => 'visible']
'field_test4' => [ 'value' => '1', 'action' => 'visible']
],
];

batchWriteItem with the same partition key

I have a dynamodb table with a partition key id and a sort key value
Now I want to run batchWriteItem to insert multiple items with the same partition key and a different sort key but it only inserts the last item:
$response = $dynamoDb->batchWriteItem([
'RequestItems' => [
'mytable' => [
[
'PutRequest' => [
'Item' => [
'id' => array('S' => '123abc'),
'value' => array('N' => '1'),
],
'Item' => [
'id' => array('S' => '123abc'),
'value' => array('N' => '2'),
],
'Item' => [
'id' => array('S' => '123abc'),
'value' => array('N' => '3'),
]],
],
],
],
]);
With this code only the item with the value 3 gets inserted
"Items": [
{
"id": {
"S": "123abc"
},
"value": {
"N": "3"
}
}
],
Is there something wrong with the code or something I didn't consider?
It perfectly works when doing single requests:
$response = $dynamoDb->putItem(array(
'TableName' => 'mytable',
'Item' => array(
'id' => array('S' => '123abc'),
'value' => array('N' => '1'),
),
));
$response = $dynamoDb->putItem(array(
'TableName' => 'mytable',
'Item' => array(
'id' => array('S' => '123abc'),
'value' => array('N' => '2'),
),
));
$response = $dynamoDb->putItem(array(
'TableName' => 'mytable',
'Item' => array(
'id' => array('S' => '123abc'),
'value' => array('N' => '3'),
),
));
Is there any performence difference between batchWriteItem or doing 3 single putItem reuests in sequence?
You are using incorrect syntax for batchWrite, try following
$response = $dynamoDb->batchWriteItem([
'RequestItems' => [
'mytable' => [
[
'PutRequest' => [
'Item' => [
'id' => array('S' => '123abc'),
'value' => array('N' => '1'),
]
],
'PutRequest' => [ ///Entire PutRequest array need to be repeated not just item array///
'Item' => [
'id' => array('S' => '123abc'),
'value' => array('N' => '2'),
]
],
],
],
],
]);
Apart from that, the difference between single insert and multiple inserts would be the call to connect with DynamoDB, in batchPut you will send entire array at once whereas in single insert it will connect to DB each time it tries to perform any operation.
In general, you can use Batch to process things faster.
Here is the refernce link for batchWrite syntax
Hope that helps.

Elasticsearch query for empty type give error and not working?

Suppose I have a type 'audit_ssn' and has not any doc, mapping is like:
protected $audit_ssn_mapping_properties = array(
'index' => 'my_index',
'type' => 'audit_ssn',
'body' => [
'audit_ssn' => [
'_source' => [
'type' => 'string',
'analyzer' => 'standard'
],
'properties' => [
'session_id' => array("type" => "string"),
'user_id' => array(
'type' => 'nested',
'properties' => array(
'id' => array('type' => 'string'),
'first_name' => array('type' => 'string'),
'last_name' => array('type' => 'string'),
"unique_user_id" => array("type" => "string")
)
),
'date' => array(
'type' => 'date',
'format' =>'YYYY-MM-dd||YYYY-MM-dd||MM/dd/yyyy||yyyy/MM/dd'
),
'time' => array(
'type' => 'date',
'format' => 'HH:mm:ss'
)
]
]
]
);
Now when i want to run bellow query problem occur.
GET my_index/audit_ssn/_search
{
"query":{
"filtered":{
"filter":{
"and":[
{
"nested":{
"path":"user_id",
"query":{
"terms":{
"user_id.id":["1000001"],
"minimum_match":1
}
}
}
}
]
}
}
}
}
and the error is like:
nested: QueryParsingException[[my_index] [nested] failed to find nested object under path [user_id]]; }{[y0tlRvUzQ8SyyHheSxcd2g][my_index][1]: SearchParseException[[my_index][1]: from[-1],size[-1]: Parse Failure
How to solve this problem ?
Is there any way to check if type is not empty then query should run ?
Or how to modify this query to return empty result ?

How do you define a Cassandra CollectionMap nested in a UDT with duoshuo's PHP client library?

I have a CollectionSet<UDT> where UDT contains a CollectionMap<int,boolean>. I have not been able to find any documentation or example of how to define this when creating a new Cassandra\Type\CollectionSet for inserting into the table. There is a great example with a CollectionList (found here) which is like this:
// CollectionSet<UDT>, where UDT contains: Int, Text, Boolean,
// CollectionList<Text>, CollectionList<UDT>
new Cassandra\Type\CollectionSet([
[
'id' => 1,
'name' => 'string',
'active' => true,
'friends' => ['string1', 'string2', 'string3'],
'drinks' => [['qty' => 5, 'brand' => 'Pepsi'], ['qty' => 3, 'brand' => 'Coke']]
],[
'id' => 2,
'name' => 'string',
'active' => false,
'friends' => ['string4', 'string5', 'string6'],
'drinks' => []
]
], [
[
'type' => Cassandra\Type\Base::UDT,
'definition' => [
'id' => Cassandra\Type\Base::INT,
'name' => Cassandra\Type\Base::VARCHAR,
'active' => Cassandra\Type\Base::BOOLEAN,
'friends' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => Cassandra\Type\Base::VARCHAR
],
'drinks' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => [
'type' => Cassandra\Type\Base::UDT,
'typeMap' => [
'qty' => Cassandra\Type\Base::INT,
'brand' => Cassandra\Type\Base::VARCHAR
]
]
]
]
]
]);
I've tried using the above example with several variations to accommodate the CollectionMap but nothing is working. My last attempt was this
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'value' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])
which gives the error Caught exception: Since v0.7, collection types should have \"definition\" directive. I've also tried using 'definition' instead of 'value'. I'm running out of ideas, any help would be greatly appreciated.
Use "definition" instead of "value". I tried this before but apparently I was doing something else wrong because this worked.
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'definition' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])

Categories