Sunday, November 16, 2014

Java Performance Analyser

Occasionally I get "Out Of Memory" error in my Apache Tomcat and with out thinking too much I will just bounce the server to solve the problem. I was searching for the same in Google and it took me to a blog post http://opcodesolutions.com/tech/solve-java-lang-outofmemoryerror-java-heap-space/ in which I found an interesting tool Visual VM.
My goal is not to re-produce every detail about this tool, Name says it all.
http://visualvm.java.net/
I just downloaded it and got up and running in seconds. I have not yet started playing with the tool, but I believe this will solve some of my Java Memory issues.
You can go through the screen cast of this tool from this page.
http://visualvm.java.net/gettingstarted.html

Saturday, November 15, 2014

Connecting to Oracle from KDB+ (Using Babel)

I have recently started messing around KDB+ and playing with cryptic Q language.

I want to compare the performance of Oracle vs KDB+, and in order to do that I have to duplicate the Oracle table in KDB+. I tried through ODBC layer but didn't work. Then I found another elegant solution posted on the code.kx.com site using Babel.
http://code.kx.com/wiki/Babel
"Babel for kdb+ is a SQL gateway process that allows kdb+ to query other vendors databases via jdbc."
Babel act as a broker between KDB+ and any database that can be connected through JDBC library. Meaning KDB knows how to talk to Babel and Babel knows how to talk to Databases through JDBC Layer. Now lets get into details.


Step 1:
Download Babel:  wget https://github.com/CharlesSkelton/babel/raw/master/dist/babel.zip
and unzip it.

Step 2:
Download ojdbc7.jar from oracle and copy it to bin directory under Babel. Note: odbc7 is for JDK 1.7 and if your Java version (java -version) is 1.6 you should use odbc6.jar.
http://www.oracle.com/technetwork/database/features/jdbc/default-2280470.html
Update: If you are using Java 8/JDK1.8 you can download ojdbc8 from here
http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html
Update: For SQL Server use this thread
https://stackoverflow.com/questions/5307048/where-do-i-install-a-jdbc-driver-on-ubuntu


Step 3: Start the babel on port 6868

$ java -Xmx1024m -Doracle.jdbc.defaultRowPrefetch=10000 -cp "babel.jar:lib/ojdbc7.jar" de.skelton.babel.Babel 6868 oracle.jdbc.OracleDriver

For SQL Server
java -Xmx1024m -cp "babel.jar:sqljdbc42.jar" de.skelton.babel.Babel 6868 com.microsoft.sqlserver.jdbc.SQLServerDriver


Step 4:
Start q (I assume you already have working instance of q). If not you should first learn about q from http://code.kx.com

q)h:hopen 6868
q)r:h("query";"jdbc:oracle:thin:scott/tiger@your_oracle_server:1521:orcl";"select * from emp");


PS: You know Scott/Tiger is user id/ password and ORCL is the Oracle instance.




More References:


Thursday, October 23, 2014

Extracting Image meta data using Oracle ORDSYS.Image

Let's say you have a table "PICTURE" has a blob column "PIC_BLOB" which contains some image and you want to extract the meta data of the image, here is the query,

Don't ask me any more questions. I spilt my hair to get it working.

PS: If you are asking me why did I do it. Because Photos taken with iPhone are rotated randomly and learned that, rotation angle is stored in the meta data


select VALUE(xml_type).getstringval() 
FROM table(
    select ORDSYS.ORDImage.getMetadata(
                ordsys.ordimage(ordsys.ordsource( PIC_BLOB, null, null, null, null, 1),null, null, null, null, null, null, null)
            )
      from PICTURE WHERE ID=1
)xml_type

Your Output will be something like this.

<ordImageAttributes xmlns="http://xmlns.oracle.com/ord/meta/ordimage" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/ord/meta/ordimage http://xmlns.oracle.com/ord/meta/ordimage"></ordImageAttributes>
<iptcMetadata xmlns="http://xmlns.oracle.com/ord/meta/iptc" xsi:schemaLocation="http://xmlns.oracle.com/ord/meta/iptc http://xmlns.oracle.com/ord/meta/iptc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

<exifMetadata xmlns="http://xmlns.oracle.com/ord/meta/exif" xsi:schemaLocation="http://xmlns.oracle.com/ord/meta/exif http://xmlns.oracle.com/ord/meta/exif" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <TiffIfd>
      <Orientation tag="274">top left</Orientation> -- This is what I am interested in
   </TiffIfd>
   <ExifIfd tag="34665">
      <ColorSpace tag="40961">sRGB</ColorSpace>
      <PixelXDimension tag="40962">3264</PixelXDimension>
      <PixelYDimension tag="40963">2448</PixelYDimension>
   </ExifIfd>
</exifMetadata>


Tip:If you are just interested in Orientation tag then apply Regular Expression like this
replace
VALUE(xml_type).getstringval() 
with
 max(REGEXP_SUBSTR(
               VALUE(xml_type).getStringval(),'<Orientation (.*)>(.*)</Orientation>',1,1,'i',2))

Sunday, February 9, 2014

Setting up automatic Wireless Connection for Raspberry Pi

Well, like many others I also bought Raspberry pi (I think a year ago) but didn't do much with it. I decided to do a Garage door opener project and bought Relay & USB Wireless adapter (http://www.amazon.com/gp/product/B003MTTJOY/ $9.99). I started fresh with Rpi new install from http://www.raspberrypi.org/downloads and used "NOOBS (offline and network install)" to install Raspbian OS. Then enabled remote SSH so that I can connect to it from my Mac and I can use the USP ports for attaching Wireless receiver. I have used tightvncserver & tightvnc viewer java client to RDP into Rpi.

From there launch "WiFi Config", and select "scan".



 It will list all available networks and from that select your network and "Double Click" on it. It will bring another window with settings preselected, you just need to enter the password and hit "add"




This will automatically save your wireless network profile to "/etc/wpa_supplicant/wpa_supplicant.conf" file.
You can as well list the contents using 
$sudo cat /etc/wpa_supplicant/wpa_supplicant.conf

--------------------------------------------------
pi@raspberrypi ~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="Your SSID Name"
psk="Your SSID Password"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}
-------------------------------------------------------------------

At this time, you can unplug your ethernet cable and still you will be able to access internet through wifi adapter. However if you restart your Rpi, your Wifi connection will not start automatically.
Follow the steps below to enable the wireless connection to startup automatically.


Note: You can't edit interfaces file directly since it is owned by root. You have to use sudo for the same

Either use nano or vi editor to match the content similar to mine

pi@raspberrypi ~ $sudo nano /etc/network/interfaces
or
pi@raspberrypi ~ $sudo vi /etc/network/interfaces
-------------------------------------------------------------------------
auto wlan0

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
-------------------------------------------------------------------------

Next edit wpa_supplicant.conf in the sameway

pi@raspberrypi ~ $sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
or
pi@raspberrypi ~ $sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

-------------------------------------------------------------------------
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="Your Network SSID"
psk="Your Network Password"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}

-------------------------------------------------------------------------
Note: proto, keymgmt can be different based upon your network security.
You can use the following blog for that reference, it's kind of the same blog, but didn't work for me.

Now you can restart your pi and enjoy it.

PS: I have used the following blog to setup my wifi, but neither did work.
Hence wrote this blog to show what worked for me.