Friday, December 29, 2023

Effective Java (3rd Edition) by Joshua Bloch

Effective Java (3rd Edition) by Joshua Bloch



ISBN:
978-0134685991
Publication Date: December 27, 2017
Price: $43.99 (Amazon)








"Effective Java" is a Jolt-award-winning book, updated for Java 7 and 8, covering new design patterns and language idioms. Authored by Joshua Bloch, it focuses on Lambda, streams, generics, collections, and selected Java 9 features.

What Sets It Apart: The book is known for its standalone essays in each chapter, offering advice, Java platform insights, and updated code examples. It covers updated techniques and best practices, particularly in objects, classes, libraries, methods, and serialization.

Target Audience: Aimed at intermediate to advanced Java programmers familiar with earlier Java versions and seeking to update their skills.

I had the opportunity to hear Josh speak several times and his knowledge of Java is unsurpassed.  This book is one every serious Java developer should read.  It is written in a clear and accessible manner.

Thursday, November 9, 2023

How to Use Math.ceil in Java

Photo by Pixabay

Are you curious about how to use the Math.ceil function in Java? Well, let's break it down, step by step!

What is Math.ceil?

Math.ceil is like a magic tool in Java that helps us with numbers. It's like having a superhero friend who can make numbers bigger, but not just any bigger—the next biggest whole number!

The Problem

Imagine you have some numbers that aren't whole. They have parts after the dot, like 5.5 or 3.7. Sometimes, you want to make them into big whole numbers. That's where Math.ceil comes to the rescue.

How to Use It

Here's the secret formula:

  1. First, you need a number. Let's call it your "special number." For example, let's use the number 7.3.
  2. Next, you ask Math.ceil to do its magic with your special number. It looks at your number and says, "I'll make it bigger, but not just a little bigger. I'll make it the next biggest whole number!" So, Math.ceil(7.3) says, "I'll turn 7.3 into 8.0."
  3. And that's it! You now have a bigger whole number. It's like turning 7.3 into 8, just like magic.

Example

Let's see it in action:


import java.lang.Math; public class CeilingExample { public static void main(String[] args) { double specialNumber = 7.3; // Our special number double biggerNumber = Math.ceil(specialNumber); // Math.ceil does its magic System.out.println("Original number: " + specialNumber); System.out.println("Bigger number: " + biggerNumber); } }

When you run this program, it will say:


Original number: 7.3
Bigger number: 8.0
    

You see, Math.ceil made our special number 7.3 bigger and turned it into the next biggest whole number, which is 8!

Why It's Cool

Math.ceil helps us when we want to be very fair with numbers. Sometimes, we need to round up to make sure everyone gets their fair share. Imagine sharing candies with your friends. If you have 7.3 candies, Math.ceil helps make sure each friend gets 8 candies. It's like being super fair!

So, that's how you use Math.ceil in Java. It's like magic that turns numbers into the next biggest whole numbers, and it helps us be fair with our numbers. Now you're ready to use it in your own programs, just like a little math wizard!

Monday, August 21, 2023

Integrating Secrets into OpenShift Deployments via Maven

Photo by Sound On:

I am using the to deploy a SpringBoot Java project to OpenShift.  This project uses secrets that are manually added to OpenShift via the console.  The secrets are added to the container as environment variables.  

When building and deploying applications on OpenShift using the Maven build tool, especially for SpringBoot Java projects, managing secrets efficiently is paramount. OpenShift provides a robust environment for container orchestration, but like any tool, it requires certain optimizations to smooth out workflows. One such hiccup often encountered is the management of secrets, which are crucial for the application's environment variables.

Background

I've been employing the  OpenShift Maven Plugin to streamline my deployment processes of a SpringBoot Java project to OpenShift. In my setup, I've relied on secrets that were being manually added to OpenShift using the console. These secrets were essential as they were loaded into the container as environment variables.

Challenge

A recurring bottleneck in this process was that every time the project underwent deployment, I found myself revisiting the OpenShift console to reapply these secrets. Not only was this tedious, but it also raised concerns about the efficiency of the deployment process.  It is also a step that is easy to forget and will leave the software in an unusable state.

The Solution

After some research, I came across a way to counteract this issue. The solution is to craft a specific YAML configuration fragment that aligns with the FAQ guidance on "How do I create an environment variable?". Rather than stipulating individual environment variables, the approach leverages the envFrom directive combined with secretRef to reference a secret. This allows for loading all key-value pairs in the secret as environment variables in one fell swoop.

 Detailed Explanation


envFrom: This directive provides an efficient method for setting multiple environment variables in a container. Instead of the laborious task of defining each environment variable one-by-one, envFrom enables users to set all environment variables from a unified source.

secretRef: A pivotal component of this approach, secretRef directs the environment variables to be derived from a Kubernetes Secret.

name:my-secret: The secret's name is crucial. For this illustration, consider the name to be my-secret. This secret should be pre-existing in the same namespace as the associated resource (e.g., Pod or Deployment). Within this secret, every key-value pair will be translated into an environment variable. Here, the key assumes the role of the environment variable's name, and the associated value is what the environment variable will be set to.

Summary


This solution not only streamlines the deployment process but also reduces the chances of manual error. By integrating the management of secrets directly into the Maven deployment workflow, we can ensure a smoother and more automated deployment process on OpenShift.

Have you encountered similar challenges with your deployments? Share your experiences below! 👇

Thursday, August 17, 2023

Mastering SQL: The Power Duo of 'GROUP BY' and 'HAVING'

Photo by Ebru Yılmaz

Welcome SQL enthusiasts! In today's post, we're diving deep into the synergistic relationship between GROUP BY and HAVING clauses in SQL. Both are paramount for data aggregation tasks, but how do they work hand in hand? Let's embark on this technical exploration!

GROUP BY is the hero of SQL when it comes to grouping rows that share the same values in specified columns. It often comes into play with aggregate functions like COUNT(), SUM(), and AVG(). But there's a catch! What if you want to filter these grouped results further?

This is where HAVING enters the scene. Unlike the WHERE clause, which filters rows before they are grouped, the HAVING clause filters the groups after they are created. This means you can apply conditions on aggregate functions directly.

Let's take a practical dive. Consider you want to find products, from a sales database, that are popular across multiple cities with an impressive sales count. Here's how you can wield both GROUP BY and HAVING to achieve this:

SELECT product_name, COUNT(DISTINCT city) as number_of_cities, SUM(units_sold) as total_units_sold
FROM sales
GROUP BY product_name
HAVING COUNT(DISTINCT city) > 1 AND SUM(units_sold) > 100;
        

As you can see, the harmony between GROUP BY and HAVING empowers SQL practitioners to perform intricate data analysis with precision. Always remember, while GROUP BY clubs the data, HAVING is there to refine your aggregated results further!

Are you eager to enhance your SQL prowess further? Bookmark our blog and join our journey to unravel the mysteries of databases and query languages! 🌐

Tuesday, August 15, 2023

Unveiling the Mysteries of Your CSV's Second Column! 🐧🔍

Photo by Mariam Antadze:


Ever gazed at a CSV file over a steaming cup of coffee ☕, scratching your head, thinking, "How many times does this value pop up?". Well, today's your lucky day! Ready for some command-line sorcery? 🎩✨

🔮 Behold... The Magical Bash Script! 🐚


awk -F, '{print $2}' input.csv | sort | uniq -c | awk '$1 != 1'

    

🕊️ Dissecting the Spell

  • awk -F, '{print $2}' input.csv: This is where the magic starts! 🌟 This command fetches the second column of our CSV. That's right! The print $2 is the star player here, ensuring we're only eyeing the second column.
  • sort: Next up, the ever-helpful librarian of the command line, putting everything in tidy rows.
  • uniq -c: Our trusty friend here spots unique items and counts 'em. Think of it as a bouncer with a clicker at the club's entrance 🎉.
  • awk '$1 != 1': Lastly, this guy filters out the solo performers, showing only values with company.

Voilà! A handy method to peer into the depths of your CSV's second column. Whether you're cleaning up data or uncovering the secrets within, this little snippet is your key!

🚀 Wrapping it up!

Remember: In the vast universe of data, every column tells a tale. Now you're equipped to hear the second chapter. May your insights always be enlightening! 🌌

Tags: #bash, #csv, #awk, #commandLineMagic

Tuesday, August 1, 2023

Simplifying Data Conversion: Converting JSON to CSV Using jq


JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used data formats, each with its unique advantages. Sometimes, you may encounter JSON data that needs to be converted into CSV format for easier analysis, sharing, or integration with other tools. In this blog post, we'll explore how to leverage jq to effortlessly convert JSON to CSV, enabling you to handle data transformation with ease and efficiency.