1 2 Previous Next

otaviojava

26 posts

The Stream API , the api whose was born in Java 8, with this resource you can interact with collections easily. This first topic will cover "why we should use it?". To put in practice I will create a simple model, a soccer team.

 


public class Team {
  private List<Player> players;
//…
}

public class Player {

  private String name;

  private int gols;

  private Position position;

//...
}

public enum Position {
GOALKEEPER,DEFENDER, FORWARDS
}

 

 

With this model, the next step is manipulate the soccer player. First this code will use just resources from Java 7. The first method will return just the forwarders.

 


   public List<Player> getForwarders() {
  List<Player> forwarders = new ArrayList<>();
  for (Player player : players) {
    if(player.isFowarder()) {
       forwarders.add(player);
     }
  }
  return Collections.unmodifiableList(forwarders);
  }

 

 

Further, the next method will return just the defenders.

 


    public List<Player> getDefenders() {
  List<Player> defenders = new ArrayList<>();
  for (Player player : players) {
     if(player.isDefender()) {
        defenders.add(player);
     }
   }
  return Collections.unmodifiableList(defenders);
  }

 

 

When we compare the two methods, we can see similar thing in this code, actually, in the code just has one difference: The condition to add in the list. There is the possibility that the next methods follow the same logic. So we'll refactor to use the same code, basically using a Filter interface.

 


public interface PlayerFilter {
  boolean filter(Player player);
} 

 

 

 

Now, the same code using the filter interface.

 


public List<Player> getForwarders() {
  return getFilteredPlayers(new PlayerFilter() {
@Override
  public boolean filter(Player player) {
return player.isFowarder();
  }
  });
  }

  public List<Player> getDefenders() {

  return getFilteredPlayers(new PlayerFilter() {
@Override
  public boolean filter(Player player) {
return player.isDefender();
  }
  });
  }

  private List<Player> getFilteredPlayers(PlayerFilter filter) {

  List<Player> filteredPlayers = new ArrayList<>();
  for (Player player : players) {
if(filter.filter(player)) {
filteredPlayers.add(player);
  }
  }
  return Collections.unmodifiableList(filteredPlayers);

  }

 

 

Just using lambda expressions, but not using Stream yet, we can make the code clearer.

 


    public List<Player> getForwarders() {
  return getFilteredPlayers(player -> player.isFowarder());
  }

  public List<Player> getDefenders() {
  return getFilteredPlayers(player -> player.isDefender());
  }

 

 

The code clearer, now using method reference:

 


   public List<Player> getForwarders() {
     return getFilteredPlayers(Player::isFowarder);
  }

  public List<Player> getDefenders() {
     return getFilteredPlayers(Player::isDefender);
  } 

 

 

But does anyone experienced this? The Stream API is a new data structure to work with stream in either sequential or parallel in an easier way.

 

Now, this time just using Stream, We can see how clear is our code using this new API, without make a new interface Filter.

 


  public List<Player> getForwarders() {
     return players.stream().filter(Player::isFowarder).collect(Collectors.toList());
  }

  public List<Player> getDefenders() {
     return players.stream().filter(Player::isDefender).collect(Collectors.toList());
  }

 

 

It's not good practice allow the client of the API modify the list, in other words, we should to return a read only list to the client. So, we can just add the “collectingAndThen” with the “Collections::unmodifiableList”.

 


    public List<Player> getForwarders() {

  return players.stream().filter(Player::isFowarder).collect(Collectors.
collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
  }

  public List<Player> getDefenders() {
  return players.stream().filter(Player::isDefender).collect(Collectors.
collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
  }

 

 

 

The result of the code using the newest API from Java 8.

 

 


public class Player {

  private String name;

  private int gols;

  private Position position;

  public Player(String name, int gols, Position position) {
  this.name = name;
  this.gols = gols;
this.position = position;
  }

  public String getName() {
  return name;
  }

  public int getGols() {
  return gols;
  }

  public Position getPosition() {
  return position;
  }


  public boolean isFowarder() {
  return FORWARDER.equals(position);
  }

  public boolean isDefender() {
  return DEFENDER.equals(position);
  }


} 

 


public class Team {

  private List<Player> players;

  public Team(List<Player> players) {
  this.players = players;
  }

  public List<Player> getForwarders() {

  return players.stream().filter(Player::isFowarder).collect(Collectors.
collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
  }

  public List<Player> getDefenders() {
  return players.stream().filter(Player::isDefender).collect(Collectors.
collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
  }


} 

 


public enum Position {
GOALKEEPER,DEFENDER, FORWARDS
} 

 


public class App
{
  public static void main( String[] args )
  {
  List<Player> players = new ArrayList<>();
players.add(new Player("Bobo", 10, Position.FORWARDER));
players.add(new Player("Zé Carlos", 8, Position.FORWARDER));
players.add(new Player("Lima", 4, Position.DEFENDER));
players.add(new Player("Lima", 14, Position.DEFENDER));
players.add(new Player("Ronaldo", 1, Position.GOALKEEPER));
  Team bahia = new Team(players);
  List<Player> defenders = bahia.getDefenders();
  List<Player> forwarders = bahia.getForwarders();
  }
}

 

 

 

 

Referência:


http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html

Otavio Santana

Money-API with Java EE

Posted by Otavio Santana Nov 29, 2015

The Java applications that use Java EE it is becoming more popular, This grow up happen mainly on 2009 after launched the Java EE 6 with CDI. A good benefits to use Java EE it's a Java specification, so there isn't locking vendor.

 

The Java EE is the most popular platform in the corporative world, these softwares have many goals. What they have in common is the fact represent money anyway. The monetary amount is really trivial in several systems such as finance applications, library, etc. However there was not a standard to work with money in Java. For many years many Java developers have been using primitives types from Java such as String, Long, Integer, etc. There are some problems when the developer use this solution like break the SOLID, lost encapsulation. So, now to solve this problem there is the JSR 354, the money-api, is a Java specification that was developed to many companies.

 

This article will use the current version of Java EE, the Java EE 7, and it will imagine a book store.

 

JPA

 

The Book model has three attributes.

 

  • Id
  • Name
  • value

 

@Entity 
public class Book implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id; 

@Column 
private String name; 

@Column 
private MonetaryAmount value; 

//getter and setter 
    
}

A common problem in a custom type, such money, that the database cannot support this kind of information. A solution that became possible in JPA 2.1 is using converters. To create a converter is really simple, you just need to implement the AttributeConverter interface.

 

public class MonetaryAmountConveter implements  AttributeConverter { 

    private static final CurrencyUnit CURRENCY = Monetary.getCurrency("BRL"); 
    
    private static final MonetaryQuery EXTRACT_BIG_DECIMAL = (m) -> m.getNumber() 
            .numberValue(BigDecimal.class); 
            
    @Override 
    public BigDecimal convertToDatabaseColumn(MonetaryAmount attribute) { 
       return Optional.ofNullable(attribute).orElse(FastMoney 
               .zero(CURRENCY)) 
               .query(EXTRACT_BIG_DECIMAL); 
    } 

    @Override 
    public MonetaryAmount convertToEntityAttribute(BigDecimal dbData) { 
        return Money.of(dbData, CURRENCY); 
    } 
} 

 

The second step is define the converter in the field with Convert annotation.

 @Column
 @Convert(converter = MonetaryAmountConveter.class)
 private MonetaryAmount value;

 

In JPA 2.1 the converters work with just one field, so we have some options:

 

  • Save just number: In application just has one currency, it's possible, define a currency as constant. This way is possible do operations in the database as sum directly in the database, nevertheless if the project grow up the internationalization became a problem.
  • Save the number and currency as String: This strategy is enough the make the software to support many countries, notwithstanding it's hard to do database operations, so sum money just in a Java layer.
  • Save number and currency in different fields: Unfortunately it's not available in JPA 2.1, but you can use the Hibernate to do it, it's similar in JPA, implement an interface, the CompositeUserType. Now you can do database operations, however do operations in database there isn't guarantee if the currencies are equals and rounding operations, so can happen a mistake.

 

The Jadira is a framework that has several implementations to types in hibernate. Jadira has support to both JSR 310 and JSR 354.

 

CDI

 

It isn't nice to the Book entity be responsible to define the currency, it isn't its responsibility, break the SOLID. There are many strategies to create currency:

Create a currency from user locale.

Create a currency from tenant, for example, the serve “.com.br” the currency will be Brazilian real, “com.us” dollar.

Neither currency from user and currency from tenant is good option have the code spread in all the packages, to solve it there is CDI that work as glue in Java EE application.

 

public class MoneyProducer { 

    @Inject 
    private HttpServletRequest request; 

    @Produces 
    @RequestScoped 
    public CurrencyUnit getCurrency() { 
        return Monetary.getCurrency(request.getLocale()); 
    } 
} 
public class MoneyProducer { 

    @Inject 
    @Named("money_from_configurations") 
    private String money; 

    @Produces 
    @RequestScoped 
    public CurrencyUnit getCurrency() { 
        return Monetary.getCurrency(money); 
    } 
}

 

With the currency producer defined from CDI, we can inject “magically”. To it doesn't have several injection point to currency. A good idea is have a class specific to create money.

 

 

public class MonetaryAmountFactory { 

    @Inject 
    private CurrencyUnit currency; 
    
    
    public MonetaryAmount create(Number number) { 
        return Money.of(number, currency); 
    } 

} 

 

JAX-RS

 

Such as in JPA, JAX-RS there is a converter interface. Implement the interface ParamConverterProvider and annotated with @Provider. This interface has just one method and return the ParamConverter interface.

 

@Provider 
public class MonetaryAmountConverterProvider implements ParamConverterProvider { 


    @Override 
    public  ParamConverter getConverter(Class rawType, Type genericType, Annotation[] annotations) { 
        if (MonetaryAmount.class.isInstance(rawType)) { 
            return new ParamConverter() { 
                @Override 
                public T fromString(String value) { 
                    if(value == null || value.isEmpty()) { 
                        return null; 
                    } 
                    return rawType.cast(Money.parse(value)); 
                } 

                @Override 
                public String toString(T value) { 
                    if(value == null) { 
                        return null; 
                    } 
                    return value.toString(); 
                } 
            }; 
        } 
        return null; 
    } 
} 


@Provider 
public class CurrencyUnitConverterProvider implements ParamConverterProvider { 

    private CurrencyUnitParamConverter converter = new CurrencyUnitParamConverter(); 

    @Override 
    public  ParamConverter getConverter(Class rawType, Type genericType, Annotation[] annotations) { 
        if (CurrencyUnit.class.isInstance(rawType)) { 
            return new ParamConverter() { 
                @Override 
                public T fromString(String value) { 
                    if(value == null || value.isEmpty()) { 
                        return null; 
                    } 
                    return rawType.cast(Monetary.getCurrency(value)); 
                } 

                @Override 
                public String toString(T value) { 
                    if(value == null) { 
                        return null; 
                    } 
                    return value.toString(); 
                } 
            }; 
        } 
            return null; 
    } 
}

 

JSF

 

Interaction with the user is an important point in the applications, allow to an user read and write information is really important in several applications. The JSF is a specification to make it possible, such as other JSRs there is a converter to JSF.

 

@FacesConverter("money.midas.CurrencyConverter") 
public class CurrencyConverter implements Converter { 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, 
            String value) { 
         
        if (Objects.isNull(value)) { 
            return null; 
        } 
        return Monetary.getCurrency(value); 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, 
            Object value) { 
        if (Objects.isNull(value)) { 
            return null; 
        } 
        return value.toString(); 
    } 

} 

@FacesConverter("money.midas.MoneyConverter") 
public class MoneyConverter implements Converter { 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, 
            String value) { 
         
        if (Objects.isNull(value)) { 
            return null; 
        } 
        return Money.parse(value); 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, 
            Object value) { 
        if (Objects.isNull(value)) { 
            return null;
        } 
        return value.toString(); 
    } 
}

 

The Midas project

 

To do integrations between money-api with others frameworks, there is the Midas project. The goal of Midas is get support to:

  • JSF
  • Bean Validation
  • JPA
  • JAX-RS
  • CDI
  • Google Guice
  • Spring

 

In the midas, all converters of JSF, JPA, JAX-RS that was showed in this article is in midas. Beyond there is support to bean validation:

  • CurrencyAccepted: Define the currencies will be allowed in the either Currency and MonetaryAmount. You can inform either currency code or locale where the currency is from.
  • CurrencyRejected: Define the currencies will be rejected in the either Currency and MonetaryAmount. You can inform either currency code or locale where the currency is from.
  • MonetaryMax: Define the maximium value to the money.
  • MonetaryMin: Define the minimum value to the money.

 

    @CurrencyAccepted(currencies = "BRL") 
    private CurrencyUnit currencyUnit; 

    @CurrencyAccepted(currencies = "BRL") 
    @MonetaryMax("10.12") 
    private MonetaryAmount money;

 

References

Since begin of the time, the man need to do exchanges of goods materials or services, began with simples goods exchanges. In this time, already has necessity to start the standardization process, the first step was using metals as gold, silver and bronze becoming to representative money today. Nowadays with large number of e-commerce and automation of finance market and another systems that take care of money. The question frequently done is: How take care of this important measure unit? This article will talk about this point.

The first question is, witch variable use to represent the money? Integer, Long, Double, Float or BigDecimal? In Java Effective's book, told the problem to use floating representation, so we should avoid float and double and use BigDecimal, long or int.

public class Product { 
    private String name; 
    private BigDecimal price; 
    //getter and setter
}

Using BigDecimal, for example, we will have some problems, such show the value with monetary symbol, rounding, etc. So some developers make a MoneyUtils (a class with utilities of money), to help to do that. The problem will be spread and smell code. A good solution to solve this problem is create the Money's class, so centralize all money's behavior in just one object (show monetary symbol, etc.), this solutions look like more beautiful and has more OO aspect.

public class Product { 
    private String name; 
    private Money price;    
            //getter and setter 
}

But with a strong globalization and the buys from the Internet, is important worry to another monetary units, beyond your country, so born new challenges:

  • Find rates
  • Store currency to each country
  • Exchange rate
  • Understand the world standardization to currencies (ISO 4217)

With this goal was born the money API, JSR 354, to make easy the money's manipulation such rounding, exchange rate, etc.

Starting to talk about this API, the first step is create the currency unit, it is represented using the CurrencyUnit's interface, it's possible make a instance some forms as inform the currency code or using java.util.Locale of country of origin.

public class CreateCurrencyUnit 
{ 
    public static void main( String[] args ) 
    { 
        CurrencyUnit real = MonetaryCurrencies.getCurrency("BRL"); 
        CurrencyUnit dollar = MonetaryCurrencies.getCurrency(Locale.US); 
        System.out.println(real); //BRL

<p>Redis is a NOSQL database written in C. The Remote Dictionary Server is a key-value data base whose the storage is in memory, then the write and read will fastest way, but which difference between Redis and Cache? What does happen when the database fall down? Will we lost the all informations? The main goal of this article is talk about the Redis and show an open source project, the redis-collection.</p> <p> Analyzing cache tools such memcache and infinispan, also has this behavior with key-value, the first question that a Java developers does is: Which difference about Redis and cache?. To start, the serialization of the values, while the caches write in binary way, with Kryo or java.io.Serializable, in Redis everything is text or String. The problem in binary serialization is the impossibility of change the file manually or using another language. When Kryo or java.io.Serializable are used if change the object structure, you cannot retrieve the information with this structure, however write and read is faster than using text. There is a reason to it, caches are made to be faster but also temporary. Redis is a database and can storage this information, doing backup in hard driver, this way, if Redis fall down it can retrieves without lost all informations. Another dissemblance, in Redis there are another structures inside:</p> <br/> <li>String key and value</li> <li>List: are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis (similar in Java with java.util.List).</li> <li>Set: are an unordered collection of Strings constants with list you cannot put repetitive String (similar in Java with java.util.Set).</li> <li>Sorted Set: are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score,</li> <li>Hashes: are maps between string fields and string values, so they are the perfect data type to represent objects (simillar in Java with Map<String, String>).</li> <br/> <p>Beyond there are more two, Bit arrays and HyperLogs, but will not mentioned in this article.</p> <p>After explain divergences between Redis and cache, next step will install the Redis that is very easy:</p> <br/> <ol> <li>Do the Download here: <a href="http://redis.io/" > http://redis.io/</a></li> <li> Uncompress and then compile the Redis: -> tar zxvf redis-version.x.tar.gz -> cd redis-version.x -> make </li> <li>Last step is run redis: cd src -> ./redis-server</li> </ol> <br/> <p>Done, Redis is running. To do some test, just execute the native client, just go to REDIS_HOME/src, next, run ./redis-cli.</p> <p>Feel free to run any commands, to know the commands in redis: <a href="http://redis.io/commands" >http://redis.io/commands</a></p> <p>After Redis ran and tested. Will used some data structures on Redis, some popular structures in Java: java.util.List, java.util.Set, java.util.Queue e java.util.Map, and more three: key-value as cache, counter and the last one to ranking.<p> <p>You can see the API here:</p> <br/> <pre class="brush:java"> public interface keyValueRedisStructure<T> { T get(String key); void set(String key, T bean); List<T> multiplesGet(Iterable<String> keys); void delete(String key); } public interface CountStructure<T extends Number> { T get(); T increment(); T increment(T count); T decrement(); T decrement(T count); void delete(); void expires(int ttlSeconds); void persist(); } public interface ListStructure <T> extends Expirable { List<T> get(String key); void delete(String key); } public interface MapStructure <T> extends Expirable{ Map<String, T> get(String key); void delete(String key); } public interface QueueStructure <T> extends Expirable { Queue<T> get(String key); void delete(String key); } public interface RankingStructure<T extends Number> extends Expirable { ScoresPoint<T> create(String key); void delete(String key); } </pre> <br/> <br/> <p>The idea of Expirable interface is define on key, the lifecycle, or time to expiry, of the objects in seconds, to remove this lifecycle just use the persist method. Remembering the Redis is in memory, but periodically the data are saved and all information are represented with String. In these structures was chose to serialize and deserialize the objects JSON, because is</p>

The new version of Easy-Cassandra, the version 2.0.1, have launched, some news are:

  • Refactoring constructor on Session Factory
  • QueryBuilder (InsertBuilder, UpdateBuilder, DeleteBuilder, SelectBuilder and BatchBuilder)
  • Go faster both write and read
  • Processo ]]>
  • More supports to Spring-Data

As have an easier the configuration a Session Factory, it was made the ClustInformation, this way do more configuration with less code.

 

 ClusterInformation clusterInformation = ClusterInformation.create() .addHost(HOST) .withDefaultKeySpace(KEY_SPACE).withUser(USER).withPassword(PASS); easyCassandraManager = new EasyCassandraManager(clusterInformation); 

 

This way the Spring Cassandra configuration has changed.

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" <xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <context:component-scan base-package="org.easycassandra.persistence.cassandra.spring" /> <bean id = "clusterInformation" class="org.easycassandra.persistence.cassandra.ClusterInformation"> <property name="keySpace" value="campus" /> <property name="hosts"> <list> <value>localhost</value> </list> </property> </bean> <bean id="cassandraFactory" class="org.easycassandra.persistence.cassandra.spring.CassandraFactoryAnnotation"> <constructor-arg name="clusterInformation" ref="clusterInformation" /> <property name="annotatedClasses"> <list> <value>org.easycassandra.persistence.cassandra.spring.entity.Contact</value> <value>org.easycassandra.bean.model.Step</value> <value>org.easycassandra.bean.model.Weight</value> </list> </property> </bean> <bean id="cassandraTemplate" class="org.easycassandra.persistence.cassandra.spring.SimpleCassandraTemplateImpl" > <constructor-arg name="factory" ref="cassandraFactory" /> </bean> </beans> 

Configuration to Spring data on Easy-Cassandra

Maybe the most important improvement on this version is the QueryBuilder, the querybuilders are easy mode to do interaction with Cassandra, do more with less code, also was included BatchBuilder.

 

 SimpleID id = new SimpleID(); id.setIndex(ONE_HUNDRED_THIRTY); id.setKey(ONE_HUNDRED_THIRTY); UpdateBuilder update = template.updateBuilder(SimpleQueryBuilder.class, key); update.put("map", "name", "otavioMap").value("value", TEN_DOUBLE).execute(); 

Example of update

 UpdateBuilder update = persistence.updateBuilder(SimpleQueryBuilder.class); update.whereEq(Constant.KEY, Constant.ONE).whereEq(Constant.INDEX, Constant.ONE).addList("list", "otavioList").execute(); 
 UpdateBuilder update = persistence.updateBuilder(SimpleQueryBuilder.class); update.whereEq(Constant.KEY, Constant.ONE).whereEq(Constant.INDEX, Constant.ONE).value("value", 12D).executeAsync(new ResultAsyncCallBack() { @Override public void result(Boolean bean) { // do some action } }); 

run an update asynchronously

 InsertBuilder insert= persistence.updateBuilder(SimpleQueryBuilder.class); Set set = new HashSet<>(); set.add("Linda"); Map map = new HashMap<>(); map.put("love", "Otavio and Poliana"); insert.value(Constant.KEY, Constant.ONE_HUNDRED).value(Constant.INDEX, Constant.ONE_HUNDRED).value(Constant.LIST_COLUMN, Arrays.asList("Poliana", "Otavio", "Love")) .value(Constant.SET_COLUMN, set).value("map", map).execute(); 

Do insert

 SimpleID id = new SimpleID(); id.setIndex(ONE_HUNDRED_TWO); id.setKey(ONE_HUNDRED_TWO); SimpleBean simpleBean = new SimpleBean(); simpleBean.setId(id); simpleBean.setValue(VALUE); InsertBuilder insert2 = persistence.updateBuilder(simpleBean); insert2.executeAsync((new ResultAsyncCallBack() { @Override public void result(Boolean bean) { // do some action } }); 

Do an insert asynchronously

 DeleteBuilder delete = persistence.deleteBuilder(SimpleQueryBuilder.class); delete.whereEq(Constant.INDEX, ONE_HUNDRED_TWO).whereEq(Constant.KEY, ONE_HUNDRED_TWO).execute(); DeleteBuilder delete2 = persistence.deleteBuilder(SimpleQueryBuilder.class,"map", "list", "set"); delete2 .whereEq(Constant.INDEX, ONE_HUNDRED_TWO).whereEq(Constant.KEY, ONE_HUNDRED_TWO).execute(); 

The first query remove all columns from key and the second one will remove just the map, list and set columns.

 SimpleID id = new SimpleID(); id.setIndex(ONE_HUNDRED_TWO); id.setKey(ONE_HUNDRED_TWO); DeleteBuilder delete2 = persistence.deleteBuilder(SimpleQueryBuilder.class, id); delete2.executeAsync((new ResultAsyncCallBack() { @Override public void result(Boolean bean) { // do some action } }); 

Removing asynchronously.

 SelectBuilder select = persistence.selectBuilder(SimpleQueryBuilder.class); select.eq("name", "name"); select.in("index", ONE, TWO, THREE); List result = select.execute(); 

 

 SelectBuilder select = persistence.selectBuilder(SimpleQueryBuilder.class); select.eq(NAME, NAME).gt("index", THREE).asc(INDEX).executeAsync(new ResultAsyncCallBack>() { @Override public void result(List beans) { // do some thing } }); 

Run asynchronously the query.

The batch is a feature on Cassandra, that allow you execute modification on database (insert, update and remove) as one, this way you can send inserts, updates and deletes as on request.

 

 DeleteBuilder delete = dao.deleteBuilder(); delete.whereEq(Constant.INDEX, Constant.ONE_HUNDRED_TWO) .whereEq(Constant.KEY, Constant.ONE_HUNDRED_TWO); InsertBuilder insert = dao.insertBuilder(); insert.value(Constant.KEY, Constant.ONE_HUNDRED).value(Constant.INDEX, Constant.ONE_HUNDRED); insert.value(Constant.LIST_COLUMN, Arrays.asList("Poliana", "Otavio", "Love")); UpdateBuilder update = dao.update(); update.whereEq(Constant.KEY, Constant.ONE).whereEq(Constant.INDEX, Constant.ONE); update.addList("list", "otavioList"); BatchBuilder batchBuilder = dao.batchBuilder(); batchBuilder.addOperations(delete, insert, update); batchBuilder.executeAsync(new ResultAsyncCallBack() { @Override public void result(Boolean bean) { // do some action } }); 

Run the inserts, deletes, updates atomically with BatchBuilder.

This article talked about the new version of Easy-Cassandra and show new its features.

  1. https://github.com/otaviojava/Easy-Cassandra/wiki/Builders
  2. https://github.com/otaviojava/Easy-Cassandra/wiki
  3. https://github.com/otaviojava/Easy-Cassandra/
  4. https://github.com/otaviojava/Easy-Cassandra-samples

The Java is a platform which grown up quickly, for many rations such it can write once time and run anywhere, it runs languages on JVM. There is a myth that Java cannot compile its sources on run time, but it's truth?

Have a dynamic language is important to make some projects, for example, calculate taxes to different cities, so the source should be on the database and each city it puts the specific formula.

To show this functionality we will use a sample to solved the problem above, do change of formula on run time, to be easy and get focus on demonstration, we will use the four simple operations (add, multiply, subtract and divide) and to simulate the database, will have the txt file with Java's source inside. How cannot reference a class that was not created yet, we will make a interface to all classes on our

Spring Data no Cassandra com Easy-Cassandra

Spring is an open source framework whose goals is facilitate the Java developer's life, it began with dependency injection and nowadays works on many plug-ins. Plug-ins such Spring Social, to connects social media like twitter and facebbok, Spring security beyond control inversion. The goal of this post is talk about Spring Data working with Cassandra.

Spring Data does part of Spring plug-in, it has as objective do a high level among a data base and Java application. This way, you can use POJO for persist in many kind of data bases.

  • RMDB with JPA and JDBC
  • BigData with Apache Hadoop
  • Data-Grid with GermFire
  • HTTP with Rest
  • NOSQL with MongoDB, Neo4J, Redis and Hbase

The main advantage of Spring data does part of Spring, it may run without Java EE server, it can run a simple container such tomcat or outside of a server. Its dependency injection works together of XML file, but, unlike some developers think, you may use annotations to help, however, you should know when use xml or annotation.

So Spring Data with Cassandra has as objective do a high level layer to NOSQL Cassandra, with POJO structured can do CRUD operations. The Spring Data Cassandra has not launched, but Easy-cassandra has integration with Spring, now you can use Cassandra with most popular frameworks around world.

The application will really simple, it does put information and retrieve by id. Firstly, make the Person entity, our POJO.


 
@Entity(name = "person") 
public class Person implements Serializable { 

    @Id 
    private UUID id; 
     
    @Index 
    @Column(name = "name") 
    private String name; 
    
    @Column(name = "born") 
    private Integer year;
//getter and setter

}

POJO entity Peron


How you can see, the entity had mapped by JPA 20, secondly is configuration with XML file, within has information of Cassandra Base (base, port, default keySpace) is our connections factory Cassandra inside Spring, also was included the mapped class.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<context:component-scan base-package="org.javabahia.cassandra.spring" />  
<bean id = "clusterInformation" class="org.easycassandra.persistence.cassandra.ClusterInformation">
    <property name="keySpace" value="consegi" />
    <property name="hosts">
        <list>
            <value>localhost</value>
        </list>
    </property>
</bean>
<bean id="cassandraFactory" class="org.easycassandra.persistence.cassandra.spring.CassandraFactoryAnnotation"
    <constructor-arg name="clusterInformation" value="clusterInformation" />
    <property name="annotatedClasses">
        <list>
            <value>org.javabahia.cassandra.spring.entity.Person</value>
        </list>
    </property>
</bean>   

Spring Data configuration, that informs configurations of Cassandra (name of host, number of port, default keyspace and mapped class) beyond Cassandra's Template.


With Cassandra connections factory may return a session, the bridge among Cassandra and Easy-cassandra, also will create CassandraTemplase, it an api to do simple operations on Cassandra..

public interface CassandraTemplate{ 
         
        <T> T save(T entity); 
        
        <T> Iterable<T> save(Iterable<T> entities); 
        
        <T> void delete(T entity); 
        
        <T> void delete(Iterable<T> entities); 
        
        <K> void delete(K key, Class<?> entity); 
        
        <K,T> void delete(Iterable<K>  keys, Class<T> entity); 

        <T> void deleteAll(Class<T> entity); 
        
        <T> T update(T entity); 
        
        <T> Iterable<T> update(Iterable<T> entities); 
        
        <T,K> T findOne(K key, Class<T> entity); 
        
        <T,K> List<T> findAll(Iterable<K> keys, Class<T> entity); 
        
        <T> List<T> findAll(Class<T> entity); 
        
        <T,I> List<T> findByIndex(String columnName,I index,Class<T> entity); 
        
        <K,T>boolean exist(K key, Class<T> entity); 

        void executeUpdate(String query); 

CassandraTemplase's operations


Also there is CassandraReposiroty, this abstract class implements CrudRepository of SpringData, to use it is necessary implement the method which returns CassandraTemplate.

@Repository("personRepository")
public class PersonRepository extends CassandraRepository<Person, UUID>{

    
    @Autowired
    private CassandraTemplate cassandraTemplate;
    
    @Override
    protected CassandraTemplate getCassandraTemplate() {
        return cassandraTemplate;
    }

}

PersonRepository with CassandraRepository


 

Whereby CassandraRepository implements the base of persistence on Spring Data, consequently, is available use this operations on Cassandra.

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { 
    <S extends T> S save(S entity); 

    <S extends T> Iterable<S> save(Iterable<S> entities); 

    T findOne(ID id); 

    boolean exists(ID id); 

    Iterable<T> findAll(); 

    Iterable<T> findAll(Iterable<ID> ids); 

    long count(); 

    void delete(ID id); 

    void delete(T entity); 
    void delete(Iterable<? extends T> entities); 

    void deleteAll(); 
}

CrudRepository's Interface of Spring Data


 

Finally run this project.

public class App 
{
    public static void main( String[] args )
    {
        @SuppressWarnings("resource")
        ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
        
        PersonRepository personService=ctx.getBean(PersonRepository.class);
        
        UUID uuid=UUID.randomUUID();
        Person person=new Person();
        person.setId(uuid);
        person.setName("Ot
Cassandra Trip 

Cassandra is an open source column oriented nosql database. It was born to solve problems with persistence and high performance. Cassandra has been used by many companies in many industries for a wide variety of use cases. In addition it has a strong and thriving community supporting it.

We will be having a special series of events throughout Brazil in the first week of September. Each day between September 2th and 6th, we will hold at least one event in different cities throughout the country.

The cities will be:

  • September 2th, Campinas
  • September 3th, S

The applications have grown together with the man dream to get more and more information, then more application have been emerging and the man had became hostage of the application.

For example we can recognized the evolution between man and machine:

In the first time: Many men to just one machine ( like mainframe's age) then one man to one machine ( in to Personal computer era) and the last one is man to many machines ( Nowadays has been appearing devices like smartphones, tablets, PC, Google glasses, watches with Internet, web of things, etc.). With too many softwares, the informations are created in exponential way, so we need to management the data because theirs are important to business rules. Every year they have been increasing of 60%, for example, a company with one thousand of employers makes 1000 terabytes and this volume will have expansion fifty times on 2020.

When the bigdata was born the first difficulty is find the concept, there are many conceits of many people in deferents white paper, blogs, books, etc. The most commons is management a bigger volume of data quickly. But this definition is abstract, the first fail is what is a bigger volume ? To person A a bigger volume of data is one GigaByte and to person B thinks a bigger volume is one hundred of GybaBytes, and the second question is what is faster ? Minutes, hours, seconds, etc is different for each person.

So the challenge of bigdata is manager a big volume of data quickly to do anything like data mining, insert, etc. Makes a application that can grown where it needs is relevant, and to use scalability is a great strategic, it has two kind: the scalability horizontal( increase more hardware like more power's computer and more RAM's memory) and the vertical scalability ( increase number of computer and these work together) the last one has complexity and spends more time, but cheaper because may increase and decrease the number of machine when you need, so in lazy time of your application you use one server but you might have ten if you want, this way an elastic application.

To storage these informations with horizontal scalability, the NOSQL database are a good idea. To understand better NOSQL is not only SQL, one different between NOSQL and SQL is the first one has variety of character between theirs. The usually the NOSQL is faster to read and write a data, however is slower to find him. It may to use other service like the Lucene framework.

With service to find information or find by id in HD is slower than in RAM's memory, so have a device with fast access spend less time to recovery information, call him cache. When you think about cache you should have reflection of:

Have information before of you need, warm up the information, also you can warm up in real time, so when the second request have happened will faster than first one, however kill data is significant, you should know when the information is not necessary or replace with newest information.

A another aspect in bigdata, it is less popular, is speed in developing and modeling, for example, Twitter with many users using a hastag, in a little time there were an improvement to search with these hastags. So the bigdata also is related with a fast software development. In Java's world know more about Java EE 6 and JDK 7 get more productivity. Finaly, the concept of bigdata is too easy, but put in practice is complicated, because you should know many frameworks, methodologies, technologies, many types of banks like NewSQL, SQL, NOSQL, cache, Lucene. The SQL and normalization still are most popular by University but they were made on 1970, when the server had 1kb of RAM's memory and 800kb and nowadays the smartphone is faster than those old servers. In other word, is wrong think in always use the SQL database.  



   In last week was born the SouJavaLivre in Brazil, its main objective is disseminating and help to envolve the java free and  the OpenJDK in country. For does that there are some sub-projects:

  1.    Support to Adopt OpenJDK : With list in Portuguese also some tutorial in Portuguese to help the people to adopt the JVM's reference, but helping and with integration with international community, so all tutorials and courses will be translated to English.
  2.    OpenJDK in universities: To talk about and teach how to work, architecture, JNI for university students or high school students.
  3.    Show successful case about companies and part of the Government that use OpenJDK and exciting other companies to do the something.
  4.    The Brazilian Government and some companies signed a document to use OpenJDK ( Bank of the Brazil, SERPRO, CEF, Ministry of Science and technology, Ministry of the planning).


Some links about the group:


 
    Nowadays there are too many NOSQL database, with different architecture and data structure. However with much variety they share one thing in common: Only search information from the keys. A good option is uses other service to help the NOSQL databases. In this post will how the Lucene does work together a NOSQL database, join two world in one application.     The apache Lucene is an API for searching and indexation of document, written in Java. Its operation is did in two steps: The first: Given the text, the Lucene does indexes in document, it transforms the original text in words-key for be search easy and faster.
Last: Find the document from words-key, done it in the first step.     The biggest advantage to using Lucene is which with him, is high abstraction. The developer does need know about indexation algorithm. The index, from Lucene, may stay in distributed system, so could get more scalability and performance in this tool.
    Talked about Lucene, the next step is join two worlds: NOSQL database and Lucene. For this will created a sample software its objective is resume's storage , it will follow the sequence bellow:
1 some user register him resume. 2 The analyzes of human resources will search the professional starting the resume.
3. The professional can be found with all informations in resume: address, skills, soft-skills, languages, etc.
    The application will be Web, so I gonna use the Java EE platform in latest version, 6.0.
    In Lucene, the indexes are storage from Directory's interface, which has two implementations, for storage in RAM and Hard disc. For get high availability, will use indexation process in RAM memory, but for don't lost the information, the application does backup in hard disk.
For do that is very simple with EJB 3.1 schedule resources, so eventually get what in the memory and put in HD.

 
@Schedule(minute = "*/1", hour = "*")
  public void reindex() {
   
    try {   
      Directory disco = FSDirectory.open(new File(Constantes.getIndexDirectory()));
      luceneManager.backup(directory, disco);
      
    }
    catch (Exception e) {
     Logger.getLogger(ScheduleService.class.getName()).log(Level.SEVERE,
                null, e);
    }

  }


         This way when the application fall out and get up again, will load all in H.D to memory.
@ApplicationScoped
public class LuceneManager implements Serializable{

 private static final long serialVersionUID = -8280220793266559394L;
 
  @Produces
        private Directory directory;
    

    @Inject
    public void init() {
        directory = new RAMDirectory();
        try {
            levantarServico();
        } catch (IOException e) {
         Logger.getLogger(LuceneManager.class.getName()).log(Level.SEVERE,
                    null, e);
        }
    }

    public void levantarServico() throws IOException {
        Directory disco = FSDirectory.open(new File(Constantes.getIndexDirectory()));
        backup(disco, directory);
    }

    public void backup(Directory deDiretorio, Directory paraDiretoria) throws IOException {

        for (String file : deDiretorio.listAll()) {
            deDiretorio.copy(paraDiretoria, file, file); // newFile can be either file, or a new name
        }
    }

}


The next step is create the index responsible to communication between application and nosql database. The Lucene has the hierarchy bellow:
The index consists in Document, Document consists fields which in turn consist the information. The structure storage in Lucene can be:
  • Storage and not indexed: ( The key in NOSQL data base is more probable, because it needs storage in Lucene and in original way, for in the future, recover all field in base).
  • Field not storage and not indexed: Information that needs be meet in precise way ( like document number) or very short (like sex with F for female and M for Male).
  • Fields not storage and indexed: Information like large text (book, for example) or search some portion of the text. The index process in Cassandra consist in remove accentuations and connectives ( pont, comma, 'and', etc.), gets the radical of the words ( engineering and engineer have engineer like radical ) and other things for leave the search faster, but is not necessary storage the information because that is in base.


  
  private Document criarDocumento(Pessoa pessoa) throws IOException {
    Document document = new Document();

    document.add(new Field(Constantes.ESTADO_INDICE,pessoa.getEndereco().getEstado(), Store.YES,
        Index.NOT_ANALYZED_NO_NORMS));
    document.add(new Field(Constantes.ID_INDICE,pessoa.getNickName(), Store.YES,
            Index.NOT_ANALYZED_NO_NORMS,TermVector.WITH_POSITIONS_OFFSETS));

    document.add(new Field(Constantes.TUDO, getConteudoCurriculo(pessoa), Store.NO,
        Index.ANALYZED));

    return document;
  }

The lifecycle of the application will: 
  • The sequence has simple process
  • The user add the information, then send to server
  • The information is persisted in Cassandra
  • The information is indexed in Lucene also storage the ID.
  • When the Human resource analyst does research, will find a document from Lucene, next, get the key which finally recover all information in Cassandra.
















Done! In this way both, insertion and search for data, will be faster, the Cassandra has secondary indexes that may uses to seeks beyond the keys for search, but do it makes the search slower, than use only key. An other form to get search quickly is using cache.
  In this article was talked about a problem in that there are in majority NOSQL, the searh for fields beyond the key. For solve this problem was showed a work together with Lucene. Source: http://softwarelivre.org/otagonsan/codigofonte/cassandra-lucene.rar
http://cssjockey.com/images/google-juice.jpg
   


    Launched the latest Easy-cassandra version, that come with improvements to manage multi-nodes. Use many node is very usefully in Cassandra because avoid bottlenecks, fault tolerance, elasticity.
    After have multi-nodes in Cassandra, the next step is divide task between their. Secondly join the databases like only one ( reply all information among their, read and write from consistency level, etc.).

    The main strategy that Easy-cassandra use is load balance, in other words, for each requisitions uses different client, there are two ways:
Sequential: Each node receives a crescent number, the order begin in 0 to N-1, in N is instance's number, then all answer of Cassandra (update, create, delete) is used a different client following the crescent order, for example:

  • First request is for Client 0 
  • Second request is for client 1 

     When the number reach to final, this order is come back to client number 0.

    Randomly: It is look like sequential way, give number for each Cassandra's client 0 to N-1, after that for each request is generated a random number.

   For do you these strategy on ORM is extremely simple:

  • First Do create instance's Cassandra 
  • Second Do include nodes in Cassandra 
  • Finally Do create either strategy, inform Keys space's name, if there is not It will create automatically. 


    For show those improvements will used a sample:  The main objective is only retract answer or all user for this question:  
Release the newest version of the framework to persist objects in Apache Cassandra in easy way. Among improvement is the JPA annotations, also JPQL. 
  JCassandra jCassandra=persistence.createJCassandra("select * from Person");
Release the newest version of the framework to persist objects in Apache Cassandra in easy way. Among improvement is the JPA annotations, also JPQL. 
  JCassandra jCassandra=persistence.createJCassandra("select * from Person");

Filter Blog

By date: