Skip to main content

Posts

Showing posts with the label java

Filtering a large (> 2 GB) CSV file?

Are you filtering a large CSV file, typically  > 2 GB? Let's say you have a CSV file larger than 2 GB and you want to filter only the matching rows. First option that comes to our mind is shell script. It's simple and fast. Alright, lets do it. Scope I have CSV file, input.csv with 10.1 million rows The file size is 2.1 GB I need to search for any of the 200 words I have in terms.txt file  I want the matching rows in output.csv, containing any of the 200 search texts #!/bin/bash > output.csv # clear or create output file # Read each search string from search.txt while IFS= read -r search; do # Loop through all CSV files inside the folders find . -type f -name "*.csv" | while IFS= read -r csv; do # Search for the string in the CSV file and append matching rows grep -iF -- "$search" "$csv" >> output.csv done done < terms.txt   👏Cool. It completed in 5:49 mins. Problem solved! Wait.🤔 Im supposed to get only 1...

Configuring and using Keycloak

In this article we will see how to use Keycloak , one of the popular SSO software. Here is the excerpt from the official website. Integrated SSO and IDM for browser apps and RESTful web services.  Built on top of the OAuth 2.0, Open ID Connect, JSON Web Token (JWT) and SAML 2.0 specifications.  Keycloak has tight integration with a variety of platforms and has a HTTP security proxy service where we don't have tight integration.  Options are to deploy it with an existing app server, as a black-box appliance, or as an Openshift cloud service and/or cartridge. Installation Download the software from   here . Let's use  keycloak-1.4.0.Final   for the purpose of demonstration in this article and we will try to set it as a standalone server. To install first download either the zip or tar.gz and extract. Then start by running either: keycloak-1.4.0.Final/bin/standalone.sh or: keycloak-1.4.0.Final/bin/standalone.bat That's it. you are do...

How to execute a singleton task in a cluster?

Have you come across a situation where you wanted a singleton to execute a task in a cluster of servers (such as running a clean up task every day, sending a report everyday etc)? You know that these type of tasks should be executed by exactly only one of many servers you have. Here you go. Following simple program will let you do that exactly. import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.jgroups.Address; import org.jgroups.JChannel; import org.jgroups.ReceiverAdapter; import org.jgroups.View; import org.springframework.stereotype.Component; /** * * @author Pragalathan M */ @Component public class ClusteredSingletonManager { private JChannel channel; private Address ownAddress; private static final Logger logger = Logger.getLogger(ClusteredSingletonManager.class.getName()); @PostConstruct private void init() { try { logger.info(...

Transform Object into XML using JAXB (Part 2)

In one of the posts  we had seen how to convert Java objects into XML. This was done using JAXB and XSD .  What if you don't like to write XSD by hand. Well, you can use any online or offline tools available, to generate the desired XSD from XML. Most of the times the generated XSD will be nearly accurate and you have to hand edit them to make it 100% perfect. Alright. What if you don't know XSD at all. Is there a simpler way? Yes there is. Here we are going to use a different approach to achieve exactly the same result that we have got in the  old  post. Open Netbeans and create a Java Application File->New Project->Java Application Enter "XMLBinding2" in the Project Name field Click Finish Create the following Java classes Replace the Java files with the following contents appropriately. package xmlbinding2; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrappe...

Permutaion Program

Let's see how we can apply permutation on the following input. a, b,c The expected output is, a b c a c b b c a b a c c a b c b a The logic is simple. Each position p (where p>1) will have p rotations after which the next position is rotated. input: a b c position: 3 2 1 print (a, b, c) rotate b   and print (a, c, b) rotate c ; (a, b, c) count exhausted. position 2 is already rotated 2 times rotate next position which is a and print (b, c, a) rotate c and print (b, a, c) rotate a ; (b, c, a) count exhausted. position 2 is already rotated 2 times rotate next position which is b and print (c, a, b) rotate a and print (c, b, a) rotate b ; (c, a, b) count exhausted. position 2 is already rotated 2 times try rotating next position which is c ;  count is exhausted. position 3 is already rotated 3 times. look for next position no more positions, hence terminate public class Permutation {     public static void main(String[] ar...

Hidden facts about HashMap and HashSet

We all know that hashcode is used when you store objects in map or set. And we all know that the hashcode() method of the object is used to uniquely identify the object stored in the set and that HashSet will not store duplicate values. What do we derive from these? Shall we say, "Objects having same hashcode are considered as same"?! Guess what is the output of the following program. public class HashCodeTest { static class Person { int hashCode = 1; @Override public int hashCode() { return hashCode; } } public static void main(String[] args) { Set persons = new HashSet (); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); System.out.println("Set size: " + persons.size()); Map map = new HashMap (); map.put(new Person(), 1); map.put(new Person(), 2); map.put(new Person(), 3); map.put(new Person(), 4); System.out.pr...

How to inherit an inner class?

Consider the following example, class A { class B { B(int i) { ... } } ... } Sub-classing B inside class A is very simple because both class B and its sub-class, say C, require the instance of class A and can be done as follows. class A { class B { B(int i) { ... } } class C extends B { C(int i) { super(i); ... } } ... } However, if you want to sub-class B outside class A, as follows, class C extends A.B { C(int i) { super(i); ... } ... } you will get a compiler error as "an enclosing instance that contains A.B is required". In order to solve this problem, you need to get an instance of A, as follows, class C extends A.B { C(A a, int i) { a.super(i); } } Here is the compelete code. public class A { class B { int i; B(int i) { this.i = i; } } } class C extends A.B { C(A a, int i) { a.super(i); } public static void main(String[] args) { A a = new A(); ...