Thursday, September 27, 2018

Command Line Arguments and reading those values depending on the Options , working on that option

package com.muthyatechnology.sqltomongoconverter;

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class Main {

    public static final int DEFAULT_RESULT_BATCH_SIZE = 50;
    public static final String ENTER_SQL_TEXT = "Enter input sql:\n\n ";
    private static final String DEFAULT_MONGO_PORT = "27017";

    public static Options buildOptions() {
        Options options = new Options();


        final OptionGroup sourceOptionGroup = new OptionGroup();
        sourceOptionGroup.setRequired(false);

        sourceOptionGroup.addOption(Option.builder("s")
                .longOpt("sourceFile")
                .hasArg(true)
                .required(false)
                .desc("the source file.")
                .build());

        sourceOptionGroup.addOption(Option.builder("i")
                .longOpt("interactiveMode")
                .hasArg(false)
                .required(false)
                .desc("interactive mode")
                .build());

        sourceOptionGroup.addOption(Option.builder("sql")
                .longOpt("sql")
                .hasArg(true)
                .required(false)
                .desc("the sql select statement")
                .build());


        options.addOption(Option.builder("d")
                .longOpt("destinationFile")
                .hasArg(true)
                .required(false)
                .desc("the destination file.  Defaults to System.out")
                .build());

        options.addOption(Option.builder("h")
                .longOpt("host")
                .hasArg(true)
                .required(false)
                .desc("hosts and ports in the following format (host:port) default port is " + DEFAULT_MONGO_PORT)
                .build());

        options.addOption(Option.builder("db")
                .longOpt("database")
                .hasArg(true)
                .required(false)
                .desc("mongo database")
                .build());

        options.addOption(Option.builder("a")
                .longOpt("auth database")
                .hasArg(true)
                .required(false)
                .desc("auth mongo database")
                .build());

        options.addOption(Option.builder("u")
                .longOpt("username")
                .hasArg(true)
                .required(false)
                .desc("usename")
                .build());

        options.addOption(Option.builder("p")
                .longOpt("password")
                .hasArg(true)
                .required(false)
                .desc("password")
                .build());

        options.addOption(Option.builder("b")
                .longOpt("batchSize")
                .hasArg(true)
                .required(false)
                .desc("batch size for query results")
                .build());

        options.addOptionGroup(sourceOptionGroup);

        return options;
    }

    public static void main(String[] args) throws IOException, ParseException, ClassNotFoundException, org.apache.commons.cli.ParseException {
            Options options = buildOptions();


            CommandLineParser parser = new DefaultParser();
            HelpFormatter help = new HelpFormatter();
            help.setOptionComparator(new OptionComparator(Arrays.asList("s","sql","i","d","h","db","a","u","p","b")));

            CommandLine cmd = null;
            try {
                cmd = parser.parse(options, args);

                String source = cmd.getOptionValue("s");
                boolean interactiveMode = cmd.hasOption('i');

                String[] hosts = cmd.getOptionValues("h");
                String db = cmd.getOptionValue("db");
                String username = cmd.getOptionValue("u");
                String password = cmd.getOptionValue("p");
                String destination = cmd.getOptionValue("d");
                String authdb = cmd.getOptionValue("a");
                String sql = cmd.getOptionValue("sql");
                final int batchSize = Integer.parseInt(cmd.getOptionValue("b", ""+DEFAULT_RESULT_BATCH_SIZE));

                isFalse(hosts!=null && db==null,"provided option h, but missing db");
                isFalse(username!=null && (password==null || authdb==null),"provided option u, but missing p or a");

                isTrue(interactiveMode || source!=null || sql !=null,"Missing required option: s or i or sql");

             

            } catch (org.apache.commons.cli.ParseException e) {
                System.err.println(e.getMessage());
                help.printHelp(Main.class.getName(), options, true);
               // throw e;
            }

            System.exit(0);
        }

    private static void isTrue(boolean expression, String message) throws org.apache.commons.cli.ParseException {
        if (!expression) {
            throw new org.apache.commons.cli.ParseException(message);
        }
    }

    private static void isFalse(boolean expression, String message) throws org.apache.commons.cli.ParseException {
        if (expression) {
            throw new org.apache.commons.cli.ParseException(message);
        }
    }


    private static class OptionComparator implements Comparator<Option> {
        private final List<String> orderList;

        public OptionComparator(List<String> orderList) {
            this.orderList = orderList;
        }


        @Override
        public int compare(Option o1, Option o2) {
            int index1 = orderList.indexOf(o1.getOpt());
            int index2 = orderList.indexOf(o2.getOpt());
            return index1 - index2;
        }
    }

}



Output

Missing required option: s or i or sql
usage: com.muthyatechnology.sqltomongoconverter.Main [-s <arg> | -sql
       <arg> | -i]   [-d <arg>] [-h <arg>] [-db <arg>] [-a <arg>] [-u
       <arg>] [-p <arg>] [-b <arg>]
 -s,--sourceFile <arg>        the source file.
 -sql,--sql <arg>             the sql select statement
 -i,--interactiveMode         interactive mode
 -d,--destinationFile <arg>   the destination file.  Defaults to
                              System.out
 -h,--host <arg>              hosts and ports in the following format
                              (host:port) default port is 27017
 -db,--database <arg>         mongo database
 -a,--auth database <arg>     auth mongo database
 -u,--username <arg>          usename
 -p,--password <arg>          password

 -b,--batchSize <arg>         batch size for query results


Monday, September 24, 2018

How to convert object to JSON of an integer field having leading zeros using Jackson?

Question:
When I try to convert an object having an integer field with value as 0000, the converted JSON contains 0 instead of 0000.
How can I configure Jackson's ObjectMapper to convert 0000 to 00000?
Answer:
A leading 0 indicates an octal number. Hence leading zeros are not allowed for numeric values in JSON. See the following quote from the RFC 7159:
The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed.
If you need leading zeros, consider using a string.

Saturday, September 22, 2018

How do I resolve the “java.net.BindException: Address already in use: JVM_Bind” error?

(Windows Only)
To kill a process you first need to find the Process Id (pid)
By running the command :
netstat -ano | findstr :yourPortNumber
As shown in picture below
You will get your Process Id (PID), Now to kill the same process run this command:
taskkill /pid yourid /f
enter image description here

Friday, September 21, 2018

Steps to add the Java Decompiler in Eclipse :

http://jd.benow.ca/

Release

Source code

Installation

  1. Download and unzip the JD-Eclipse Update Site,
  2. Launch Eclipse,
  3. Click on "Help > Install New Software...",
  4. Click on button "Add..." to add an new repository,
  5. Enter "JD-Eclipse Update Site" and select the local site directory,
  6. Check "Java Decompiler Eclipse Plug-in",
  7. Next, next, next... and restart Eclipse.

To Add the Class patterns follow the following steps.

To Make it work in Eclipse Poton- 
I had to do some additional steps.
Eclipse -> Windows -> Preferences ->General -> Editors -> File Association
  1. Select "*.class" and mark "Class File Editor" as default
  2. Select "*.class without source" -> Add -> "Class File Editor" -> Make it as default
  3. Add "*.jar" in FileTypes and -->Add -> "Class File Editor" -> Make it as default
  4. Add "*.war" in FileTypes and -->Add -> "Class File Editor" -> Make it as default
  5. Restart eclipse

Integer with leading zeroes

Question:
When I write System.out.println(0123); I get 83 however System.out.println((int)0123F);prints 123.
Why does it work that way?

Answer:

Octal (base-8 number)

0123 means octal 123, that is 1*8*8 + 2*8 + 3, which equals 83. For some reason, octal floats are not available.
Creating 0123 means the integer 83. Creating 0123F means the floating 123. When converted back to integer, it remains 123.
Just don't use the leading 0 if you don't mean octal. After all, they are not exactly useful(and programmers who do know about octal numbers will get confused when they see 09F).

Remove credentials from Git or my old username is still in use or git authentication filed

The Git credential cache runs a daemon process which caches your credentials in memory and hands them out on demand. So killing your git-credential-cache--daemon process throws all these away and results in re-prompting you for your password if you continue to use this as the cache.helper option.
You could also disable use of the Git credential cache using git config --global --unset credential.helper. Then reset this, and you would continue to have the cached credentials available for other repositories (if any). You may also need to do git config --system --unset credential.helper if this has been set in the system configuration file (for example, Git for Windows 2).
On Windows you might be better off using the manager helper (git config --global credential.helper manager). This stores your credentials in the Windows credential store which has a Control Panel interface where you can delete or edit your stored credentials. With this store, your details are secured by your Windows login and can persist over multiple sessions. The managerhelper included in Git for Windows 2.x has replaced the earlier wincred helper that was added in Git for Windows 1.8.1.1. A similar helper called winstore is also available online and was used with GitExtensions as it offers a more GUI driven interface. The manager helper offers the same GUI interface as winstore.
If this problem comes on a Windows machine, do the following.
  • Go to Credential Manager
  • Go to Windows Credentials
  • Delete the entries under Generic Credentials
  • Try connecting again. This time, it should prompt you for the correct username and password.
Go to Credential Manager
Go to Windows Credentials and Delete the entries under Generic Credentials

Add Your Gmail Account to Outlook 2016 Using IMAP settings

Add Your Gmail Account to Outlook 2016 Using IMAP settings

Log into your Gmail account and open the Settings page with the little gear button. Click on the Forwarding and POP/IMAP tab and make sure IMAP is enabled and click on save changes.
a webmail view of Gmail Settings
Open Outlook 2016 and go to the File tab.
view of Outlook 2016 File tab
Then, just above the Account Settings button, click Add Account.
view of Outlook 2016 Add Account button
Select Manual setup or additional server types.
shows Outlook 2016 account types
Now, on the Choose Your Account Type step, select "POP or IMAP".
view of Outlook 2016 IMAP choice
Add your user information and your host server information,
Account type: IMAP
Incoming mail server: imap.gmail.com
Outgoing mail server (SMTP): smtp.gmail.com
Then add your logon information as shown below.
shows server settings in Outlook 2016
Go to More settings and select the Outgoing server tab. Check the box next to My outgoing server (SMTP) requires authentication and select the radio button next to Use same settings as my incoming mail server.
shows Gmail settings in Outlook 2016
In the same window go to the Advanced tab and verify the following.
Incoming server (IMAP): 993 or 143
Incoming server encrypted connection: SSL
Outgoing server (SMTP): 465 or 587
Outgoing server encrypted connection: TLS or Auto
Click OK when finished.
Advanced Settings in Outlook 2016
Click Next.
If you've entered everything correctly, both testing tasks will be completed successfully and then you can close that little window and again click Next.
Outlook 2016 test email successful
You'll get the "You're all set!" message,
Click Finish.
All Set message and Finish button.
And now you can check your email and "subscribe" to imap mailbox folders through Outlook 2016.
Outlook 2016 Gmail Inbox view

Monday, September 17, 2018

Percent-encoding, also known as URL encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI) under certain circumstances

Percent-encoding in a URI[edit]

Types of URI characters[edit]

The characters allowed in a URI are either reserved or unreserved (or a percent character as part of a percent-encoding). Reserved characters are those characters that sometimes have special meaning. For example, forward slash characters are used to separate different parts of a URL (or more generally, a URI). Unreserved characters have no such meanings. Using percent-encoding, reserved characters are represented using special character sequences. The sets of reserved and unreserved characters and the circumstances under which certain reserved characters have special meaning have changed slightly with each revision of specifications that govern URIs and URI schemes.
RFC 3986 section 2.2 Reserved Characters (January 2005)
!*'();:@&=+$,/?#[]
RFC 3986 section 2.3 Unreserved Characters (January 2005)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789-_.~
Other characters in a URI must be percent encoded.

Percent-encoding reserved characters[edit]

When a character from the reserved set (a "reserved character") has special meaning (a "reserved purpose") in a certain context, and a URI scheme says that it is necessary to use that character for some other purpose, then the character must be percent-encoded. Percent-encoding a reserved character involves converting the character to its corresponding byte value in ASCII and then representing that value as a pair of hexadecimal digits. The digits, preceded by a percent sign (%) which is used as an escape character, are then used in the URI in place of the reserved character. (For a non-ASCII character, it is typically converted to its byte sequence in UTF-8, and then each byte value is represented as above.)
The reserved character /, for example, if used in the "path" component of a URI, has the special meaning of being a delimiter between path segments. If, according to a given URI scheme, / needs to be in a path segment, then the three characters %2F or %2f must be used in the segment instead of a raw /.
Reserved characters after percent-encoding
!#$&'()*+,/:;=?@[]
%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D
Reserved characters that have no reserved purpose in a particular context may also be percent-encoded but are not semantically different from those that are not.
In the "query" component of a URI (the part after a ? character), for example, / is still considered a reserved character but it normally has no reserved purpose, unless a particular URI scheme says otherwise. The character does not need to be percent-encoded when it has no reserved purpose.
URIs that differ only by whether a reserved character is percent-encoded or appears literally are normally considered not equivalent (denoting the same resource) unless it can be determined that the reserved characters in question have no reserved purpose. This determination is dependent upon the rules established for reserved characters by individual URI schemes.

Percent-encoding unreserved characters[edit]

Characters from the unreserved set never need to be percent-encoded.
URIs that differ only by whether an unreserved character is percent-encoded or appears literally are equivalent by definition, but URI processors, in practice, may not always recognize this equivalence. For example, URI consumers shouldn't treat %41 differently from A or %7E differently from ~, but some do. For maximum interoperability, URI producers are discouraged from percent-encoding unreserved characters.

Percent-encoding the percent character[edit]

Because the percent character ( % ) serves as the indicator for percent-encoded octets, it must be percent-encoded as %25 for that octet to be used as data within a URI.

Percent-encoding arbitrary data[edit]

Most URI schemes involve the representation of arbitrary data, such as an IP address or file system path, as components of a URI. URI scheme specifications should, but often don't, provide an explicit mapping between URI characters and all possible data values being represented by those characters.

Binary data[edit]

Since the publication of RFC 1738 in 1994 it has been specified that schemes that provide for the representation of binary data in a URI must divide the data into 8-bit bytes and percent-encode each byte in the same manner as above.[1] Byte value 0x0F, for example, should be represented by %0F, but byte value 0x41 can be represented by A, or %41. The use of unencoded characters for alphanumeric and other unreserved characters is typically preferred as it results in shorter URLs.

Character data[edit]

The procedure for percent-encoding binary data has often been extrapolated, sometimes inappropriately or without being fully specified, to apply to character-based data. In the World Wide Web's formative years, when dealing with data characters in the ASCII repertoire and using their corresponding bytes in ASCII as the basis for determining percent-encoded sequences, this practice was relatively harmless; it was just assumed that characters and bytes mapped one-to-one and were interchangeable. The need to represent characters outside the ASCII range, however, grew quickly and URI schemes and protocols often failed to provide standard rules for preparing character data for inclusion in a URI. Web applications consequently began using different multi-byte, stateful, and other non-ASCII-compatible encodings as the basis for percent-encoding, leading to ambiguities and difficulty interpreting URIs reliably.
For example, many URI schemes and protocols based on RFCs 1738 and 2396 presume that the data characters will be converted to bytes according to some unspecified character encoding before being represented in a URI by unreserved characters or percent-encoded bytes. If the scheme does not allow the URI to provide a hint as to what encoding was used, or if the encoding conflicts with the use of ASCII to percent-encode reserved and unreserved characters, then the URI cannot be reliably interpreted. Some schemes fail to account for encoding at all, and instead just suggest that data characters map directly to URI characters, which leaves it up to implementations to decide whether and how to percent-encode data characters that are in neither the reserved nor unreserved sets.
Common characters after percent-encoding (ASCII or UTF-8 based)
newlinespace"%-.<>\^_`{|}~
%0A or %0D or %0D%0A%20%22%25%2D%2E%3C%3E%5C%5E%5F%60%7B%7C%7D%7E
Arbitrary character data is sometimes percent-encoded and used in non-URI situations, such as for password obfuscation programs, or other system-specific translation protocols.

Current standard[edit]

The generic URI syntax recommends that new URI schemes that provide for the representation of character data in a URI should, in effect, represent characters from the unreserved set without translation, and should convert all other characters to bytes according to UTF-8, and then percent-encode those values. This suggestion was introduced in January 2005 with the publication of RFC 3986. URI schemes introduced before this date are not affected.
Not addressed by the current specification is what to do with encoded character data. For example, in computers, character data manifests in encoded form, at some level, and thus could be treated as either binary or character data when being mapped to URI characters. Presumably, it is up to the URI scheme specifications to account for this possibility and require one or the other, but in practice, few, if any, actually do.

Non-standard implementations[edit]

There exists a non-standard encoding for Unicode characters: %uxxxx, where xxxx is a UTF-16 code unit represented as four hexadecimal digits. This behavior is not specified by any RFC and has been rejected by the W3C. The eighth edition of ECMA-262 still includes an escape function that uses this syntax, along with encodeURI and encodeURIComponent functions, which apply UTF-8 encoding to a string, then percent-escape the resulting bytes.[2]

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...