Wednesday, June 18, 2014

JDBC Client Sample Code

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
 
public class HiveJdbcClient {
  private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
 
  /**
 * @param args
 * @throws SQLException
   */
  public static void main(String[] args) throws SQLException {
      try {
      Class.forName(driverName);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
    Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
    Statement stmt = con.createStatement();
    String tableName = "testHiveDriverTable";
    stmt.executeQuery("drop table " + tableName);
    ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");
    // show tables
    String sql = "show tables '" + tableName + "'";
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    if (res.next()) {
      System.out.println(res.getString(1));
    }
    // describe table
    sql = "describe " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(res.getString(1) + "\t" + res.getString(2));
    }
 
    // load data into table
    // NOTE: filepath has to be local to the hive server
    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
    String filepath = "/tmp/a.txt";
    sql = "load data local inpath '" + filepath + "' into table " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
 
    // select * query
    sql = "select * from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
    }
 
    // regular hive query
    sql = "select count(1) from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(res.getString(1));
    }
  }
}



Monday, May 12, 2014

View System configuration - Ubuntu

How to see your system configuration



  1. Open Terminal.
  2. Type this command : sudo lshw -html > lshw.html
  3. Go to Home folder
  4. Open lshw.html file in browser

Thats it.... :)


sudo lshw -html > lshw.html will create a html file on your home folder with all configuration details.

Friday, November 29, 2013

About the book "Instant Apache Sqoop"

Instant Apache Sqoop 


Apache Sqoop(TM) is a tool designed for efficiently transferring bulk data between Apache Hadoop and structured datastores such as relational databases. Sqoop successfully graduated from the Incubator in March of 2012 and is now a Top-Level Apache project.

Developers may need to import data from Sql to Hadoop HDFS, Hive, HBase. Sqoop is the best tool for it. “Instant Apache Sqoop” is describing how to use Sqoop. “Instant Apache Sqoop” the title is accurate and self describing. Introduction itself is in a good and informative one. Even the layman can use Sqoop by using this book, the author “Ankit Jain ” wrote this book as simple. This book covers almost every apsects of Sqoop, import to HDFS, Hive, HBase and the exports as well.

This book is well illustrated especially in “How it works” is added in each and every part. This helped me alot to understand the back-stage things of Sqoop. Actually i am blind about the various connectors supporting by the Sqoop. This book helps me to find out these.

  • MySQL
  • Oracle
  • SQL Server
  • PostGre
  • DB2
  • HSQLDB

The important thing is I learned “Incremental Import”. Incremental import means importing the new version of records or the latest inserted records from the RDBMS table into HDFS . I think that is a very good option in Sqoop.

I can't find a Sqoop client  (Sqoop-Java client) in this book. I expect that also. But as the name implies it is Instant.

So my friends if you need a quick start on Sqoop, “Instant Apache Sqoop” will help you.

All codes used in this book are available at http://www.PacktPub.com . 
You can buy this book from :