Wednesday, July 30, 2014

Linux: Splitting Large File Into Exact Number Of Character Chunks

Just as a NoteToSelf, Below is simple bash command line to create smaller chunks of files with specific number of characters from a huge text file.

  • Source text file is source.txt
  • Number of characters to split: 100
  • Command Line: cntr=1;for chunk in `sed -e 's/.\{100\}/&\n/g' source.txt`; do echo $chunk > chunk$cntr.txt; cntr=$(( $cntr + 1 )); done

Easy peasy

Cheers !!!
- Jay

Monday, April 14, 2014

Ever growing Java's Collection

With every major release of Java you get to see some really really useful collection classes addressing some real needs and use cases. But with this ever growing classes there is always a question which class is the best for your need. In general you can use couple of classes interchangeably. For example you can use ArrayList as well as LinkedList just to store random number of items in a list. But what if you are really dealing with huge list where you need random access to the list? Is ArrayList best? or LinkedList. To address such questions and help choose best collection class for your need, I stumbled upon this nice flowchart the other day while just going through my google+ stream. Found it useful and hence sharing it over here.



Cheers !!!
- Jay

Reference: From ImmaWake's G+ post 

Wednesday, March 6, 2013

Creating large dummy files for test

At times during development we need to create some large files regardless of what data is there inside. For e.g. to see if your program can handle insanely large file or to see how long it takes to transfer a particular size on a network or on your JMS queue etc. For such matter almost all OS provides some nice ways - which sadly I was not aware till now :-(. Anyway here are the different ways you can create such files under different OS

  • Windows:
    • Open the command shell - cmd
    • Execute the following command in order to create a file of 1mb
      fsutil file createnew ./test.tmp 1048576
  • Linux:
    • Use convert and copy command - dd
    • dd if=/dev/zero of=test.tmp bs=1M count=1
  • OS X & Solaris:
    • mkfile 1m test.tmp
Go fill your disk with insanely large files :-)

 Cheers !!!
- Jay

Friday, February 8, 2013

Import Oracle dump with different table space, schema , roles with imp utility

At (bad)times it happens that for a nasty bug happening only in client environment, we receive dump file (exported using Oracle's exp utility) from client or say from different environment where table space, schema and roles are different from the one we use in development environment. In such a case it becomes difficult or impossible to import such dump file using oracle's imp utility. To tackle such situation there is a kind of workaround.

Note: Base assumption over here is you already have a local database in your development environment having the structurally same schema as you wish to import from the dump file. If this is the case below are the steps
  1. Disable all the constraints: Below script will give you ALTER TABLE scripts to disable all the constraints in your schema (the one to which you want to import the data from dump)
    SELECT 'ALTER TABLE'||' '||owner||'.'||table_name||' DISABLE CONSTRAINT '||constraint_name||' ;' FROM dba_constraints WHERE constraint_type = 'R' and owner='<OWNER>' and status = 'ENABLED';
  2. Truncate all the tables: Below script will give you TRUNCATE TABLE scripts for all the tables in your schema (the one to which you want to import the data from dump)
    SELECT 'TRUNCATE TABLE '||OWNER||'.'||TABLE_NAME||' ;' FROM DBA_TABLES WHERE OWNER='<OWNER>';
  3. Import data from the dump: Now you have a clean schema with only structure. Now you need to import ONLY data from the dump. No structure, constraints etc. Below is the script to import only data using imp utility.
    imp <USER>/<PASSWORD>@<SCHEMA_NAME> file=<DUMPFILE_NAME> INDEXES=y full=y data_only=y
  4. Enable all constraints: Below script will give you ALTER TABLE scripts to enable all the constraints in your schema
    SELECT 'ALTER TABLE'||' '||owner||'.'||table_name||' ENABLE CONSTRAINT '||constraint_name||' ;' FROM dba_constraints WHERE constraint_type = 'R' and owner='<OWNER>' and status = 'DISABLED';
  5. Done
Cheers !!!
- Jay

Ref: Got scripts in step 1, 2 and 4 from Sergey. Thanks Sergey

Monday, October 1, 2012

Importing Eclipse Workspace Settings From An Old Workspace

This is a classic problem I guess any java developer or better say any eclipse developer would have faced or is facing every other day. I've been using eclipse as my primary IDE for years and like most of the users, I also have multiple workspaces to manage my code base depending upon the trunk/branch or any particular release I'm working on. Having multiple workspaces makes life so easy and work so hassle free, but it also comes with some "extra overhead" of having consistence settings across the workspace and at time across the instances (home/work). Until sometime back I used to set all the configuration (except formatters) each time I create new workspace and also had to update any settings I change in workspace to another. Recently I found a easy way to have same settings in matter of seconds from an existing workspace to the new workspace. This is what I'm going to show you here in this post. To do so follow the below mentioned simple steps.

  1. Create the new workspace.
  2. Close your eclipse instance having this new workspace open.
  3. Go to your old workspace file system from which you want your preferences Copy the directory <oldworkspaceroot>\.metadata\.plugins\org.eclipse.core.runtime\.settings to your <newworkspaceroot>\.metadata\.plugins\org.eclipse.core.runtime\
  4. Start your eclipse. 
  5. Done!!!
Cheers !!!
- Jay

Tuesday, September 25, 2012

Error: could not open `C:\Program Files\Java\jre6\lib\amd64\jvm.cfg'

In some cases where you are playing around with your JDK/JRE its possible you corrupt your settings or JRE in total. I personally dont like the default JRE installation location on windows i.e. C:\Program Files\ for the fact that there is a space between Program and Files which at times create problems especially when you are using cygwin and writing some linux script. So as a regular practice I always install my JDK/JRE to c:\java. So the other day I got this brand new Windows 7 64bit machine with 8gb RAM and 3.3ghz i5 processor. My system admin was so kind enough that he gave me all the *required* tools already installed for my development. This obviously came with JRE 6 installed in Program Files directory. So I just un-installed it, got new 64bit JDK and JRE from Oracle and installed it under my regular directory i.e. c:\java. Now when I turned to my cygwin terminal and ran java what I see is below mentioned error

Error: could not open `C:\Program Files\Java\jre6\lib\amd64\jvm.cfg'


I checked my environment variables - JAVA_HOMEPATH and they all refer to c:\java. So this was bit frustrating. After sometime I found that the default installation also copied java.exejavaw.exe and javaws.exe to C:\Windows\System32 (i.e. uninstall of JRE didnt went well). I just removed them and voila, I'm back on track. That annoying error is no longer popping.

Cheers !!!
- Jay

Sunday, December 4, 2011

WordPress on OS X : Error establishing connection mysql

Recently I was trying to install/configure WordPress on OS X (Lion) with MySql 5.5. After reading the installation guide, I did not expect anything to go wrong, but as it goes without saying, theory is totally different from practical, I did face issue. After setting wp-config.php, when I hit the /wordpress/wp-admin/install.php, I got a nice page saying Error establishing a database connection. Obvious reason for this would be (as mentioned on the error page) would be the wrong user/password or wrong host or wrong database name. I checked all parameters and none were wrong and I was also able to connect to MySQL database which I created for WordPress with MySQL WorkBench and also from mysql command line.

To verify my settings, I just created and a test php page to test if I can connect to my newly create WordPress database. I was not able to connect and got some more details about no connection. I got following wraning

ErrorException [ Warning ]: mysql_connect() [function.mysql-connect]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)
Obviously when I checked /var/mysql/mysql.sock, I could not find it. So next step was to find where is my missing mysql.sock and found it under /tmp. After googling a lot, I could not find any concrete solution to resolve this. Niether there is anything which I can change for MySQL configuration nor for the PHP configuration. So my last resort and the easy solution was creating a symbolic link. I just created a symbolic link on /var/mysql to point to /tmp (sudo ln -s /tmp /var/mysql) and hit the refresh on WordPress installation page and everything was just smooth. Happy blogging :-)

 Cheers !!!
- Jay
 
Disclaimer : This is a personal blog and all content represent what I think and it does not advocate/support/advertise any other person/company. I do not earn money or intended to do so with this blog or any of the contents the blog hosts (except the google ads which you see). If I post something here that you find helpful, that's wonderful. Just in case, if I say something stupid, the stupidity is mine, and mine alone and I can not be held for anything if you fall for such stupidity :-). I cannot be held responsible for any kind of damage that may be caused by downloading or viewing the files or information provided herewith. Anybody and everybody can use/refer the contents of this blog at their own will and of course at own risk. There is no need for any kind of approval of the author. Although it would be great if feedback is left for any such usage to the author.