Passing configuration options to Hadoop

Posted March 7th, 2013 in Razno by Metod

If you want to override Hadoop job configuration options, you can do so via command line -D key=value.

When I wanted to pass multiple configuration options including some special characters:

  1. hadoop jar MyJob.jar MyJob -D key=something,else -D key2=jdbc:mysql://host?user=u&password=p

I got some errors like:

  1. -bash: -D: command not found

The solution is to put values in quotes like this:

  1. hadoop jar MyJob.jar MyJob -D key="something,else" -D key2="jdbc:mysql://host?user=u&password=p"

Enjoy! :)

Comments Off

So you wish to play poker online

Posted January 26th, 2013 in Razno by Metod

Pound for Pound: Advantages of online poker over live poker

Online Poker Since the online poker industry boomed in the beginning of the 21st century, these online poker sites are reaping the benefits of the rapid technological advancement in the global scale. Aside from having millions of players worldwide, these poker sites can hold huge live poker tournaments, get brand ambassadors who are World Series of Poker bracelet holders, and place advertisements all over the internet. However, despite the success of these online poker sites, live poker at brick-and-mortar casinos and poker houses still thrive in the foundation it laid decades ago. Las Vegas, Macau, and other gaming hubs in the world are still alive and kicking and haven’t lost the glitz and glamour that attracts poker aficionados worldwide. With these things mentioned, let us examine what are the advantages and disadvantages of playing poker online or live.

To begin with, playing online poker has many advantages. One good thing about online poker is that provides players an alternative in case there is a shortage of poker tables in their country. In Slovenia, there are only a handful of casinos where poker players can hone their card skills and compete with the best Slovenian players around. However, because of there are only a few casinos in Slovenia, poker players have problems waiting in queue just to play poker. This is the reason why professional Slovenian players are at the bottom half of the ranking of poker players worldwide. While neighboring countries like Germany and France have a lot of poker players who already have millions of tourney earnings, only Casey Kastle from Ljublana has joined the global millionaire’s club. Fortunately, there are online poker sites like PartyPoker Slovenia where Slovenians can sharpen their poker skills without having to wait for a long time just to finally get a seat in a poker table.

Aside from providing a training ground for future pro players, there are also lots of advantages that players can reap from playing online poker. Imagine the hassles of having to dress up, drive all the way to a casino, pay entrance fees, exchange cash for chips, and look for a decent poker table—all of these things you have to experience before you can actually play. In online poker, you can just fire up your laptop or any device capable of online poker gaming, open the program or go to the website directly, and start playing poker in a matter of minutes. Another advantage of online poker is that you are in control of your gaming. You can start and stop at your convenience or in case you already suffer from a losing streak. The only disadvantage of playing online poker is that you are not playing vis-à-vis with other players, which means there is no way you can read their situation with their facial expressions, eye movements, etc.

Fix for: There is no `_sonata_admin` defined for the controller …

Posted December 25th, 2012 in Razno by Metod

If you stumbled upon the following error while trying to make SonataAdminBundle work:

There is no `_sonata_admin` defined for the controller `Acme\DemoBundle\Controller\PostController` and the current route `admin_acme_demo_post_list`

Here is what is wrong with your configuration.

The official documentation (in my case for ORM) says to define the *Admin service as follows:

  1. # app/config/config.yml
  2. services:
  3.    acme.demo.admin.post:
  4.       class: Acme\DemoBundle\Admin\PostAdmin
  5.       tags:
  6.         – { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
  7.       arguments: [null, Acme\DemoBundle\Entity\Post, AcmeDemoBundle:PostAdmin]

Notice that null in the arguments? That is what is wrong.

That means that as the code for the admin it passes null. Whereas the correct thing to do would be to pass in the name of the service itself. Like this:

  1. # app/config/config.yml
  2. services:
  3.    acme.demo.admin.post:
  4.       class: Acme\DemoBundle\Admin\PostAdmin
  5.       tags:
  6.         – { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
  7.       arguments: [acme.demo.admin.post, Acme\DemoBundle\Entity\Post, AcmeDemoBundle:PostAdmin]

Happy coding!

Symfony2: Upgrading 2.0 to 2.1 – CallbackValidator

Posted November 12th, 2012 in Razno by Metod

The CallbackValidator class was deprecated in 2.1.

If you implemented custom validators using this interface, you can substitute them by event listeners listening to the FormEvents::POST_BIND (or any other of the *BIND events). In case you used the CallbackValidator class, you should now pass the callback directly to addEventListener.

Here is how you do it.

Before:

After:

Enjoy! ;)

Doctrine2: CHAR with annotations

Posted November 7th, 2012 in Razno by Metod

Ever wondered if there is a better way of defining a CHAR (mysql) with doctrine2 annotations?

From doctrine2 docs:

  1. <?php
  2. /**
  3.  * @Column(type="string", columnDefinition="CHAR(2) NOT NULL")
  4.  */
  5. protected $country;

Not a very nice approach imo. I dug around the code and found a somewhat neater solution:

  1. <?php
  2. /**
  3.  * @Column(type="string", length=2, options={"fixed" = true})
  4.  */
  5. protected $country;

Now you don’t need to manually type in column definitions. Enjoy!

Debugging PHP: Part II – References

Posted October 28th, 2012 in Razno by Metod

If you read around the internets, you will see alot of references are bad stuff. Well, they are if you deal with them in the wrong way. Let’s take a look at an example:

  1. <?php
  2. // …
  3.  
  4. foreach ($all_regions as &$regions) {
  5.     $regions[‘en’] = array_replace($regions[‘ascii’], $regions[‘en’]);
  6.     unset($regions[‘ascii’]);
  7. }
  8.  
  9. // alot of lines of code here
  10.  
  11. foreach ($all_regions as $country_id => $cultures) {
  12.     foreach ($cultures as $culture => $regions) {
  13.         // …
  14.     }
  15. }
  16.  
  17. // …

Just a simple code to replace any missing english translations with the ascii ones. For which we decided to use a reference. Which is all good, except … !

Can you spot the problem?

In the second nested foreach, in the second run of that foreach we get an error that the supplied value is not an array. But we know it is an array! So what is happening here?

Well, that reference we used has bitten us. Remember, how the values assigned in foreach loops do not get unset when we leave that foreach scope? The problem is that the &$regions did not get unset automatically and we kept that reference all the way down to that problematic foreach. In the first run of the second foreach we then effectively appoint the value of $cultures to the $all_regions variable, replacing it’s original array values. Thus we get the error.

What was the correct code?

Just unset the reference.

  1. <?php
  2. // …
  3.  
  4. foreach ($all_regions as &$regions) {
  5.     $regions[‘en’] = array_replace($regions[‘ascii’], $regions[‘en’]);
  6.     unset($regions[‘ascii’]);
  7. }
  8.  
  9. // this only unsets the reference, not the variable it is pointing to
  10. unset($regions);
  11.  
  12. // …

And the problem goes away. These kinds of bugs are hard to spot. Especially since there is quite some code between the two. So when using references keep in mind: Always unset your references once you’re done with them!

Debugging PHP: Part I – Type casting

Posted October 27th, 2012 in Razno by Metod

Here and there I run into some interesting bugs which I think would be nice to share. So here goes, bug #1 – Type casting.

  1. <?php
  2. $type = ‘sometype’;
  3.  
  4. // …
  5.  
  6. $entity->setSync((int)$type === ‘sometype’);
  7.  
  8. // …
  9.  
  10. if ($entity->getSync()) {
  11.     // this should be run
  12. }
  13.  
  14. // …
  15.  

Can you spot the problem? That if never gets evaluated. Why?

The problem is here:

  1. $entity->setSync((int)$type === ‘sometype’);

In this case, the (int) cast only casts the $type variable, not the whole expression. So (int)$type === 0 which definitely is not equal to ‘sometype’ and also since we are using strict checking, type int is not equal to type string. The result is always boolean false.

The correct line should be:

  1. $entity->setSync((int)($type === ‘sometype’));

So watch out with casting and parenthesis.

Symfony2: Bundle does not contain any mapped entities

Posted September 14th, 2012 in Symfony2 by Metod

There is one more tip to solving this mistery. Make sure you have a namespace statement in the entity line.

  1.  
  2. <?php
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5.  
  6. /**
  7.  * @ORM\Entity
  8.  */
  9. class User
  10. {
  11.     // code here
  12. }
  13.  

This will cause the error. But if you add the namespace (duh moment) it will work again.

  1. <?php
  2.  
  3. namespace Acme\DemoBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * @ORM\Entity
  9.  */
  10. class User
  11. {
  12.     // code here
  13. }
  14.  

Post messages to a Teamspeak3 instance

Posted September 9th, 2012 in Snippets by Metod

This is a simple script for posting messages to a Teamspeak3 instance.

It uses the TeamSpeak3 Framework.

Symfony2: Reload user roles

Posted June 22nd, 2012 in Razno, Symfony2, Tehnikalije by Metod

If you are not using FOSUserBundle for managing your user system, then you might have stumbled onto a problem of reloading user roles when you change them. Here is a possible solution.

Your UserInterface::equals() method might look something like this:

Now all you need to do is change it so it also checks the user roles:

And it should work!

Now whenever you change any user’s roles it will be effective immediately after the next page refresh instead of logout / login.