วันอาทิตย์ที่ 21 กันยายน พ.ศ. 2557

Resize Partition LVM

เพิ่งได้เจอมาคือว่า..ลง CentOS 6.5 มี HDD 1 TB ใช้ default แบ่งได้คือ lv_root = 50GB ,  lv_home = ที่เหลือ ประเด็นคือส่วนมากผมลงโปรแกรมไว้ /opt/ ปรากฏว่า lv_root เต็ม ทีนี้ต้องขยาย

Step 1

# df -h
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vg_repo-lv_root 148G  113G   29G  80% /
tmpfs                       499M     0  499M   0% /dev/shm
/dev/sda1                   485M  156M  304M  34% /boot
/dev/mapper/vg_repo-lv_home 47G  181M   45G   1%  /home

Step 2

#cd /
#cp -Ra /home /home.bak  # Make a backup of home
#umount /home
#lvm lvremove /dev/vg_<hostname>/lv_home   (Remove the logical volume for home)
#lvm lvresize -l+100%FREE /dev/vg_<hostname>/lv_root  (Resize the root logical volume so it uses 100% of the now free space)
#resize2fs /dev/vg_<hostname>/lv_root  (Resize the filesystem to use the whole logical volume)

#mv /home.bak /home  (Restore the backup)

Step 3

#lvdisplay
#lvcreate -n lv_home -l 100%FREE VolGroup
#mke2fs -j /dev/VolGroup/lv_home
#mount /dev/VolGroup/lv_home /home

เสร็จ......


*******เพิ่มเติม*******

# comment out /home
แก้ไข /etc/fstab โดย คอมเม้น /home

* ถ้า boot ไม่ขึ้น
1. mount -o remount,rw / (เพื่อให้แก้ไขได้ ไม่งั้นจะ readonly)
2. แก้ไข /etc/fstab โดย คอมเม้น /home


วันพฤหัสบดีที่ 4 กันยายน พ.ศ. 2557

ติดต่อ Big Data (Hadoop) ผ่าน Jdbc ด้วย Hive และ Impala

บ้านเรายังไม่ค่อยตื่นตัวเรื่อง Big Data กันสักเท่าไหร่ แต่พอดีผมได้มีโอกาสทำงานกับข้อมูลเดือนละหลักพันล้าน record / ใช้พวก relation database เอาไม่อยู่ครับ query ง่าย ๆ ใช้เวลากันครึ่งค่อนวันเลยทีเดียว เจ้า Big Data เลยเป็นอีกทางเลือก มาลองดูกันว่าจะเข้าไปคุยกับมันยังไงดี  ในที่นี้เป็น Cloudera Pack (Hadoop,hive,impala...)

- library ที่ต้องใช้


























1. Jdbc Hive ตัวอย่างการเขียน ดึงข้อมูลมาแสดง

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveConnection {

    private static final String driver = "org.apache.hive.jdbc.HiveDriver";
    private static final int hive_port = 10000;
    private static final String host = "127.0.0.1";
    private static final String db = "hadoopdb";

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        return DriverManager.getConnection("jdbc:hive2://" + host + ":" + hive_port + "/" + db);
    }

    public static void main(String[] args) {

        System.out.println("\n=============================================");

        System.out.println("start....");
        Connection con = null;

        try {
            con = getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from table_name");
            while (rs.next()) {
                System.out.println("-" + rs.getString(1));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
}

2. Jdbc Impala ตัวอย่างการเขียน ดึงข้อมูลมาแสดง

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ImpalaConnection {

    private static final String driver = "org.apache.hive.jdbc.HiveDriver";
    private static final int impala_port = 21050;
    private static final String host = "127.0.0.1";
    private static final String db = "hadoopdb";

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        return DriverManager.getConnection("jdbc:hive2://" + host + ":" + impala_port + "/" + db + ";auth=noSasl");
    }

    public static void main(String[] args) {

        System.out.println("\n=============================================");

        System.out.println("start....");
        Connection con = null;

        try {
            con = getConnection();

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery("select * from table_name");
            
            while (rs.next()) {
                System.out.println("-" + rs.getString(1));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
}