Skip to main content

Posts

MongoDB - Basic Commands

Greetings! These are the basic MongoDB commands we use in our day-today work. Adding as a reference to myself :) Create database use DATABASE_NAME $ use my_database Show databases $ show dbs Connect to database use DATABASE_NAME $ use my_database Drop database db.dropDatabase() Create Collection DB.COLLECTION_NAME.(OPTIONS) $ db.products.({ name: "Fruit Juice" }) Show collections $ show collections Drop collection db.COLLECTION_NAME.drop() $ db.products.drop() Insert document db.COLLECTION_NAME.insert(DOCUMENT) $ db.products.insert({ item_name: "Frui Juice", item_description: "sdfsdklf", price: "100" }) Read document db.COLLECTION_NAME.find() db.COLLECTION_NAME.find().pretty() $ db.products.find().pretty() $ db.products.find({ item_name: "Fruit Juice" }).pretty() Like statement - we have to use regex $ db.products.find( { item_name: { $regex: /789$/ } } ) AND $ db.COLLECTION_NAME.f...

Java Concurrency - Executor Framework

Greetings! What is the basic abstraction for creating threads in Java? Is it new Thread(new Runnable()) ? NO. It is Executors! Decoupling is a good programming practice and it is better to separate thread creation from the application. Java's way of encapsulating these functions are known as executors. ( Executors ) Executor Interfaces Executor - simple interface used to submit new tasks ExecutorService - sub interface of Executor which supports life cycle management of both of the tasks and Executor itself. ScheduledExecutorService - sub interface of ExcecutorService which supports task scheduling. ExecutorService This has more features than Executor interface. Mainly it support thread pooling, return the result via Callable, shutting down the service. execute(Runnable) submit(Runnable) submit(Callable) shutdown() This has following implementations in java.util.concurrent package. ThreadPoolExecutor ScheduledThreadPoolExecutor Creating a ExecutorServic...

Java Concurrency - SimpleDateFormat Is Not Simple

Greeting! Formatting, parsing dates is a painful task. It always give us errors :(. A common way to format, parse dates in Java is using SimpleDateFormat. Here is a common class we can use. import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public final class DateUtils { public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private DateUtils() {} public static Date parse(String target) { try { return SIMPLE_DATE_FORMAT.parse(target); } catch (ParseException e) { e.printStackTrace(); } return null; } public static String format(Date target) { return SIMPLE_DATE_FORMAT.format(target); } } Do you think this is working. Let's test it. private static void testSimpleDateFormatInSingleThread() { final String source = "2019-01-11"; System.out.println(DateUtils.parse(s...

Docker - Basic Commands

Greetings! Following are frequently used Docker commands. Check the docker version. docker -version Pull image from docker repository. docker pull <image_name:tag> docker pull nginx List local images. docker images Create a container from docker image. docker run docker run -it -d nginx docker run --name my-nginx -it -d nginx List running containers. docker ps docker ps -a Access the running container. docker exec docker exec -it ubuntu bash Stop running container. docker stop Kill running container. docker kill Delete a container. docker rm <container_id> Delete an image from local storage. docker rmi <image_id> Build a docker image from a given Docker file. docker build .

Java 8 - Streams - flatMap()

Greetings! With stream API it is easy to iterate over a collection and do whatever operation we like to do. Let's say we have a number list which we would like to print power of 2 of each. List<Integer> attempt1 = Arrays.asList(1, 2, 3, 4, 5); attempt1.stream().map(x -> x * x).forEach(System.out::println); // 1, 4, 9, 16, 25 What if we have a List of List? let's try that out. List<List<Integer>> attempt2 = Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(2, 5)); Stream<List<Integer>> attempt2Stream = attempt2.stream(); attempt2Stream.map(x -> x * x).forEach(System.out::println); // compile error attempt2Stream.map(x -> x.stream().map(y -> y * y)).forEach(System.out::println); // java.util.stream.ReferencePipeline$3@4c873330, java.util.stream.ReferencePipeline$3@119d7047 As you can see Stream.map() doesn't give us the expected result for the type Stream<List<Integer>>. This is because map() operation ...