Tuesday, December 21, 2021

How to kill a process that his waiting or Listening in a port.

> netstat -o -n -a | findstr 0.0:8080

 TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       6476

> taskkill /F /PID 6476

Link:
http://stackoverflow.com/questions/6204003/kill-a-process-by-looking-up-the-port-being-used-by-it-from-a-bat

Wednesday, February 7, 2018

Oracle query to display the string with comma separated value as query row results. With no use of Table creation


There was a situation in my project where query for set of data (result of the query will provide one field with different value)

For example,
Employee table, and fetch only the employee last name.

Now i wanted to test an fix in the application code, where the query was present in the properties file.
Properties file had

query.employee=select distinct employee_firstName from Employee
There was no logical change in code. For testing i need to provide the query with constant values.

For example, for customer name i had data like below

   john1,john2,john3

SELECT REGEXP_SUBSTR (
         'john1,john2,john3',
         '[^,]+',
         1,
         LEVEL)
         val
FROM DUAL
CONNECT BY REGEXP_SUBSTR (
             'jonh1,jonh2,jonh3',
             '[^,]+',
             1,
             LEVEL)
             IS NOT NULL;
The result of the above query will be a Row of values.

john1
john2
john3

Comma separated string transformed to a column of data, without any table.

The RegExp_substr function in oracle has a size limitation if we use more than certain values.
so in that case i was about to use union

SELECT REGEXP_SUBSTR ('1,2,3','[^,]+',1,LEVEL)val FROM DUAL CONNECT BY REGEXP_SUBSTR ( '1,2,3', '[^,]+', 1, LEVEL) IS NOT NULL
union
SELECT REGEXP_SUBSTR ('4,5,6','[^,]+',1,LEVEL)val FROM DUAL CONNECT BY REGEXP_SUBSTR ( '4,5,6', '[^,]+', 1, LEVEL) IS NOT NULL
union
SELECT REGEXP_SUBSTR ('7,8,9','[^,]+',1,LEVEL)val FROM DUAL CONNECT BY REGEXP_SUBSTR ( '7,8,9', '[^,]+', 1, LEVEL) IS NOT NULL
Result of the above query is
1
2
3
4
5
6
7
8
9

There are other ways of achieving this result, in my case i had around 1000 string with comma separated values. For a quick turnaround time the above approach worked.

we can use With query for the same too.









Useful Linux/Unix commands


Below command can be used for a scenario where grep command returns the searching string.

$  grep -H "some text " * | awk -F: '{print $1 }' | wc -l

-H  => as per the Linux man page, displays the file name.
some text => is the text that needs to be searched, double quotes used since there is space between the search string.
* => represents all the files in that folder or where the $ pwd command display value.

-F: => delimits using ":" on the output of grep command.

wc -l => word comm
grep output

file.txt:<some text ....

output of command  < grep -H "some text " * | awk -F: '{print $1 }' >
file.txt

----------------------------------------------

Below command can be used for a scenario where you need to move the files in the directory (Linux) to different location

$ for i in `ls -ltr | grep Jan | awk '{print $9}' `; do mv $i* ../to_delete/; done
Use: ls - lrt command, output is as follows

       -rw-rw-r--. 1 user1 root 2356 Jan  7 12:25 file.xml
       -rw-rw-r--. 1 user1 root 2356 Feb  7 12:25 file.xml

grep Jan => <month> Ls command output format will filter the first record in the above sample.

when delimited with space, the file name is on 9 th position.
awk '{print $9} => provides the file name.

mv $i is the index of the for loop, which is the file name actually.

---------------------------------------------
Below command can be used to print the size of the file in Megabits or Kilobits

$ ls -lr --block-size=M
$ ls -lhS
-h => human readable size format
-S => sort the size

refer man ls page for more details

--------------------------------------------

Tuesday, January 16, 2018

difference between java ArrayList<> add and set method

Below is the example of how the .add and .set method of ArrayList works when using the index & value attribute.

add(int index, E element)
Inserts the specified element at the specified position in this list.
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

set(int index, E element)
Replaces the element at the specified position in this list with the specified element.


import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
        List<String> listStr = new ArrayList<>();
        listStr.add("1 ");
        listStr.add("2 ");
       listStr.add("3 ");
        listStr.add("4 ");
        listStr.add("5 ");
        listStr.add("6 ");
        listStr.add("7 ");
        listStr.add("8 ");
       
        listStr.forEach(System.out::print);
        List<String> listStr1 =  listStr.stream().collect(Collectors.toList());
        System.out.println("\n *** ADD **** ");
        addAtIndex(listStr1, 5, new String("NewIndxVal "));
        listStr1.forEach(System.out::print);
        System.out.println("\n *** SET **** ");
        List<String> listStr2 = listStr;
         listStr2.forEach(System.out::print);
         System.out.println("");
        setAtIndex(listStr2, 5, new String("NewIndxVal "));
        listStr2.forEach(System.out::print);

     }
    
     public static <T> List<T> addAtIndex (List<T> strList, int idx, T value){
        
         strList.add(idx,value);
        
         return strList;
     }
      public static <T> List<T> setAtIndex (List<T> strList, int idx, T value){
        
         strList.set(idx,value);
        
         return strList;
     }

}

output:
Hello World
1 2 3 4 5 6 7 8 
 *** ADD **** 
1 2 3 4 5 NewIndxVal 6 7 8 
 *** SET **** 
1 2 3 4 5 6 7 8 
1 2 3 4 5 NewIndxVal 7 8 

Tuesday, June 6, 2017

Oracle writing the dbms_ouput.put_line to text file from pl/sql block



spool works, the output.txt file creates the file where the pl/sql block is saved.

worked in Toad.

In order to increase the buffer size of dbms_ouptut use dbms.output.enable(NULL) whenenver needed within the pl/sql block.


spool output.txt
set serveroutput on

begin
  SELECT systimestamp FROM dual INTO time_db;
  DBMS_OUTPUT.PUT_LINE('time before procedure ' || time_db);
end;
/

spool off


Reference:
Courtesy
https://stackoverflow.com/questions/1453538/how-to-redirect-the-output-of-dbms-output-put-line-to-a-file





Wednesday, September 7, 2016

Replicating and fixing ORA-01775 looping chain of synonyms

Steps to replicate this exception

1. Create a public synonym for a table (which is not created in the schema)

CREATE OR REPLACE PUBLIC SYNONYM TEMP_TABLE FOR THIS_SCHEMA.TEMP_TABLE;

2. Issue a commit;

3. Try to Grant to the table TEMP_TABLE
GRANT DELETE, INSERT, SELECT, UPDATE ON THIS_SCHEMA.TEMP_TABLE TO READONLY_SCHEMA;

The above query will report missing table in the scehma.

4. Now execute the select table query.
  SELECT * FROM temp_table;

Reports

ORA-01775 looping chain of synonyms

To fix the issue:

Just create the table "TEMP_TABLE" using the create table query.

Wednesday, February 17, 2016

Setting up SFTP at Source and Destination server - password less connection.



Assumption for SFTP setup


Needed to copy a file from SERVER2 USER2 into SERVER1 USER1

1. Log in to SERVER1 USER1, create a .ssh directory if it doesn’t exists. Issue chmod 700 .ssh, to make sure that folder permission is restricted only by the owner.
2. Navigate to the .ssh folder, issue below command which will create a public/private key pair
$ ssh-keygen –t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
c1:21:e 3:01:26:0d:f7:ec:52:0e:0c:90:9b:6e:d8:47 user1@server1.com

The key's randomart image is:

+--[ RSA ....]----+
..
Note:
If we need to connect to the server automatically issuing the sftp command, don’t provide any passphrase. (just hit enter)
If we need the public/private key to be generated to a different file, provide a file name when Enter file in which to save key is prompted.

3. After issuing the ssh-keygen command, two files would be generated in the local folder id_rsa and id_rsa.pub.

Share the id_rsa.pub (public key) to the SERVER2 USER2, to which the server is going to connect.
1. Login to SERVER2 USER2, check if the .ssh folder exists. If not, create .ssh folder and change permission to 700 using the chmod 777 .ssh command.

2. Navigate to .ssh folder, check if there is any file named authorized_keys, if there is no file create it and add the id_rsa.pub content to it.

3. If authorized_keys file doesn’t exists, create one and provide 600 permission to the file using chmod 600 authorized_keys.

4. Copy the content of id_rsa.pub to the authorized_keys, just using the cp command.

In SERVER1 USER1, now issue the below command, you will be logged in to the server without any password prompt
                $ sftp user2@server2.com
                …
                sftp >
Link:
https://askleo.com/how_can_i_automate_an_sftp_transfer_between_two_servers/

Exception that may occur:
Authentication refused: bad ownership or modes for directory /xxx/yyy
Solution:
Ensure the permission of the folder .ssh (700) and authorized key (600).
In my case the folder xxx was created by a different userZ and the yyy was created by different user2.
After making the folder xxx to the user2, the issue got resolved.

More information for debugging can be found in the log location /var/log/secure (Redhat 6.6)