Saturday, 4 June 2022

How to supply a password securely to a sqoop job?

In this post, I am going to explain how to supply the password securely to a sqoop job.

 

Step 1: Create a credential using ‘hadoop credential create’ command.

 

Open terminal and execute below command.

 

hadoop credential create retailDBPassword -provider jceks://hdfs/user/cloudera/retail.db.password.jceks

[cloudera@quickstart ~]$ hadoop credential create retailDBPassword -provider jceks://hdfs/user/cloudera/retail.db.password.jceks
WARNING: You have accepted the use of the default provider password
by not configuring a password in one of the two following locations:
    * In the environment variable HADOOP_CREDSTORE_PASSWORD
    * In a file referred to by the configuration entry
      hadoop.security.credstore.java-keystore-provider.password-file.
Please review the documentation regarding provider passwords in
the keystore passwords section of the Credential Provider API
Continuing with the default provider password.

Enter alias password: 
Enter alias password again: 
retailDBPassword has been successfully created.
Provider jceks://hdfs/user/cloudera/retail.db.password.jceks has been updated.

 

Above command prompts you for a password. Once the command execution is successful, password is encrypted and persisted to the file ‘retail.db.password.jceks’.

 

Let’s query the file ‘retail.db.password.jceks’ and see whether we can read any data from it or not.

 

Open terminal and execute the command ‘hadoop fs -cat /user/cloudera/retail.db.password.jceks’.

 


 

Step 2: Let’s create a job by supplying the .jceks file.

 

sqoop job \
-Dhadoop.security.credential.provider.path=jceks://hdfs/user/cloudera/retail.db.password.jceks \
--create customer_import_supply_pwd_via_jceks \
-- import \
--connect "jdbc:mysql://quickstart.cloudera:3306/retail_db" \
--username "root" \
--password-alias retailDBPassword  \
--table "customers" \
--warehouse-dir /job-demo-3 \
--incremental append \
--check-column customer_id \
--last-value 0

 

[cloudera@quickstart ~]$ sqoop job \
> -Dhadoop.security.credential.provider.path=jceks://hdfs/user/cloudera/retail.db.password.jceks \
> --create customer_import_supply_pwd_via_jceks \
> -- import \
> --connect "jdbc:mysql://quickstart.cloudera:3306/retail_db" \
> --username "root" \
> --password-alias retailDBPassword  \
> --table "customers" \
> --warehouse-dir /job-demo-3 \
> --incremental append \
> --check-column customer_id \
> --last-value 0 
Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
22/04/08 21:29:39 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0
[cloudera@quickstart ~]$
[cloudera@quickstart ~]$
[cloudera@quickstart ~]$ sqoop job --list
Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
22/04/08 21:30:09 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0
Available jobs:
  customer_import
  customer_import_supply_pwd_via_file
  customer_import_supply_pwd_via_jceks

 

Let’s execute the job ‘customer_import_supply_pwd_via_jceks’ by running below command.

sqoop job --exec customer_import_supply_pwd_via_jceks

[cloudera@quickstart ~]$ sqoop job --exec customer_import_supply_pwd_via_jceks
Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
22/04/08 21:30:55 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0
22/04/08 21:30:57 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
22/04/08 21:30:57 INFO tool.CodeGenTool: Beginning code generation
22/04/08 21:30:57 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `customers` AS t LIMIT 1
22/04/08 21:30:57 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `customers` AS t LIMIT 1
22/04/08 21:30:57 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /usr/lib/hadoop-mapreduce
Note: /tmp/sqoop-cloudera/compile/900d8087c6412427dd55246d3c95aa50/customers.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
22/04/08 21:30:59 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-cloudera/compile/900d8087c6412427dd55246d3c95aa50/customers.jar
22/04/08 21:30:59 INFO tool.ImportTool: Maximal id query for free form incremental import: SELECT MAX(`customer_id`) FROM `customers`
22/04/08 21:30:59 INFO tool.ImportTool: Incremental import based on column `customer_id`
22/04/08 21:30:59 INFO tool.ImportTool: Lower bound value: 0
22/04/08 21:30:59 INFO tool.ImportTool: Upper bound value: 12440
22/04/08 21:30:59 WARN manager.MySQLManager: It looks like you are importing from mysql.
22/04/08 21:30:59 WARN manager.MySQLManager: This transfer can be faster! Use the --direct
22/04/08 21:30:59 WARN manager.MySQLManager: option to exercise a MySQL-specific fast path.
22/04/08 21:30:59 INFO manager.MySQLManager: Setting zero DATETIME behavior to convertToNull (mysql)
22/04/08 21:30:59 INFO mapreduce.ImportJobBase: Beginning import of customers
22/04/08 21:30:59 INFO Configuration.deprecation: mapred.job.tracker is deprecated. Instead, use mapreduce.jobtracker.address
22/04/08 21:30:59 INFO Configuration.deprecation: mapred.jar is deprecated. Instead, use mapreduce.job.jar
22/04/08 21:30:59 INFO Configuration.deprecation: mapred.map.tasks is deprecated. Instead, use mapreduce.job.maps
22/04/08 21:30:59 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
22/04/08 21:31:00 WARN hdfs.DFSClient: Caught exception 
java.lang.InterruptedException
	at java.lang.Object.wait(Native Method)
	at java.lang.Thread.join(Thread.java:1281)
	at java.lang.Thread.join(Thread.java:1355)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.closeResponder(DFSOutputStream.java:967)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.endBlock(DFSOutputStream.java:705)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:894)
22/04/08 21:31:00 INFO db.DBInputFormat: Using read commited transaction isolation
22/04/08 21:31:00 INFO db.DataDrivenDBInputFormat: BoundingValsQuery: SELECT MIN(`customer_id`), MAX(`customer_id`) FROM `customers` WHERE ( `customer_id` > 0 AND `customer_id` <= 12440 )
22/04/08 21:31:00 INFO db.IntegerSplitter: Split size: 3109; Num splits: 4 from: 1 to: 12440
22/04/08 21:31:00 WARN hdfs.DFSClient: Caught exception 
java.lang.InterruptedException
	at java.lang.Object.wait(Native Method)
	at java.lang.Thread.join(Thread.java:1281)
	at java.lang.Thread.join(Thread.java:1355)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.closeResponder(DFSOutputStream.java:967)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.endBlock(DFSOutputStream.java:705)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:894)
22/04/08 21:31:00 INFO mapreduce.JobSubmitter: number of splits:4
22/04/08 21:31:01 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1649172504056_0010
22/04/08 21:31:01 INFO impl.YarnClientImpl: Submitted application application_1649172504056_0010
22/04/08 21:31:01 INFO mapreduce.Job: The url to track the job: http://quickstart.cloudera:8088/proxy/application_1649172504056_0010/
22/04/08 21:31:01 INFO mapreduce.Job: Running job: job_1649172504056_0010
22/04/08 21:31:08 INFO mapreduce.Job: Job job_1649172504056_0010 running in uber mode : false
22/04/08 21:31:08 INFO mapreduce.Job:  map 0% reduce 0%
22/04/08 21:31:24 INFO mapreduce.Job:  map 25% reduce 0%
22/04/08 21:31:27 INFO mapreduce.Job:  map 50% reduce 0%
22/04/08 21:31:29 INFO mapreduce.Job:  map 100% reduce 0%
22/04/08 21:31:30 INFO mapreduce.Job: Job job_1649172504056_0010 completed successfully
22/04/08 21:31:30 INFO mapreduce.Job: Counters: 30
	File System Counters
		FILE: Number of bytes read=0
		FILE: Number of bytes written=691652
		FILE: Number of read operations=0
		FILE: Number of large read operations=0
		FILE: Number of write operations=0
		HDFS: Number of bytes read=487
		HDFS: Number of bytes written=953915
		HDFS: Number of read operations=16
		HDFS: Number of large read operations=0
		HDFS: Number of write operations=8
	Job Counters 
		Launched map tasks=4
		Other local map tasks=4
		Total time spent by all maps in occupied slots (ms)=59956
		Total time spent by all reduces in occupied slots (ms)=0
		Total time spent by all map tasks (ms)=59956
		Total vcore-milliseconds taken by all map tasks=59956
		Total megabyte-milliseconds taken by all map tasks=61394944
	Map-Reduce Framework
		Map input records=12440
		Map output records=12440
		Input split bytes=487
		Spilled Records=0
		Failed Shuffles=0
		Merged Map outputs=0
		GC time elapsed (ms)=765
		CPU time spent (ms)=4640
		Physical memory (bytes) snapshot=560345088
		Virtual memory (bytes) snapshot=6044925952
		Total committed heap usage (bytes)=243007488
	File Input Format Counters 
		Bytes Read=0
	File Output Format Counters 
		Bytes Written=953915
22/04/08 21:31:30 INFO mapreduce.ImportJobBase: Transferred 931.5576 KB in 30.6614 seconds (30.3821 KB/sec)
22/04/08 21:31:30 INFO mapreduce.ImportJobBase: Retrieved 12440 records.
22/04/08 21:31:30 INFO util.AppendUtils: Creating missing output directory - customers
22/04/08 21:31:30 INFO tool.ImportTool: Saving incremental import state to the metastore
22/04/08 21:31:30 INFO tool.ImportTool: Updated data for job: customer_import_supply_pwd_via_jceks
[cloudera@quickstart ~]$


 

Previous                                                    Next                                                    Home

Java math addExact: add two numbers and throw exception when the result overflows

 Java Math class provides addExact method, which take two integer /or long arguments and return the sum of its arguments, throw an exception if the result overflows an int or long.

 


Signature

public static int addExact(int x, int y)

Returns the sum of its arguments, throwing an exception if the result overflows an int.

 

public static long addExact(long x, long y)

Returns the sum of its arguments, throwing an exception if the result overflows a long.

 

Find the below working application.

 

AddExactDemo.java

package com.sample.app;

public class AddExactDemo {

    public static void main(String[] args) {

        System.out.println("Math.addExact(10, 20) : " + Math.addExact(10, 20));
        System.out.println("Math.addExact(10l, 20l) : " + Math.addExact(10l, 20l) +"\n");

        int a = Integer.MAX_VALUE;
        int b = 10;

        try {
            System.out.println("Math.addExact(" + a + ", " + b + ") : ");
             Math.addExact(a, b);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }

        System.out.println();

        long c = Long.MAX_VALUE;
        long d = 10l;

        try {
            System.out.println("Math.addExact(" + c + ", " + d + ") : ");
            Math.addExact(c, d);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }

}

Output

Math.addExact(10, 20) : 30
Math.addExact(10l, 20l) : 30

Math.addExact(2147483647, 10) : 
java.lang.ArithmeticException: integer overflow
    at java.lang.Math.addExact(Math.java:790)
    at com.sample.app.AddExactDemo.main(AddExactDemo.java:15)

Math.addExact(9223372036854775807, 10) : 
java.lang.ArithmeticException: long overflow
    at java.lang.Math.addExact(Math.java:809)
    at com.sample.app.AddExactDemo.main(AddExactDemo.java:27)

 

Previous                                                 Next                                                 Home

How to supply the password file to a sqoop job

In this post, I am going to explain how to supply a password file to the sqoop job.

 

You can supply the password file at the time of job creation using --password-file option.

 

Example

--password-file file://{FILE_ABSOLTE_PATH}

 

Step 1: Let’s create a password file by executing below command.

echo -n "cloudera">> .mySQLPassword

 

Above command copy the string "cloudera" to the hidden file .mySQLPassword.

[cloudera@quickstart ~]$ echo -n "cloudera">> .mySQLPassword
[cloudera@quickstart ~]$ 
[cloudera@quickstart ~]$ cat .mySQLPassword 
[cloudera@quickstart ~]$ pwd
/home/cloudera

 


Step 2: Let’s create a job by supplying the password file /home/cloudera/.mySQLPassword.

sqoop job \
--create customer_import_supply_pwd_via_file \
-- import \
--connect "jdbc:mysql://quickstart.cloudera:3306/retail_db" \
--username "root" \
--password-file file:///home/cloudera/.mySQLPassword \
--table "customers" \
--warehouse-dir /job-demo-2 \
--incremental append \
--check-column customer_id \
--last-value 0

  Let’s execute above command to create the job ‘customer_import_supply_pwd_via_file’.

 

[cloudera@quickstart ~]$ sqoop job \
> --create customer_import_supply_pwd_via_file \
> -- import \
> --connect "jdbc:mysql://quickstart.cloudera:3306/retail_db" \
> --username "root" \
> --password-file file:///home/cloudera/.mySQLPassword \
> --table "customers" \
> --warehouse-dir /job-demo-2 \
> --incremental append \
> --check-column customer_id \
> --last-value 0 
Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
22/04/08 19:27:27 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0

Let’s print all the jobs.

[cloudera@quickstart ~]$ sqoop job --list
Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
22/04/08 19:28:07 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0
Available jobs:
  customer_import
  customer_import_supply_pwd_via_file

Let’s execute the job ‘customer_import_supply_pwd_via_file’.

 

sqoop job --exec customer_import_supply_pwd_via_file

 

Run above command in the terminal.

[cloudera@quickstart ~]$ sqoop job --exec customer_import_supply_pwd_via_file
Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
22/04/08 19:28:58 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0
22/04/08 19:29:00 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
22/04/08 19:29:00 INFO tool.CodeGenTool: Beginning code generation
22/04/08 19:29:00 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `customers` AS t LIMIT 1
22/04/08 19:29:00 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `customers` AS t LIMIT 1
22/04/08 19:29:00 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /usr/lib/hadoop-mapreduce
Note: /tmp/sqoop-cloudera/compile/1f442024b3782a936a38433b3d512adf/customers.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
22/04/08 19:29:02 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-cloudera/compile/1f442024b3782a936a38433b3d512adf/customers.jar
22/04/08 19:29:02 INFO tool.ImportTool: Maximal id query for free form incremental import: SELECT MAX(`customer_id`) FROM `customers`
22/04/08 19:29:02 INFO tool.ImportTool: Incremental import based on column `customer_id`
22/04/08 19:29:02 INFO tool.ImportTool: Lower bound value: 0
22/04/08 19:29:02 INFO tool.ImportTool: Upper bound value: 12440
22/04/08 19:29:02 WARN manager.MySQLManager: It looks like you are importing from mysql.
22/04/08 19:29:02 WARN manager.MySQLManager: This transfer can be faster! Use the --direct
22/04/08 19:29:02 WARN manager.MySQLManager: option to exercise a MySQL-specific fast path.
22/04/08 19:29:02 INFO manager.MySQLManager: Setting zero DATETIME behavior to convertToNull (mysql)
22/04/08 19:29:02 INFO mapreduce.ImportJobBase: Beginning import of customers
22/04/08 19:29:02 INFO Configuration.deprecation: mapred.job.tracker is deprecated. Instead, use mapreduce.jobtracker.address
22/04/08 19:29:02 INFO Configuration.deprecation: mapred.jar is deprecated. Instead, use mapreduce.job.jar
22/04/08 19:29:02 INFO Configuration.deprecation: mapred.map.tasks is deprecated. Instead, use mapreduce.job.maps
22/04/08 19:29:02 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
22/04/08 19:29:03 WARN hdfs.DFSClient: Caught exception 
java.lang.InterruptedException
	at java.lang.Object.wait(Native Method)
	at java.lang.Thread.join(Thread.java:1281)
	at java.lang.Thread.join(Thread.java:1355)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.closeResponder(DFSOutputStream.java:967)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.endBlock(DFSOutputStream.java:705)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:894)
22/04/08 19:29:03 WARN hdfs.DFSClient: Caught exception 
java.lang.InterruptedException
	at java.lang.Object.wait(Native Method)
	at java.lang.Thread.join(Thread.java:1281)
	at java.lang.Thread.join(Thread.java:1355)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.closeResponder(DFSOutputStream.java:967)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.endBlock(DFSOutputStream.java:705)
	at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:894)
22/04/08 19:29:04 INFO db.DBInputFormat: Using read commited transaction isolation
22/04/08 19:29:04 INFO db.DataDrivenDBInputFormat: BoundingValsQuery: SELECT MIN(`customer_id`), MAX(`customer_id`) FROM `customers` WHERE ( `customer_id` > 0 AND `customer_id` <= 12440 )
22/04/08 19:29:04 INFO db.IntegerSplitter: Split size: 3109; Num splits: 4 from: 1 to: 12440
22/04/08 19:29:04 INFO mapreduce.JobSubmitter: number of splits:4
22/04/08 19:29:04 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1649172504056_0009
22/04/08 19:29:05 INFO impl.YarnClientImpl: Submitted application application_1649172504056_0009
22/04/08 19:29:05 INFO mapreduce.Job: The url to track the job: http://quickstart.cloudera:8088/proxy/application_1649172504056_0009/
22/04/08 19:29:05 INFO mapreduce.Job: Running job: job_1649172504056_0009
22/04/08 19:29:12 INFO mapreduce.Job: Job job_1649172504056_0009 running in uber mode : false
22/04/08 19:29:12 INFO mapreduce.Job:  map 0% reduce 0%
22/04/08 19:29:27 INFO mapreduce.Job:  map 25% reduce 0%
22/04/08 19:29:30 INFO mapreduce.Job:  map 50% reduce 0%
22/04/08 19:29:31 INFO mapreduce.Job:  map 100% reduce 0%
22/04/08 19:29:32 INFO mapreduce.Job: Job job_1649172504056_0009 completed successfully
22/04/08 19:29:32 INFO mapreduce.Job: Counters: 30
	File System Counters
		FILE: Number of bytes read=0
		FILE: Number of bytes written=690844
		FILE: Number of read operations=0
		FILE: Number of large read operations=0
		FILE: Number of write operations=0
		HDFS: Number of bytes read=487
		HDFS: Number of bytes written=953915
		HDFS: Number of read operations=16
		HDFS: Number of large read operations=0
		HDFS: Number of write operations=8
	Job Counters 
		Launched map tasks=4
		Other local map tasks=4
		Total time spent by all maps in occupied slots (ms)=58628
		Total time spent by all reduces in occupied slots (ms)=0
		Total time spent by all map tasks (ms)=58628
		Total vcore-milliseconds taken by all map tasks=58628
		Total megabyte-milliseconds taken by all map tasks=60035072
	Map-Reduce Framework
		Map input records=12440
		Map output records=12440
		Input split bytes=487
		Spilled Records=0
		Failed Shuffles=0
		Merged Map outputs=0
		GC time elapsed (ms)=808
		CPU time spent (ms)=4370
		Physical memory (bytes) snapshot=542535680
		Virtual memory (bytes) snapshot=6044942336
		Total committed heap usage (bytes)=243007488
	File Input Format Counters 
		Bytes Read=0
	File Output Format Counters 
		Bytes Written=953915
22/04/08 19:29:32 INFO mapreduce.ImportJobBase: Transferred 931.5576 KB in 29.8469 seconds (31.2112 KB/sec)
22/04/08 19:29:32 INFO mapreduce.ImportJobBase: Retrieved 12440 records.
22/04/08 19:29:32 INFO util.AppendUtils: Creating missing output directory - customers
22/04/08 19:29:32 INFO tool.ImportTool: Saving incremental import state to the metastore
22/04/08 19:29:32 INFO tool.ImportTool: Updated data for job: customer_import_supply_pwd_via_file
[cloudera@quickstart ~]$

That’s it you are done. But storing a database password as plain text is not a recommended. To address this, we need to store the password in encrypted form. I will explain this secured way in my next post .

 


Previous                                                    Next                                                    Home

Apache Atlas: Entities

An entity is an instance of type. For example, a database entity looks like below.

{
   "referredEntities":{
      
   },
   "entity":{
      "typeName":"jdbc_db",
      "attributes":{
         "owner":"Krishna G",
         "replicatedTo":null,
         "userDescription":null,
         "replicatedFrom":null,
         "qualifiedName":"abc.net:1234/warehouse",
         "displayName":"warehouse database",
         "name":"warehouse",
         "description":"warehouse database in MySQL",
         "createdTime":0,
         "locationUri":null,
         "ownedBy":"Platforms team"
      },
      "guid":"b11dd53c-1c5c-4bf7-9513-7962e7ffccd2",
      "isIncomplete":false,
      "status":"ACTIVE",
      "createdBy":"admin",
      "updatedBy":"admin",
      "createTime":1643363877582,
      "updateTime":1643363877582,
      "version":0,
      "relationshipAttributes":{
         "inputToProcesses":[
            
         ],
         "pipeline":null,
         "schema":[
            
         ],
         "tables":[
            {
               "guid":"ccf28d22-f3e6-46a0-8de6-355f697da2cf",
               "typeName":"jdbc_table",
               "entityStatus":"ACTIVE",
               "displayText":"products",
               "relationshipType":"jdbc_db_to_table",
               "relationshipGuid":"4feed41e-9368-4997-ab6c-a133d85d6a76",
               "relationshipStatus":"ACTIVE",
               "relationshipAttributes":{
                  "typeName":"jdbc_db_to_table"
               }
            }
         ],
         "model":null,
         "meanings":[
            
         ],
         "outputFromProcesses":[
            
         ]
      },
      "labels":[
         
      ]
   }
}

 

Points to be noted

a.   Instance of type is called an entity, and has an unique identifier (GUID) to identify the entity in the Atlas. This GUID is assigned by Atlas, and will not be changed in the lifetime.

b.   Every entity belongs to exactly one type. In this example,  warehouse entity is of type jdbc_db

c.    Every entity has attributes, referredEntities, referredAttributes associated with it. You will learn these in upcoming posts.

 

 

 

 

Previous                                                    Next                                                    Home

Friday, 3 June 2022

Get Date object from year, month and day

Below snippet return the Date object from the given year, month and day.

private static Date getDate(Integer year, Integer month, Integer day) throws ParseException {
	String dateStr = year + "-" + month + "-" + day;
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	return formatter.parse(dateStr);
}

 


 

Find the below working application.

 

DateUtil.java

package com.sample.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	private static Date getDate(Integer year, Integer month, Integer day) throws ParseException {
		String dateStr = year + "-" + month + "-" + day;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.parse(dateStr);
	}

	public static void main(String[] args) throws ParseException {
		Date date = getDate(2022, 6, 6);

		System.out.println(date);
	}

}

Output

Mon Jun 06 00:00:00 IST 2022


  

Previous                                                 Next                                                 Home

Trigonometric functions in Java with example

Java Math class provides various functions to support trigonometric calculations. Below table summarizes the methods.

 

Method

Description

acos(double a)

Return arc cosine of a value.

asin(double a)

Return arc sine of a value.

atan(double a)

Return arc tangent of a value.

cos(double a)

Returns the trigonometric cosine of an angle. Angle ‘a’ is specified in radians.

cosh(double x)

Returns the hyperbolic cosine of a double value.

sin(double a)

Returns the trigonometric sine of an angle. Angle ‘a’ is specified in radians.

sinh(double x)

Returns the hyperbolic sine of a double value.

tan(double a)

Returns the trigonometric tangent of an angle. Angle ‘a’ is specified in radians.

tanh(double x)

Returns the hyperbolic tangent of a double value.




TrignometricFunctionsDemo.java

package com.sample.app;

public class TrignometricFunctionsDemo {
	public static void main(String[] args) {
		double degrees = 45;
		double radians = Math.toRadians(degrees);

		double acosValue = Math.acos(0.5);
		double asinValue = Math.asin(0.5);
		double atanValue = Math.atan(0.5);
		double cosValue = Math.cos(radians);
		double coshValue = Math.cosh(0.5);
		double sinValue = Math.sin(radians);
		double sinhValue = Math.sinh(0.5);
		double tanValue = Math.tan(radians);
		double tanhValue = Math.tanh(0.5);

		System.out.println("degrees : " + degrees);
		System.out.println("radians : " + radians);
		System.out.println("acosValue : " + acosValue);
		System.out.println("asinValue : " + asinValue);
		System.out.println("atanValue : " + atanValue);
		System.out.println("cosValue : " + cosValue);
		System.out.println("coshValue : " + coshValue);
		System.out.println("sinValue : " + sinValue);
		System.out.println("sinhValue : " + sinhValue);
		System.out.println("tanValue : " + tanValue);
		System.out.println("tanhValue : " + tanhValue);
	}

}

 

Output

degrees : 45.0
radians : 0.7853981633974483
acosValue : 1.0471975511965979
asinValue : 0.5235987755982989
atanValue : 0.4636476090008061
cosValue : 0.7071067811865476
coshValue : 1.1276259652063807
sinValue : 0.7071067811865475
sinhValue : 0.5210953054937474
tanValue : 0.9999999999999999
tanhValue : 0.46211715726000974

 


Previous                                                 Next                                                 Home