Script to delete all tables in Hive

2015年12月28日 09:50

hive -e 'use xxx;show tables' | xargs -I '{}' hive -e 'use xxx;drop table {}'

继续阅读 »

How to check visibility of software keyboard in Android

2015年7月17日 09:25

In AndroidManifest.xml

        <activity
            android:name=".activity.LoginActivity"
            android:label="@string/app_name" 
            android:windowSoftInputMode="adjustResize"
            >
        </activity>

1.Use onMeasure() method

This method have problem on Sumsung S4. The onMeasure() Method will run three times after the keyboard appear. But only the second time run result is right, the last run result is : the size of the window no changed. So the onShow() method will not run.

//define custom layout
//LoginLayout.java
package com.pxdl.carsystem.layout;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

public class LoginLayout extends RelativeLayout {
    public LoginLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoginLayout(Context context) {
        super(context);
    }

    private OnSoftKeyboardListener onSoftKeyboardListener;

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        if (onSoftKeyboardListener != null) {
            final int newSpec = MeasureSpec.getSize(heightMeasureSpec); 
            final int oldSpec = getMeasuredHeight();
            if (oldSpec > newSpec){
                onSoftKeyboardListener.onShown();
            } else {
                onSoftKeyboardListener.onHidden();
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public final void setOnSoftKeyboardListener(final OnSoftKeyboardListener listener) {
        this.onSoftKeyboardListener = listener;
    }

    public interface OnSoftKeyboardListener {
        public void onShown();
        public void onHidden();
    }
}

//activity.java
ly_main.setOnSoftKeyboardListener(new OnSoftKeyboardListener() {
        @Override
        public void onShown() {
            hideImgIcon();
        }
        @Override
        public void onHidden() {
            showImgIcon();
        }
    });

2.Simpler method Use OnGlobalLayoutListener

//activity.java
ly_main.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = ly_main.getRootView().getHeight() - ly_main.getHeight();
                if (heightDiff > 200) { // if more than 200 pixels, its probably a keyboard...
                    hideImgIcon();
                }else{
                    showImgIcon();
                }
             }
        });

继续阅读 »

pptp forword setting

2015年5月30日 09:04

1. 设置允许转发
sysctl -w net.ipv4.ip_forward=1 (这步一般文档都有)
2. 设置ppp0 -> eth0的跳转
iptables --insert FORWARD 1 --source 192.168.0.0/24 --destination 0.0.0.0/0.0.0.0 --jump ACCEPT

测试:

tcpdump -n -i ppp0 icmp and src host 192.168.0.234 and dst host 8.8.8.8
3. 设置Target-> eth0 的返回
iptables --table nat --append POSTROUTING --out-interface eth0 --jump MASQUERADE

测试:

tcpdump -n -i eth0 icmp and dst host 8.8.8.8
4. 设置ppp0 -> eth0 的返回
iptables --insert FORWARD 1 --source 0.0.0.0/0.0.0.0 --destination 192.168.0.0/24 --jump ACCEPT --out-interface ppp0

测试:

tcpdump -n -i eth0 icmp and src host 8.8.8.8

继续阅读 »

respond_to_missing?

2015年5月05日 10:59

method_missing problem

class StereoPlayer
  def method_missing(method, *args, &block)
    if method.to_s =~ /play_(\w+)/
      puts "Here's #{$1}"
    else
      super
    end
  end
end

p = StereoPlayer.new
# ok:
p.play_some_Beethoven # => "Here's some_Beethoven"
# not very polite:
p.respond_to? :play_some_Beethoven # => false

userespond_to?

class StereoPlayer
  # def method_missing ...
  #   ...
  # end

  def respond_to?(method, *)
    method.to_s =~ /play_(\w+)/ || super
  end
end
p.respond_to? :play_some_Beethoven # => true

This is better, but it still doesn’t make play_some_Beethoven behave exactly like a method. Indeed:

p.method :play_some_Beethoven
# => NameError: undefined method `play_some_Beethoven'
#               for class `StereoPlayer'

Ruby 1.9.2 introduces respond_to_missing? that provides for a clean solution to the problem. Instead of specializing respond_to? one specializes respond_to_missing?. Here’s a full example:

class StereoPlayer
  # def method_missing ...
  #   ...
  # end

  def respond_to_missing?(method, *)
    method =~ /play_(\w+)/ || super
  end
end

p = StereoPlayer.new
p.play_some_Beethoven # => "Here's some_Beethoven"
p.respond_to? :play_some_Beethoven # => true
m = p.method(:play_some_Beethoven) # => #<Method: StereoPlayer#play_some_Beethoven>
# m acts like any other method:
m.call # => "Here's some_Beethoven"
m == p.method(:play_some_Beethoven) # => true
m.name # => :play_some_Beethoven
StereoPlayer.send :define_method, :ludwig, m
p.ludwig # => "Here's some_Beethoven"

继续阅读 »

Linode VPN

2014年12月16日 23:33

sudo iptables --insert FORWARD 1 --source 192.168.0.0/24 --destination 0.0.0.0/0.0.0.0 --jump ACCEPT

继续阅读 »

Rails 4: how to use $(document).ready() with turbo-links

2014年11月14日 14:20

CoffeeScript:

ready = ->

  ...your coffeescript goes here...

$(document).ready(ready)
$(document).on('page:load', ready)

Javascript

var ready;
ready = function() {

  ...your javascript goes here...

};

$(document).ready(ready);
$(document).on('page:load', ready);

继续阅读 »

Resetting a lost MySQL root password

2014年8月05日 20:10

Stop MySQL

Safe mode

mysqld_safe --skip-grant-tables &

Login

mysql -u root
use mysql;

Reset Password

update user set password=PASSWORD("mynewpassword") where User='root';
flush privileges;

继续阅读 »

How to extract tar xz in Linux

2014年8月01日 13:24

First decompress the file with the appropriate xz command

unxz linux-2.6.32.61.tar.xz

Then

tar xvf linux-2.6.32.61.tar

Note: If not found unxz command, you can try.

cd /usr/local
wget http://tukaani.org/xz/xz-5.0.5.tar.gz
tar xzvf xz-5.0.5.tar.gz
**don't forget README file
./configure
make -C po update-po
make install

继续阅读 »

Monit tomcat run or not on Windows with Java

2014年1月21日 14:36

StreamGobbler.java
package cn.sotips.www;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
class StreamGobbler extends Thread
{
    InputStream is;
    String type;

    StreamGobbler(InputStream is, String type)
    {
        this.is = is;
        this.type = type;
    }

    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                System.out.println(type + ">" + line);    
            }
        } catch (IOException ioe){
            ioe.printStackTrace();  
        }
    }
}

继续阅读 »

Monit tomcat run or not on Linux

2014年1月21日 14:28

#!/bin/bash  
#  
# Keep watch at tomcat's status,   
# automatically restart it if it dead or out of memory. 

tomcat_port=":80";
tomcat_port_pattern="/:80$/";
tomcat_base_dir="/share/tomcat80";
check_page_url=http://127.0.0.1/LotteryHome3/run.jsp;
pid_file_pattern="tomcat80";
guarder_dir="/share/monitor";
log_file="/share/monitor/tomcat_run_log.log";
d=$(date +%F" "%T);

#email config
to=your_phone_num@139.com

#139mail can send a short message to me
#but the short message just display subject
mail_to(){
  echo "$1" | mail -s "$1" $to
}

#init log file
touch ${log_file}

do_restart_tomcat(){
  # first try to shutdown it anyway
  ${tomcat_base_dir}/bin/shutdown.sh

  # second, check if the tomcat pid still exist,if exist then kill it!
  if ps -ef | grep ${pid_file_pattern} | grep -v grep
  then
    kill -9 $(ps -ef | grep ${pid_file_pattern} | grep -v grep | awk '{print $2}')
  fi

  #run start tomcat
  ${tomcat_base_dir}/bin/startup.sh
  echo "$d success restart tomcat ,the new pid id is " >> ${log_file}
  ps -ef | grep ${pid_file_pattern} | grep -v grep | awk '{print $2}' >> ${log_file}
  echo >> ${log_file}
  mail_to "$d success restart tomcat"
}

# first, check if the tomcat si listen on the port
runingcount=$(netstat -ant | awk "\$6 == \"LISTEN\" && \$4 ~ $tomcat_port_pattern" | wc -l)
if [ "$runingcount" == "1" ];
then
  #init the check result file
  if [ -e ${guarder_dir}/checkResult.tmp ] 
  then
    cat /dev/null > ${guarder_dir}/checkResult.tmp
  else
    touch ${guarder_dir}/checkResult.tmp
  fi

  # try to get the check result
  wget -b -o wget.log -O ${guarder_dir}/checkResult.tmp ${check_page_url}
  # wait 5 second to let the get check result job done.
  sleep 5

  # check the result
  workflag=$(cat ${guarder_dir}/checkResult.tmp | grep ServerStillWorking)
  memoryflag=$(cat ${guarder_dir}/checkResult.tmp | grep LessOfMemory)

  if [ "$workflag" == "" ]; then
    echo "$d can not found [ServerStillWorking] in the check result, try to restart tomcat ......" >> ${log_file}
    do_restart_tomcat
  elif [ "$memoryflag" == "" ]; then
    echo "$d can not found [LessOfMemory] in the check result, the tomcat server may out of memory, try to restart it ......" >> ${log_file}
    do_restart_tomcat
  else
    echo "$d tomcat$port is running ......" >> ${log_file}
  fi
else
  echo "$d found the tomcat not listen on ${tomcat_port}, try to restart it ......" >> ${log_file}
  do_restart_tomcat
fi

继续阅读 »