I am having an issue when adding a new row to Bigtable instead of one row it is adding 3 ideally I want it to have only one copy
this is the code I am using
use Google\Cloud\Bigtable\BigtableClient;
use Google\Cloud\Bigtable\DataUtil;
use Google\Cloud\Bigtable\Mutations;
$bigtable = new BigtableClient();
$table = $bigtable->table('claster', 'configuration');
$column_family_id = 'campaign';
$column_id = 'dsakjhasdkjhasdkj';
$mutations = (new Mutations())->upsert($column_family_id, "hahahaha", "campaign123");
$v = $table->mutateRow("campaign1854", $mutations);
printf('Successfully wrote row.' . PHP_EOL);
echo '<pre>';
print_r($v);
echo '</pre>';
what I get in return is this
Array
(
[campaign] => Array
(
[hahahaha] => Array
(
[0] => Array
(
[value] => campaign123
[labels] =>
[timeStamp] => 1586350256130000
)
[1] => Array
(
[value] => campaign123
[labels] =>
[timeStamp] => 1586350254707000
)
[2] => Array
(
[value] => campaign123
[labels] =>
[timeStamp] => 1586350253750000
)
)
)
)
in addition, every time I am trying to read the key it ads another copy of the value here is the code I use to read
$bigtable = new BigtableClient();
$table = $bigtable->table('claster', 'configuration');
$data = $table->readRow('campaign1854');
echo '<pre>';
print_r($data);
echo '</pre>';
I am getting this response with additional copy
Array
(
[campaign] => Array
(
[hahahaha] => Array
(
[0] => Array
(
[value] => campaign123
[labels] =>
[timeStamp] => 1586350256130000
)
[1] => Array
(
[value] => campaign123
[labels] =>
[timeStamp] => 1586350254707000
)
[2] => Array
(
[value] => campaign123
[labels] =>
[timeStamp] => 1586350253750000
)
[3] => Array
(
[value] => campaign123
[labels] =>
[timeStamp] => 1586350252676000
)
)
)
)
Every row in Bigtable is made up of Cells which is a set of values and timestamps. We call those cell versions.
Each time you run that script, it will add another value at the current timestamp which is causing you to have multiple versions in your cell. So the code you provided isn't causing it to write multiple versions, it will only write one version, but if you run the code multiple times, then it will add more versions.
I'm not sure why reading is causing more versions to be written, you must be running your write code by accident when your read code is run.
So there are some solutions you can use for this:
Allow each cell to only have one version through garbage collection. You can use the cbt tool to configure only one version per cell like so
cbt createfamily your-table cf2
cbt setgcpolicy your-table cf2 maxversions=1
When you are reading from Bigtable, you can apply a filter to only read the latest version of your cell like so:
$filter = Filter::limit()->cellsPerColumn(1);
$table->readRows([
'filter' => $filter
]);
Related
I have simple API call for themoviedb.org and I get results by external id (imdb_id), but I can't parse and get correct value.
I just want get value of ID --> [id] => 18773
So how I can get stored only themoviedb ID from API?
My code:
$url = "https://api.themoviedb.org/3/find/".$imdbId."?api_key=$apikey&language=en-US&external_source=imdb_id";
$content = file_get_contents($url, false);
// Tried
$json = json_decode($content, true);
echo $json['id']; // return nothing?
// Testing (check below array return)
$result = array_values(json_decode($content, true));
print("<pre>".print_r($result,true)."</pre>");
Array from $result
Array
(
[0] => Array
(
[0] => Array
(
[adult] =>
[backdrop_path] => /iSaKc4Nu7hTQDAvJigUVmNwTkj6.jpg
[genre_ids] => Array
(
[0] => 27
[1] => 9648
[2] => 53
)
[id] => 18773
[original_language] => en
[original_title] => Doppelganger
[overview] => A woman moves from NYC to LA after a murder, in which she is implicated. She is followed by what is apparently her evil alter- ego. She moves into a room for rent by a writer, and he begins having an affair with her, but after some strange things happen, he's not so sure if the affair is with her or her doppelganger.
[poster_path] => /kdMJhjqAlwJekpp8jGu6Lk3Tfy6.jpg
[release_date] => 1993-03-01
[title] => Doppelganger
[video] =>
[vote_average] => 5
[vote_count] => 56
[popularity] => 5.931
)
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
According to the documentation: https://developers.themoviedb.org/3/find/find-by-id you can get the id value by something like this $json['movie_results'][0]['id'].
But as you can see, the object movie_results is an array so you may have there more than one result or even no results.
So here are some tips what you should consider in your code:
What to do if there are more than one results?
What to do if there are no result at all?
If there is only one result you can safely get the ID.
Working on a personal project that will pull results from an API with full details of each pokemon.
So far I got the contents of the URL and returned the results into a JSON array format.
At the moment I am stuck on trying to retrieve results for[stats] inside from the array in an efficient manner.
private function getGenOnePokemon()
{
// the url of the api
$url = $this->baseUrl;
//get the contents of $url var and decode it into a json array
$json = file_get_contents($url , true);
$pokemon = json_decode($json, true, JSON_UNESCAPED_UNICODE);
// array output of pokemon
echo '<pre> ';
print_r($pokemon);
echo'</pre>';
//echo out value as speed
foreach($pokemon['results'][0] as $happy)
{
echo $happy['name'] . '<br />';
}
// echo base_stat value for speed with value of 90
echo $pokemon['stats'][0]['base_stat'];
}
However I do not seem to get anywhere much printing values/keys as I need to add something else to have full access to the values?
Would prefer not to directly access results, like I am doing with base_stat as plan on using this logic to pass into HTML View layer later.
Example of print_r dump (not full dump as really long) Full example: https://pokeapi.co/api/v2/pokemon/pikachu
Array
(
[forms] => Array
(
[0] => Array
(
[url] => https://pokeapi.co/api/v2/pokemon-form/25/
[name] => pikachu
)
)
[abilities] => Array
(
[0] => Array
(
[slot] => 3
[is_hidden] => 1
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/31/
[name] => lightning-rod
)
)
[1] => Array
(
[slot] => 1
[is_hidden] =>
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/9/
[name] => static
)
)
)
[stats] => Array
(
[0] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/6/
[name] => speed
)
[effort] => 2
[base_stat] => 90
)
[1] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/5/
[name] => special-defense
)
[effort] => 0
[base_stat] => 50
)
[2] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/4/
[name] => special-attack
)
[effort] => 0
[base_stat] => 50
)
[3] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/3/
[name] => defense
)
[effort] => 0
[base_stat] => 40
)
[4] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/2/
[name] => attack
)
[effort] => 0
[base_stat] => 55
)
[5] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/1/
[name] => hp
)
[effort] => 0
[base_stat] => 35
)
)
Any advice on how to access the data using foreach or other tips greatly appreciated. Thank you!
PHP has a specific function designed to target columnar data from arrays. It is called array_column()
If you want to isolate all of the name elements inside the forms subarray, use this:
$names=array_column($pokemon['forms'],'name');
If you want to isolate all of the base_stat elements inside of the stats subarray, use this:
$base_stats=array_column($pokemon['stats'],'base_stat');
Now you will have $names and $base_stats which are single-dimensional arrays by which you can perform additional processes or return from the function. Clean, intuitive, and simple.
Your $pokemon array doesn't contain a results field. There's only an forms field. So you should iterate over forms to print the names of the forms.
foreach($pokemon['forms'] as $happy) {
echo $happy['name'] . '<br />';
}
You could do the same thing with the stats
foreach($pokemon['stats'] as $stat) {
$base_stat = $stat['base_stat'];
// ...
}
I'm using php and got output from JIRA API like below which connects to tool using https://github.com/chobie/jira-api-restclient/blob/master/README.md. It fetches records very fine but i just want few information from all records so written below code with print_r($issue['description']) but it throws error:
Uncaught Error: Cannot use object of type chobie\Jira\Issue as array
What changes do I need to make in place of $issue to get these 3 information - [id:protected], [description] and [name].
How do I count total bugs found for e.g. number of chobie\Jira\Issue Object found?
Code:
$walker = new Walker($api);
$walker->push(
'project = "SEA" AND (status != "closed" AND status != "resolved") ORDER BY priority DESC'
);
foreach ( $walker as $issue ) {
print "<pre>";
print_r($issue);
print "</pre>";
}
So $issue prints everything like below but i just want to get [id:protected], [description] and [name]. What changes i need to make in place of $issue to get these 3 information. Can someone please give me some hint?
JSON:
chobie\Jira\Issue Object
(
[id:protected] => 21373505
[self:protected] => https://test.corp.com/rest/api/2/issue/21373505
[key:protected] => S12E-7337
[fields:protected] => Array
(
[Status] => Array
(
[self] => https://test.corp.com/rest/api/2/status/3
[description] => Working on the issue
[iconUrl] => https://test.corp.com/images/icons/statuses/assigned.png
[name] => In Progress
[id] => 3
[statusCategory] => Array
(
[self] => https://test.corp.com/rest/api/2/statuscategory/4
[id] => 4
[key] => indeterminate
[colorName] => yellow
[name] => In Progress
)
)
)
chobie\Jira\Issue Object
(
[id:protected] => 74534233
[self:protected] => https://test.corp.com/rest/api/2/issue/74534233
[key:protected] => ASE-7327
[fields:protected] => Array
(
[Status] => Array
(
[self] => https://test.corp.com/rest/api/2/status/3
[description] => This issue is being actively worked on at the moment by the assignee.
[iconUrl] => https://test.corp.com/images/icons/statuses/assigned.png
[name] => In Progress
[id] => 3
[statusCategory] => Array
(
[self] => https://test.corp.com/rest/api/2/statuscategory/4
[id] => 6
[key] => indeterminate
[colorName] => yellow
[name] => In Progress
)
)
)
Got a answer, you need to convert object to array first, get array fields and parse it accordingly.
$walker = new Walker($api);
$walker->push('project = "SEA" AND (status != "closed" AND status != "resolved") ORDER BY priority DESC');
foreach ( $walker as $issue ) {
$issue = $bugs->getFields();
print "<pre>";
print_r($issue);
print_r(trim($issue['Status']['name']);
print "</pre>";
}
I am trying to build a form where user inputs a data from a string and the form will get subsets of data through json src based on the input and create a multidimensional array and save it in database. If the input was previously added to database, I dont want it to append in the array again.
Here is how my code looks:
//SET UP the Array
$thisquery_themes[] = array(
strtolower($themename) => array(
"author"=>$data['Author'],
"desc"=>$data['Description'],
"screenshot"=> $screenshot,
"link"=> $data['URI'],
"count"=> 1
)
);
//Get Previously saved data
$existing_themes = get_option('top_themes');
if(!empty($existing_themes)){
foreach ($existing_themes as $group){
foreach(array_keys($group) as $key) {
if($group[strtolower($themename)] == strtolower($themename)){
unset($group[$key][strtolower($themename)]);
}
}
}
$total_themes= array_merge($existing_themes , $thisquery_themes);
update_option('top_themes', $total_themes);
} else {
update_option('top_themes', $thisquery_themes);
}
its not. if the key exist in the array, the data is still being added in the array:
Array (
[0] => Array (
[towfiq-i._v5] => Array (
[author] => Towfiq I.
[desc] => Towfiq I. official website.
[count] => 1
)
)
[1] => Array (
[towfiq-i._v5] => Array (
[author] => Towfiq I.
[desc] => Towfiq I. official website.
[count] => 1
)
)
[2] => Array (
[wp-bangla] => Array (
[author] => Ifty Rahman
[desc] => A website template for wpbangla
[count] => 1
)
)
[3] => Array (
[towfiq-i._v5] => Array (
[author] => Towfiq I.
[desc] => Towfiq I. official website.
[count] => 1
)
)
[4] => Array (
[wp-bangla] => Array (
[author] => Ifty Rahman
[desc] => A website template for wpbangla
[count] => 1
)
)
But I want it to be like this(Notice how "count" field value added up. let me know if its possible ):
Array (
[0] => Array (
[towfiq-i._v5] => Array (
[author] => Towfiq I.
[desc] => Towfiq I. official website.
[count] => 3
)
)
[1] => Array (
[wp-bangla] => Array (
[author] => Ifty Rahman
[desc] => A website template for wpbangla
[count] => 2
)
)
Any help would be greatly appreciated. Thanks
Why are you using an array within an array if it's only going to hold one value? Unless that's what's being thrown at you, your structure could easily look like:
Array (
[towfiq-i._v5] => Array (
[author] => Towfiq I.
[desc] => Towfiq I. official website.
[count] => 3
)
[wp-bangla] => Array (
[author] => Ifty Rahman
[desc] => A website template for wpbangla
[count] => 2
)
)
Then your world would become a lot easier because you could check to see if the array element exists by using isset($existing_themes[strtolower($themename)]) (as an example.)
If you can't change how you receive $existing_themes, you can reformat the data like this:
$existing_themes_mod = array();
foreach ($existing_themes as $t) {
$existing_themes_mod[key($t)] = reset($t);
}
This should give you enough of a strategy to work with an be able to run an "update" on existing entries instead of an appendage. If you need help writing it, please show me your attempt first.
EDIT - Thanks for pasting your code. You should watch out for all that whitespace by the way... here's a better way to achieve your goal
if (!empty($existing_themes)) {
if (isset($existing_themes[strtolower($themename)])) {
if (isset($existing_themes[strtolower($themename)]['count'])) {
$existing_themes[strtolower($themename)]['count'] = $existing_themes[strtolower($themename)]['count'] + 1;
} else {
$existing_themes[strtolower($themename)]['count'] = 1;
}
$thisquery_themes = array();
}
$total_themes= array_merge($existing_themes , $thisquery_themes);
update_option('top_themes', $total_themes);
} else {
update_option('top_themes', $thisquery_themes);
}
The reason your attempt didn't work is because when you try to change the $value by incrementing it by 1, it doesn't modify the original array. When you use foreach like that, you need to iterate by reference or else you can't modify the value. You're making too much work for yourself!
can't figure out what is the right syntax to add an additional attributes to a LDAP entry. When authenticating i get this array:
Array
(
[0] => Array
(
[cn] => Array
(
[0] => Vit Kos
)
[shortname] => Array
(
[0] => vit.kos
)
[uid] => Array
(
[0] => vit.kos
)
[mail] => Array
(
[0] => vit.kos#email.com
)
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => inetOrgPerson
[4] => dominoPerson
)
[givenname] => Array
(
[0] => Vit
)
[userpassword] => Array
(
[0] => password here
)
[sn] => Array
(
[0] => Kos
)
[localadmin] => Array
(
[0] => CN=#SysHQAdmin
)
[mailaddress] => Array
(
[0] => Vit.Kos#email.com
)
[maildomain] => Array
(
[0] => EMAIL
)
[dn] => CN=Vit Kos,OU=###,O=EMAIL
)
)
need to get an additional attribute member to be like
Array (
[uid] => Array
(
[0] => vit.kos
)
[mail] => Array
(
[0] => vit.kos#email.com
)
[member] => Array
(
[0] => MEMBER HERE
)
)
Never worked with LDAP before so it's quite confusing for me. Thanks for the answers.
To assign data to the directory item that you retrieved above, you will perform a "modify" operation with ldap_modify(). This is assuming that the schema of your database allows an attribute called member on this object - which it may not, you cannot simply add attributes to any object as and when you feel like it.
Firstly, you will need to create the entry or entries that will belong to the member attribute, and store them in an array:
$member = array (
0 => "This is some data",
1 => "This is some more data"
);
In order to tell the directory which object we want to modify, we will need it's DN. We can get this from the result of your previous search/list/read operation - the array that you show you have retrieved already (I assume this is stored in a variable called $array):
$dn = $array[0]['dn'];
Now we have all the information we need to perform the modify operation (I assume your connected/bound LDAP resource is held in a variable called $ds):
$result = ldap_modify($ds, $dn, array('member'=>$member));
After this, $result will be a boolean indicating whether the operation was successful or not. If it was unsuccessful, you can get an error message explaining why by calling:
$error = ldap_error($ds);