Skip to main content

Posts

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...

Using Nginx as proxy server for Keycloak

I have used Keycloak  in its very early stage ( when it is was in 2.x version). But now it has come a long way (at this time of writing it is in 21.x) In this article let's configure Keycloak behind Nginx. Here are the points to consider.  If you want to configure Apache2 as a proxy server for your java application, please check  this article . We are going to use a domain name other than localhost Anything other than localhost will require Keycloak to run in production mode which requires SSL configurations etc. Or it requires a proxy server. Lets begin. Requirements Keycloak distribution Ubuntu 22.04 server Configuring Keycloak 1. Download Keycloak from here . 2. Extract it using tar -xvzf  keycloak-21.0.1.tar.gz 3. Create a script file called keycloak.sh with the following contents #!/bin/bash export KEYCLOAK_ADMIN=<admin-username-here> export KEYCLOAK_ADMIN_PASSWORD=<admin-password-here> nohup keycloak-21.0.0/bin/kc.sh start-dev --proxy edge --hos...

How to compile Electron from source (for arm64 / x64)?

Sometimes if prebuilt binaries of electron are not available for your platform or if you want to modify the electron source to customize to your need, you are at the right place. I had a requirement to have a <webview> tag inside another <webview> tag. Seems it is not supported out of the box. As per the suggestion provided in the above link, I thought I could modify a couple of lines and see if it works. Coincidentally I am running Kubuntu arm64 build on M1 mac via parallels. So I wanted a aarch64 (a.k.a arm64 ) build. So I provisioned a  t4g.medium (2vCPU , 4 GB RAM)   with Ubuntu 22.04 LTS.   I tried checking out the code as mentioned below. But within 30 mins I ran out of CPU credits and had to switch to   r6g.large (2vCPU , 16 GB RAM) . Code was checked out pretty fast (fast for 50GB of code). Yes you will need at least 60GB of disk. Ok, lets go through the steps for checking out and compiling. Step-1: Prepare the build environment su...

How to use Groovy + GraalVM + Quarkus together?

If you are one among those who like Groovy for its simplicity and tight integration with Java (and not for its dynamic capabilities) and wondered how to write Groovy code in a Quarkus application and compile it to native using GraalVM? I have you covered. You dont have to look anywhere else. Scope groovy code as part of quarkus application statically compilable groovy code with no dynamic feature compile the final application to native code using graalvm Code example can be found here . Steps Create a quarkus application as outlined here Add your groovy class under src/main/java Annotate your groovy class with @groovy.transform. CompileStatic create a file called application.properies under src/main/resources folder with the following content. quarkus.native.additional-build-args=\ --allow-incomplete-classpath, \ --report-unsupported-elements-at-runtime, \ --initialize-at-build-time, \ --initialize-at-run-time=org.codehaus.groovy.control.XStreamUtils\\,groov...

How to retry a method call in Spring or Quarkus?

Have you ever come across a situation where you wanted to retry a method invocation automatically? Let's say you are calling a stock ticker service for a given stock and get a transient error. Since it is a transient error, you will try again and it may work in second attempt. But what if it doesn't? Well, you will try third time. But how many times can you try like that? More importantly after how much time will you retry? Imagine if you have a handful of methods like this. Your code will become convoluted with retry logic. Is there a better way? Well, if you are using spring/spring boot, you are in luck. Here is how you can do that using spring. Let's write our business service as follows. import java.time.LocalDateTime; import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Retryable; import org.springframework.scheduling.annotation.Async; import...

HTTP/2 Java Server using Embedded Jetty - Part 2

In the last post we saw how to spin a http/2 server using embedded jetty. In this post we will see, how to use asynchronous servlet (as well as AIO a.k.a non-blocking IO) and why that is important? Asynchronous Servlets ≠ Asynchronous IO [1] The concept of Asynchronous Servlets is often confused with Asynchronous IO or the use of NIO. However, Asynchronous Servlets are not primarily motivated by asynchronous IO, since: HTTP Requests are mostly small and arrive in a single packet. Servlets rarely block on requests. Many responses are small and fit within the server buffers, so servlets often do not block writing responses. Asynchronous Servlets Use-case The main use-case for asynchronous servlets is waiting for non-IO events or resources. Many web applications need to wait at some stage during the processing of a HTTP request, for example: waiting for a resource to be available before processing the request (e.g., thread, JDBC Connection) waiting for an applica...

HTTP/2 Java Server using Embedded Jetty - Part 1

Have you ever wanted to configure internal Java servers using HTTP/2 (or H2 in short), because of the following advantages? Internal clients making concurrent calls, can now use a single connection by utilizing the multiplexing nature of HTTP/2. Server will/can use less threads as there are less connections overall. Servers can also push resources/data and can communicate using websockets But what if you don’t want to go through tedious process of setting up SSL/TLS certificates? HTTP/2 insists on SSL/TLS as no browser supports HTTP/2 on insecure mode. Or what if you want to save every bit of CPU and time by skipping encryption and decryption? You are at the right place. Since everyone nowadays is moving towards (or talking about) micro services, we will also configure Jetty HTTP/2 server in embedded mode. What more? We are also going to configure our server to use Asynchronous Servlets and Asynchronous IO (aka non-blocking IO or NIO) in Part-2 of this article. ...

Using Apache as (SSL) proxy server

If you are a using any Java web server (such as Tomcat) or app server (such as Wildfly, JBoss or Glassfish), then you know that they dont listen on http port 80. Well, why does it have to listen on port 80? because if you want your user to specify only the url and not the port; for example, http://mydomain.com and not http://mydomain:8080 Note: throughout this article mydomain.com can be replaced with localhost, if your product is in development stage. Software used: Ubuntu apache2 Any Java web/app server What we want to achieve We are going to divert all user requests coming on port 80 to Tomcat running 8080 on the same host. Installation Run the following commands sudo apt-get install apache2 sudo apt-get install libxml2-dev sudo apt-get install libapache2-mod-proxy-html Note: If your ubuntu OS is latest, then you may not have libapache2-mod-proxy-html package. Instead run the following command. sudo a2enmod proxy_html Configuration 1. Append t...

How to install (K)Ubuntu alongside Yosemite and Windows 8 on Mac Book Pro?

Here let's see how we can install Windows 8 and (K)ubuntu 15.10 (Wily Werewolf) along side Mac Yosemite as triple boot. Software/Hardware required Mac book Pro 11,1 (instruction might be similar for newer models) Yosemite Windows 8 (K)Ubuntu 15.10  Wily Werewolf (might work with 15.04 as well) Refind (not reEFIt) Here is the summary of what we are going to do. Partition mac to have windows boot-camp partition  Install windows 8 Create two new partitions from mac partition for Linux Install (K)ubuntu Fix GPT-MBR Install Refind Step-0 In case you want to backup your mac:  Restart mac and hold  This will boot Mac in recovery mode as shown below    Plugin an external drive and choose Disk Utility option Select the mac partition (not your hard disk root) and go to restore tab Choose your external drive as the destination as shown below Important: ensure that your mac partition is the Source Click Restore to take a cop...

Installing GoDaddy certificate in Wildfly/Keycloak

In the previous post we saw how to set up Keycloak . Here we will see how to generate and install GoDaddy.com certificate in Keycloak. The steps are similar for Wildfly as well. Step 1: Generate CSR file Run the following commands in your terminal. <mydomain.com> has to be replaced with your actual domain name. keytool -genkey -alias mydomain_com -keyalg RSA -keysize 2048 -keystore mydomain_com.jks keytool -certreq -alias mydomain_com -file mydomain_com.csr -keystore mydomain_com.jks Step 2: Generate certificate Upload  mydomain_com . csr  file content into GoDaddy.com, generate and download certificate for tomcat server (steps to generating SSL certificate is beyond the scope of this article). If you unzip the file, you will see the following files. gd_bundle-g2-g1.crt ..5f8c...3a89.crt   #some file with alphanumeric name gdig2.crt Files 1 and 2 are of our interest. Third file is not required. Step 3: Import certificate to key...