Thursday, September 3, 2015

Installing MySQL on CentOS

Applicable to Centos Versions:

  • Centos 5.x
  • Centos 6.x

Requirements

  1. Login to a terminal as root using one of these options: (su –login | su -l | or: su –)
  2. Yum and rpm must also be installed and functional (something is seriously wrong if they aren’t)

Doing the Work

1. Login as root user

su - root

2. Install mysql mysql-server:

yum install mysql-server

3. Check the staus of mysql service

service mysqld status

4. Start MySQL service

service mysqld start

If it does not show any error. You are done with the installation.

Login to mysql


Type the below command on terminal
#> mysql

Thursday, August 27, 2015

JDBC Hive client

Before Writing the JDBC client for Hive, Make sure you add the required Hive jar file on your project classpath.

JDBC HIVE 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";
  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:9000/default""""");
    Statement stmt = con.createStatement();
    String tableName = "testHiveTable";
    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));
    }
  }
}