AnsweredAssumed Answered

HBase Code is not Running - java.net.ConnectException

Question asked by sunny_hello18 on May 30, 2012
Latest reply on Jun 1, 2012 by yufeldman
I have some sample code downloaded for the HBase Definative Guide and tried to execute it. But it is shwoing some starnge issue for which I have not got any answer on any internet site blog. I am using MapR virtual machine (as downloaded from Mapr website)

Any help is highly appriciated. Thanks in advance

**Here is the error Code -**

    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.3.2--1, built on 08/30/2011 17:53 GMT
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:host.name=mapr-desktop
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:java.version=1.6.0_26
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Sun Microsystems Inc.
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:java.home=/usr/lib/jvm/java-6-sun-1.6.0.26/jre
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:java.class.path=/home/mapr/EclipseWorkspace/MyHBase/bin:/opt/mapr/hbase/hbase-0.90.4/hbase-0.90.4.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/hadoop-0.20.2-dev-core.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/commons-logging-1.0.4.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/log4j-1.2.15.jar:/opt/mapr/hadoop/hadoop-0.20.2/lib/zookeeper-3.3.2.jar
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:java.library.path=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/amd64/server:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/amd64:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=/tmp
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:os.version=2.6.32-33-generic
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:user.name=mapr
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:user.home=/home/mapr
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Client environment:user.dir=/home/mapr/EclipseWorkspace/MyHBase
    12/05/30 23:27:03 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection
    12/05/30 23:27:03 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181
    12/05/30 23:27:03 WARN zookeeper.ClientCnxn: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
    java.net.ConnectException: Connection refused
     at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
     at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)
     at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1125)
    12/05/30 23:27:04 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181
    12/05/30 23:27:04 WARN zookeeper.ClientCnxn: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
    java.net.ConnectException: Connection refused
     at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
     at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)
     at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1125)

Source Java Code -

HBaseHelper.java
----------------

    package client;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.KeyValue;
    import org.apache.hadoop.hbase.client.Get;
    import org.apache.hadoop.hbase.client.HBaseAdmin;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    /**
     * Used by the book examples to generate tables and fill them with test data.
     */
    public class HBaseHelper {
    
      private Configuration conf = null;
      private HBaseAdmin admin = null;
    
      protected HBaseHelper(Configuration conf) throws IOException {
       System.out.println("HBaseHelper Constructor step1...");
     this.conf = conf;
     System.out.println("HBaseHelper Constructor step2...");
        this.admin = new HBaseAdmin(conf);
        System.out.println("HBaseHelper Constructor step-last...");
      }
    
      public static HBaseHelper getHelper(Configuration conf) throws IOException {
       System.out.println("getHelper...");
        return new HBaseHelper(conf);
      }
    
      public boolean existsTable(String table)
      throws IOException {
       System.out.println("existsTable...");
        return admin.tableExists(table);
      }
    
      public void createTable(String table, String... colfams)
      throws IOException {
        createTable(table, null, colfams);
      }
    
      public void createTable(String table, byte[][] splitKeys, String... colfams)
      throws IOException {
        HTableDescriptor desc = new HTableDescriptor(table);
        for (String cf : colfams) {
          HColumnDescriptor coldef = new HColumnDescriptor(cf);
          desc.addFamily(coldef);
        }
        if (splitKeys != null) {
          admin.createTable(desc, splitKeys);
        } else {
          admin.createTable(desc);
        }
      }
    
      public void disableTable(String table) throws IOException {
        admin.disableTable(table);
      }
    
      public void dropTable(String table) throws IOException {
        if (existsTable(table)) {
          disableTable(table);
          admin.deleteTable(table);
        }
      }
    
      public void fillTable(String table, int startRow, int endRow, int numCols,
                            String... colfams)
      throws IOException {
        fillTable(table, startRow, endRow, numCols, -1, false, colfams);
      }
    
      public void fillTable(String table, int startRow, int endRow, int numCols,
                            boolean setTimestamp, String... colfams)
      throws IOException {
        fillTable(table, startRow, endRow, numCols, -1, setTimestamp, colfams);
      }
    
      public void fillTable(String table, int startRow, int endRow, int numCols,
                            int pad, boolean setTimestamp, String... colfams)
      throws IOException {
        fillTable(table, startRow, endRow, numCols, pad, setTimestamp, false, colfams);
      }
    
      public void fillTable(String table, int startRow, int endRow, int numCols,
                            int pad, boolean setTimestamp, boolean random,
                            String... colfams)
      throws IOException {
        HTable tbl = new HTable(conf, table);
        Random rnd = new Random();
        for (int row = startRow; row <= endRow; row++) {
          for (int col = 0; col < numCols; col++) {
            Put put = new Put(Bytes.toBytes("row-" + padNum(row, pad)));
            for (String cf : colfams) {
              String colName = "col-" + padNum(col, pad);
              String val = "val-" + (random ?
                Integer.toString(rnd.nextInt(numCols)) :
                padNum(row, pad) + "." + padNum(col, pad));
              if (setTimestamp) {
                put.add(Bytes.toBytes(cf), Bytes.toBytes(colName),
                  col, Bytes.toBytes(val));
              } else {
                put.add(Bytes.toBytes(cf), Bytes.toBytes(colName),
                  Bytes.toBytes(val));
              }
            }
            tbl.put(put);
          }
        }
        tbl.close();
      }
    
      public String padNum(int num, int pad) {
        String res = Integer.toString(num);
        if (pad > 0) {
          while (res.length() < pad) {
            res = "0" + res;
          }
        }
        return res;
      }
    
      public void put(String table, String row, String fam, String qual,
                      String val) throws IOException {
        HTable tbl = new HTable(conf, table);
        Put put = new Put(Bytes.toBytes(row));
        put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), Bytes.toBytes(val));
        tbl.put(put);
        tbl.close();
      }
    
      public void put(String table, String row, String fam, String qual, long ts,
                      String val) throws IOException {
        HTable tbl = new HTable(conf, table);
        Put put = new Put(Bytes.toBytes(row));
        put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), ts,
                Bytes.toBytes(val));
        tbl.put(put);
        tbl.close();
      }
    
      public void put(String table, String[] rows, String[] fams, String[] quals,
                      long[] ts, String[] vals) throws IOException {
        HTable tbl = new HTable(conf, table);
        for (String row : rows) {
          Put put = new Put(Bytes.toBytes(row));
          for (String fam : fams) {
            int v = 0;
            for (String qual : quals) {
              String val = vals[v < vals.length ? v : vals.length - 1];
              long t = ts[v < ts.length ? v : ts.length - 1];
              put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), t,
                Bytes.toBytes(val));
              v++;
            }
          }
          tbl.put(put);
        }
        tbl.close();
      }
    
      public void dump(String table, String[] rows, String[] fams, String[] quals)
      throws IOException {
        HTable tbl = new HTable(conf, table);
        List<Get> gets = new ArrayList<Get>();
        for (String row : rows) {
          Get get = new Get(Bytes.toBytes(row));
          get.setMaxVersions();
          if (fams != null) {
            for (String fam : fams) {
              for (String qual : quals) {
                get.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual));
              }
            }
          }
          gets.add(get);
        }
        Result[] results = tbl.get(gets);
        for (Result result : results) {
          for (KeyValue kv : result.raw()) {
            System.out.println("KV: " + kv +
              ", Value: " + Bytes.toString(kv.getValue()));
          }
        }
      }
    }

----------------------------------------------------

PutExample.java
---------------

package client;


    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.util.Bytes;
    // ^^ PutExample
    import client.HBaseHelper;
    
    
    import java.io.IOException;
    
    public class PutExample {
    
      public static void main(String[] args) throws IOException {
     System.out.println("step1...");
     Configuration conf = HBaseConfiguration.create();
     System.out.println("step2...");
        
    
        HBaseHelper helper = HBaseHelper.getHelper(conf);
        System.out.println("step3...");
        helper.dropTable("testtable");
        System.out.println("step4...");
        helper.createTable("testtable", "colfam1");
        System.out.println("step5...");
        HTable table = new HTable(conf, "testtable");
    
        Put put = new Put(Bytes.toBytes("row1"));
    
        put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
          Bytes.toBytes("val1"));
        put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
          Bytes.toBytes("val2"));
    
        table.put(put);
      }
    }
    // ^^ PutExample

Outcomes