Sunday, November 7, 2021

Move docker-desktop-data out of Windows 10 installed drive

By default Docker Desktop will create two distros as below at %LOCALAPPDATA%/Docker/wsl

  1. docker-desktop
  2. docker-desktop-data

This folder will be used to store image and so on. Hence, its size will be increased in the future, which result in our Windows installed drive out of space. I will move it to E drive.

Below steps will move docker-desktop-data out of the Windows installed drive.

Open command prompt in Windows 10 or Powershell:

1. Stop Docker

wsl --shutdown

2. Export docker-desktop-data to tar file

wsl --export docker-desktop-data E:\docker-desktop\docker-desktop-data.tar

3. Unregister current docker-desktop-data

wsl --unregister docker-desktop-data

4. Import docker-desktop-data from tar file

wsl --import docker-desktop-data E:\docker-desktop\data E:\docker-desktop\docker-desktop-data.tar --version 2

5. Start Docker

start docker

Friday, October 22, 2021

Read Properties file using Java | Three ways

Three ways to read properties file using Java code.

 

1. Lazy Initialization way: Use when needed, read properties file every time.

<<<<<<<
import java.io.FileInputStream;
import java.util.Objects;
import java.util.Properties;

public final class ReadPropertyFile {
    private ReadPropertyFile() {
    }

    public static String getValue(String key) throws Exception {
        Properties property = new Properties();
        FileInputStream file = new FileInputStream(System.getProperty("user.dir") + "/src/test/resources/conf/config.properties");
        property.load(file);
        String value = property.getProperty(key);
        if (Objects.isNull(value)) {
            throw new Exception("Property name " + key + " is not found. Please check the config.properties");
        }
        return value;
    }
}
>>>>>>>

2. Eager Initialization way - Hashtable: Read properties file at start once and store it in Hashtable.

<<<<<<<
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;

public final class ReadPropertyFile {
    private ReadPropertyFile() {
    }

    private static Properties property = new Properties();

    static {
        try {
            FileInputStream file = new FileInputStream(System.getProperty("user.dir") 
                    + "/src/test/resources/conf/config.properties");
            property.load(file);    //Store in java Hashtable - little slow
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getValue(String key) throws Exception {
        if (Objects.isNull(property.getProperty(key)) || Objects.isNull(key)) {
            throw new Exception("Property name " + key + " is not found. Please check the config.properties");
        }
        return property.getProperty(key);
    }
}
>>>>>>>       

3. Eager Initialization way - HashMap: Read properties file at start once and store it in Hashtable, then convert Hashtable into HashMap for faster operation.

<<<<<<<
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

public final class ReadPropertyFile {
    private ReadPropertyFile() {
    }

    private static Properties property = new Properties();
    private static final Map CONFIG_MAP = new HashMap<>();
    static {
        try {
            FileInputStream file = new FileInputStream(System.getProperty("user.dir")
                    + "/src/test/resources/conf/config.properties");
            property.load(file);    //Store in java Hashtable - little slow

            // Convert Hashtable to HashMap
            // Store all properties in HashMap
            for (Object key : property.keySet()) {
                CONFIG_MAP.put(String.valueOf(key), String.valueOf(property.get(key)));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getValueFromMap(String key) throws Exception {
        if (Objects.isNull(CONFIG_MAP.get(key)) || Objects.isNull(key)) {
            throw new Exception("Property name " + key + " is not found. Please check the config.properties");
        }
        return CONFIG_MAP.get(key);
    }
}
>>>>>>>      

Read CSV from S3

 import csv def count_records(csv_file):     record_count = 0     first_line = None     last_line = None     # Open the CSV file and read it...