Friday, June 7, 2013

PleaseHoldException in HBase Shell (CloudEra)


Started learning a bit on Hadoop, HBase. In the coming months will post a bit about what I have been learning on that. So here goes,

I installed CloudEra on my local, installed Hadoop in a pseudo-distributed mode and HBase in a standalone mode. All installations were successful, but when I went into the HBase Shell and tried to run some commands, all I got was a "PleaseHoldException: Master is initializing"

So, the solution to this is to open the "/etc/hosts" file. Check the entries and there you'll find an entry with ip as "127.0.1.1". Change this to "127.0.0.1" and restart HBase and things should work fine.

This error normally comes because when you start HBase, it will search for the master node and try to connect to that. In the local system, it is not able to resolve the "127.0.1.1" and hence the exception.

Hope it helps. Cheers.


Tuesday, January 29, 2013

Connecting to servlet from Java client

Code as below. Hope it is easily understandable

Servlet Code:
public class MyServlet extends HttpServlet 
{
 public void init(ServletConfig config)
 {
  //Servlet Init code here
 }
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
        
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception
 {
  ObjectInputStream resultStream = null;
  String results = null;
  System.out.println("Got Data......");
  resultStream = new ObjectInputStream(request.getInputStream());
  System.out.println("Got Result Stream......");
  results = resultStream.readObject();
  resultStream.close();
  sendResponse(response, results);
 }

 public void sendResponse(HttpServletResponse response, String results) throws IOException 
 {
  ObjectOutputStream sendStream = null;
  sendStream = new ObjectOutputStream(response.getOutputStream());
  sendStream.writeObject(results);
  sendStream.flush();
  sendStream.close();
 }
}

Client Code:

public class MyClient 
{
 public String callServlet(String dataToSend)
 {
  String responseData = "";
  try 
  {
   URL serverURL = new URL("http://localhost:8080/MyServlet");
   URLConnection serverConnection = serverURL.openConnection();
   serverConnection.setDoInput(true);
   serverConnection.setDoOutput(true);
   serverConnection.setUseCaches(false);
   serverConnection.setDefaultUseCaches(false);
   serverConnection.setRequestProperty("Content-Type", "text/plain");
   
   ObjectOutputStream out = new ObjectOutputStream(serverConnection.getOutputStream());
   out.writeObject(Compressor.compress(dataToSend));
   out.flush();
   out.close();
   ObjectInputStream in = new ObjectInputStream(serverConnection.getInputStream());
   responseData = (String) in.readObject();
   System.out.println("Records received...");
  } 
  catch (MalformedURLException e) 
  {
   e.printStackTrace();
  } 
  catch (IOException e) 
  {
   e.printStackTrace();
  } 
  catch (ClassNotFoundException e) 
  {
   e.printStackTrace();
  }
  return responseData;
 }
}

Friday, January 25, 2013

Getting AutoIncrement value in PHP-MySQL


It is more often than not a requirement for developers to get the value of the 'id' field back after the insert operation. PHP-MySQL has a couple of ways to do this.

The first way: use "mysql_insert_id()"
Example:

$query = "INSERT INTO tableA (id,user_id,field2, field3) VALUES (NULL, '$user_id','$field2_id','$field3_id')";
$result = mysql_query($query);
$insert_id = mysql_insert_id();

The second way: use "LAST_INSERT_ID()"
This is more useful when you have to insert the data into some other table in the same session.
Example:
INSERT INTO TABLEA (ID, FIELD1) VALUES(NULL, 'some value');
 
INSERT INTO TABLEB (ID, NAME, TABLEA_ID)
VALUES (NULL, 'Pramod Rao', LAST_INSERT_ID());

Hope this helps.

N.B: Both cases are unique for the session, so there should not be any issues. But if you have multiple threads, then you might have concurrency issues.

Thursday, January 24, 2013

Wordpress - Creating a plugin

 Similar to my last post on creating wordpress templates, creating a plugin follows pretty much the same pattern of simplicity. So, lets just dive in. Here are the steps to do it.
  • Navigate to the plugins directory in your wordpress installation. [/wp-content/plugins/]
  • Create a new folder "my_plugin" in the directory
  • Create a new file "my_plugin.php" in the "my_plugin" directory
  • Type the below code in the file
    <?php  
        /* 
        Plugin Name: my_plugin 
        Plugin URI: http://www.flexmycode.com
        Description: Test Plugin
        Author: Pramod Rao
        Version: 1.0 
        Author URI: http://www.flexmycode.com
        */  
    ?>  

That's all. By all means, change the content in the header.
Once this is done, if you refresh the plugins page in the admin panel in wordpress, you'll see the plugin listed below.

Wednesday, January 23, 2013

Wordpress - Creating a template

In my previous post we saw on how to create a custom child theme. In this post we'll extend our understanding to creating templates in wordpress.

When working with wordpress to create custom sites, more often than not you will encounter the need to create customized pages. Thanks to the extensibility of wordpress, this is very simple to achieve. Below are the simple steps to do this.

  • Navigate to the theme directory in your wordpress installation. [e.g. "/wp-content/themes/twentyten"]
  • Create a new file called, "my_template.php" (or whatever name you choose)
  • In the new file add the code below. 

<?php
/*
Template Name: My Template
*/
?>

<?php get_header(); ?>
Welcome to my template!!
<?php get_footer(); ?>

Here, "Template Name" refers to the name that your template will be called. This will be shown to you in the admin panel while creating new pages.
Next we get the header and the footer for the site with some custom coding in between, where in you can place your HTMLs.
Simple enough and you are done. Now all you have to do is create a new page in wordpress and assign your template to it and you are ready to go!!

In the next post we will see how to create plugins in your wordpress installation.

Saturday, January 19, 2013

Wordpress - Creating a child theme

Been dabbling with wordpress these days. One of the interesting things that I came along is creating themes for wordpress. Although, I did not create a fully functional theme, I managed to create a child theme. Here's the basics of creating a child theme.
For simplicity's sake, I'll be using the TwentyTen theme that comes along with wordpress installations.

Steps to follow
  1. Create a new folder in ""wordpress/themes" directory
  2. Name it "twentytenchild". (or whatever you choose)
  3. Create a file "style.css" in that folder.
  4. Punch in the "Theme Name", "Description", "Author" and "Template" under block comments
  5. Import the twentyten stylesheet
And lo and behold. Your child theme is created.
To add custom functionality, you can start off with adding a "functions.php" in your "twentytenchild" folder. This file will extend the functions.php defined in the twentyten theme. This way you can put in your functionalities without them being erased by the updation of the original theme itself.

In the next post I'll be talking about the creation of templates in wordpress. See it here.

Friday, January 18, 2013

Shared Objects in multiple Applications

So, I had this problem of opening one Flex Application from another and passing on some data to it entirely in the client side. What hit was using SharedObject to pass around the data. The one roadblock though was that Application2 was not able to read the SharedObject created by Application1. A bit of research led to my understanding as below.

In the normal sense, when we create a SharedObject we write the code as below,
var so:SharedObject = SharedObject.getLocal( "mySO" );

What the above line does is that it reads the SharedObject named "mySO" from the file "mySO.sol" placed in the local system. If the file does not exist, then it creates a new one with the same name.

In Windows 7, you can find the file in the following file path (This may vary based on the OS)
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost\work\myApp\flexbin\Application1.swf\mySO.sol

The problem what happens when Application2 tries to access mySO is that, it actually is looking into another folder to get the SharedObject and not the above one. So the path in which Application2 tries to access is (Again, this will vary based on the OS)
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost\work\myApp\flexbin\Application2.swf\mySO.sol

To get around this, what we can do is create a SharedObject in a common location that both the applications can access. We do this by writing code as below,
var so:SharedObject = SharedObject.getLocal( "mySO", "/" );

The above code creates/reads a SharedObject from the following location
C:\Users\Pramod\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\TC6AK52F\localhost

So now, both the applications can read the SharedObject. Hope this helps.

Tuesday, January 15, 2013

WAMP MySQL Error 1045

I have just started dabbling with wordpress. So as a first step I decided to set it up in my local system with WAMP server. Pretty surely, as soon as I set it up I ran into the mysql issue when I tried to open the phpmyadmin console in the web browser.


 #1045 - Access denied for user 'root'@'localhost' (using password: NO)
OR

 #1045 - Access denied for user 'root'@'localhost' (using password: NO)

I figure this is a common error when setting up, but it surely wasted a lot of my time.
Here are the common steps that one can go through to fix this,

  1. Open MySQL console and login to MySQL. If you are able to login, then ensure that the password that you used to login is updated into "wamp/apps/phpmyadmin/config.inc.php".
  2. Restart WAMP server
  3. Delete all files in "wamp/tmp"
  4. Delete all cookies from your browser
After these steps try opening the phpmyadmin console again. Hopefully you won't have the same error again.