(Updated) Java Example Code using HBase Data Model Operations

This is the updated version to previous post on 2012: Java Example Code using HBase Data Model Operations

This time we will use latest stable Hbase (1.2.5), IntelliJ and Maven.

  • My local test environment: Virtual Box of Ubuntu 16 Desktop version.
  • Install HBase: https://hbase.apache.org/book.html#quickstart
    • Note: I use stable version 1.2.5 instead of 2.0.0
    • Open a terminal
      1. start HBase in terminal: bin/start-hbase.sh
      2. start HBase shell: bin/hbase shell
      3. create a table: create ‘myLittleHBaseTable’, ‘myLittleFamily’
  • To install JDK: sudo apt install openjdk-8-jre-headless
  • To install Maven: sudo apt install maven
  • Create a maven project (replace “autofei” to whatever you like): mvn archetype:generate -DgroupId=autofei -DartifactId=autofei -DarchetypeArtifactId=maven-archetype-quickstart
  • Download IntelliJ community version

Open the project in IntelliJ and add following dependencies to the pom.xml:

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-common</artifactId>
  <version>2.7.3</version>
</dependency>
<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase</artifactId>
  <version>1.2.5</version>
</dependency>
<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-client</artifactId>
  <version>1.2.5</version>
</dependency>

Enable auto-import function for IntelliJ to easy your life 🙂

The example code in the old post will still work but with some deprecation warning.

 

Factory Pattern

Following UML [1] describes the key concepts of this pattern:

  • Client is coding on Interface not class, which make the client decoupled from concrete product type
  • Factory encapsulates the creation detail of the product, which make any change of creation is transparent to client

Factory Pattern

The next question is how does Client tell the Factory to create different Product? There are several ways:

  • pass a parameter to the Factory
  • register the product into Factory, using Reflection, etc.
  •  Factory design pattern with abstractions(Factory Method): create Factory for each Product

Reference:

  1. http://www.oodesign.com/factory-pattern.html
  2. http://en.wikipedia.org/wiki/Factory_(object-oriented_programming)

Practice on Fraud

Tools to detect fraud include geolocation, velocity pattern analysis, OFAC list lookups, ‘black-list’ lookups, delivery address verification, computer finger printing technology, identity morphing detection, and basic AVS checks. [1]

Velocity

Velocity of change counts the number of changes between data elements on new transactions with previous ones as a means of identifying suspicious behavior. [2]

Geo IP

Service provider[4]:

  • geoPlugin
  • IP2Location
  • MaxMind
  • Quova
  • Digital Envoy
  • NetGeo
  • Cyscape CountryHawk
  • Digital element
  • IPligence

Device Finger Printing

Device Identification is a technique used to establish a “fingerprint” of a user’s computer or other web access device in order to track their activity and determine linkages between other devices.[3]

OFAC

OFAC (Office of Foreign Assets Control) and AML (Anti Money Laundering) etc. are common regulation for online payment.

Black list

Reference:

  1. http://en.wikipedia.org/wiki/Payment_service_provider
  2. http://www.fraudpractice.com/gl-velchange.html
  3. http://www.fraudpractice.com/fl-deviceid.html
  4. http://en.wikipedia.org/wiki/Geobytes

Java concurrency

Topic

Reference:

Books:

  • The Art of Multiprocessor Programming
  • Java Concurrency in Practice

Java 8 Features and Enhancements

  • Lambda expressions
public class Calculator {
  
    interface IntegerMath {
        int operation(int a, int b);   
    }
  
    public int operateBinary(int a, int b, IntegerMath op) {
        return op.operation(a, b);
    }
 
    public static void main(String... args) {
    
        Calculator myApp = new Calculator();
        IntegerMath addition = (a, b) -> a + b;
        IntegerMath subtraction = (a, b) -> a - b;
        System.out.println("40 + 2 = " +
            myApp.operateBinary(40, 2, addition));
        System.out.println("20 - 10 = " +
            myApp.operateBinary(20, 10, subtraction));    
    }
}
  • Method references
  • Default Methods (Defender methods)
  • A new Stream API.
List<Person> roster;
roster
    .stream()
    .filter(
        p -> p.getGender() == Person.Sex.MALE
            && p.getAge() >= 18
            && p.getAge() <= 25)
    .map(p -> p.getEmailAddress())
    .forEach(email -> System.out.println(email));
  • A new Date/Time API
  • Nashorn, the new JavaScript engine
    • A Next-Generation JavaScript Engine for the JVM
  • Removal of the Permanent Generation

Ref

Java 7 Features and Enhancements

Diamond Operator

  • Old code: Map<String, List<Trade>> trades = new TreeMap<String, List<Trade>> ();
  • New code: Map<String, List<Trade>> trades = new TreeMap <> ();

Using strings in switch statements

  • Switch statements work either with primitive types or enumerated types before 1.7

Automatic resource management

  • Resources such as Connections, Files, Input/OutStreams, etc. should be closed manually by the developer. Behind the scenes, the resources that should be auto closed must implement java.lang.AutoCloseable interface.
		try {
			fos = new FileOutputStream("movies.txt");
			dos = new DataOutputStream(fos);
			dos.writeUTF("Java 7 Block Buster");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
				dos.close();
			} catch (IOException e) {
				// log the exception
			}
		}
	}

	public void newTry() {

		try (FileOutputStream fos = new FileOutputStream("movies.txt");
				DataOutputStream dos = new DataOutputStream(fos)) {
			dos.writeUTF("Java 7 Block Buster");
		} catch (IOException e) {
			// log the exception
		}
	}

Numeric literals with underscores

  • 1000000 (one million) as follows: int million  =  1_000_000

Improved exception handling

	public void newMultiCatch() {
		try {
			methodThatThrowsThreeExceptions();
		} catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {
			// log and deal with all Exceptions
		}

	}

	public void newMultiMultiCatch() {
		try {
			methodThatThrowsThreeExceptions();
		} catch (ExceptionOne e) {
			// log and deal with ExceptionOne

		} catch (ExceptionTwo | ExceptionThree e) {
			// log and deal with ExceptionTwo and ExceptionThree
		}

	}

	public void oldMultiCatch() {
		try {
			methodThatThrowsThreeExceptions();
		} catch (ExceptionOne e) {
			// log and deal with ExceptionOne
		} catch (ExceptionTwo e) {
			// log and deal with ExceptionTwo
		} catch (ExceptionThree e) {
			// log and deal with ExceptionTwo
		}
	}

New file system API (NIO 2.0)

  • File change notifications using WatchService API

Fork and Join
Supporting dynamism

Ref: