Data processing with Kafka
December 24, 2016
Kafka can be an extremely powerful tool for data pre/post processing especially when one deals with a high number of raw data messages.
Kafka becomes very useful both on the processing side and contingency storage system.
Kafka will help when you downstream components will be offline, storing them in queues and deliver them once the system is back online.
On the other hand, Kafka can help you enrich the incoming data with a series of meta-tags, calculation, aggregation, etc
Let’s take a specific example, an apache log line coming from the audit system:
67.131.107.5 - - [12/Mar/2004:11:39:31 -0800] "GET /twiki/pub/TWiki/TWikiLogos/twikiRobot46x50.gif HTTP/1.1" 200 2877
This translates into the following logical format:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
The following elements can be processed to become an enrichment so in the downstream can be used as an index or column to be used for analytics or aggregation:
%h is the remote host (ie the client IP)
%l is the identity of the user determined by indentation (not usually used since not reliable)
%u is the user name determined by HTTP authentication
%t is the time the request was received.
%r is the request line from the client. (“GET / HTTP/1.0”)
%>s is the status code sent from the server to the client (200, 404 etc.)
%b is the size of the response to the client (in bytes)
Referer is the page that linked to this URL.
User-agent is the browser identification string.
This is the Kafka consumer that would apply the transformation to the messages:
switch (msg.get("type").asText()) {
case "log":
System.out.println(msg.toString());
String [] data = msg.get("message").asText().split(" ");
Arrays.stream(data).forEach( f -> System.out.println(f));
((ObjectNode)msg).put("SOME1", data[0]);
((ObjectNode)msg).put("SOME2", data[0]);
((ObjectNode)msg).put("TIMESTAMP", data[0]);
((ObjectNode)msg).put("URl", data[0]);
((ObjectNode)msg).put("PROTOCOL", data[0]);
((ObjectNode)msg).put("IP", data[0]);
break;
default:
throw new IllegalArgumentException("Illegal message type: " + msg.get("type"));
}
break;
Multiple Sheet Excel Report with JasperReports
December 14, 2013In this post we will try to create an excel export with 2 sheet using Dynamic Jasper library.
In order to finish this tutorial we will need to clone the following git repository.
https://github.com/rbrn/spring-dynamic-jasper-huge.git
Requirements:
Java JDK
Maven
Import the project in you IDE and from JUnit run XlsxMultipleSheetsReportTest
Wait for the report to be created and inspect the code.
Spring Java Huge Exports with Dynamic Jasper with source coded
December 1, 2013In this post we will try to create an excel export of 100.000 records using Dynamic Jasper library. For the ease of the setup we will use a maven project that we had taken from the internet and used as a platform
In order to finish this tutorial we will need to clone the following git repository.
https://github.com/rbrn/spring-dynamic-jasper-huge.git
Requirements:
Java JDK
Maven
Import the project in you IDE and from JUnit run XlsxReportTest
Wait for the report to be created, sometimes it takes some time especially if in debug mode.
Note:
Even if this test was very successful as you may notice, this framework didn’t pass the test of the real world application. When it comes to complex objects (talking about 150 complex properties with many sub-properties where a lot of reflection is done) it becomes less of a help. It’s a memory monster. Even using the virtualizer implementation.
Upcoming Flex Apache sdk 4.8.0 incubating
May 4, 2012For the curious and crazy, new upcoming flex sdk from apache
Granite generation problem with Enum astract method or anonymous classes
March 7, 2012If you have to generate as model and you have Enums to translate sometimes there just a need to use an abstract method the gas generator will go crazy generating no name as files. Here’s the fix
<excludeJavaClasses>
<excludeJavaClass>mypackage.model.*$*</excludeJavaClass>
</excludeJavaClasses>
Test Parsley/Cairngorm commands with mockito-flex
February 8, 2012[RunWith(“org.mockito.integrations.flexunit4.MockitoClassRunner”)]
public class MyClassCommandTest{
[Mock(“com.mypackage.MyClassFacade”)]
public var mockie:MyClassFacade;
private var command:MyClassCommand=new MyClassCommand();
[Test]
public function successfulExecuteCommandTest():void {
var repository:MockRepository = new MockRepository();
mockie = MyClassFacade(repository.createStrict(MyClassFacade));
var request:MyEvent = new MyEvent (new MyObject ());
Expect.call(mockie.create(MyObject(request.eventData))).returnValue(AsyncHelper.createToken(CommandResponseFactory.createValidResponse()));
repository.replayAll();
command.service = mockie;
command.execute(request);
repository.verifyAll();
}
Migrating from FlexMojos 3.9 to 4.x
January 23, 2012I must warn you, it will be a very tedious operation to make, especially if you have a complex multi-module structure.
Also at the time being there is no production ready release. It’s true we have 2 release candidate for 4.0 though the 4.1 is again in beta.
Here are some hints. What changes.
1) maven 3.x.x is necessary so don’t start unless you have everything set up already.(And this is another adventure)
Gas generation
<beanTemplate> and <enumTemplate> definition into
<templates>
<enum-template>${project.build.directory}/generatorTemplates/enum.gsp</enum-template>
<base-entity-template>${project}/generatorTemplates/bean.gsp
</base-entity-template>
<entity-template>${basedir}/src/main/template/bean.gsp</entity-template>
<base-bean-template>${project.build.directory}/generatorTemplates/beanBase.gsp
</base-bean-template>
<bean-template>${project.build.directory}/generatorTemplates/bean.gsp</bean-template>
</templates>
definesDeclaration becomes defines with the same property definition
<includeAsClasses> get <includeClasses>
A new depedency is necessary if you are using flex3
<dependency> <groupId>org.sonatype.flexmojos</groupId> <artifactId>flexmojos-flex3-compatibility-layer</artifactId> <version>${flexmojo.version}</version> </dependency>
<configuration><compiledLocals<locale>en_US</locale></compiledLocals></configuration>
<configuration> <localesCompiled> <locale>en_US</locale> </localesCompiled> </configuration>
Update:
If you are using Intellij for your project there might be some issues using the hotdeploy. In this case the best way would be using Intellij 11 which generates it’s own config file but at this time would be still not an official release but an EAP. In anycase the fix to make it work is to add to the configuration file produced by the compilation process the file-spec compiler option.
<file-specs> <path-element>C:\Documents and Settings\....myproject\MyMainFile.mxml</path-element> </file-specs>
Get also some inspiration from
https://dev.c-ware.de/confluence/display/PUBLIC/Migrating+from+Flexmojos+3+to+Flexmojos+4
Spark Grid Column Drag and Drop or Move
January 18, 2012So what does this class have to prove:
1) Adobe didn’t provide with drag and drop column for the spark datagrid. The developers job is to provide this for himself, if needed.
2) Even for such a simple functionality which before was granted, can be very tricky and quite difficult to create and optimize.
3) That sharing can be useful. Hopefully people will use it and find problems.
package com.costin.components
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;
import mx.core.DragSource;
import mx.core.IFactory;
import mx.core.IFlexDisplayObject;
import mx.core.UIComponent;
import mx.events.DragEvent;
import mx.events.FlexEvent;
import mx.managers.DragManager;
import spark.components.DataGrid;
import spark.components.gridClasses.GridColumn;
import spark.components.gridClasses.IGridItemRenderer;
import spark.events.GridEvent;
public class DragAndDropGrid extends DataGrid
{
private var dragColumn:GridColumn;
private var dropIndex:int;
private var dropIndicator:DisplayObject;
private var lastXMousePosition:int;
private var dragTimer:Timer = new Timer(100, 1000);
/**
* horizontal scroll position back variable
* after drop column the scroll position is lost
* and is set back by setScrollBackWhereItWas in the callLater method trigger
*/
protected var _horizontalScrollBeforeDrop:int;
public function DragAndDropGrid()
{
super();
this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
}
private function creationCompleteHandler(event:FlexEvent):void {
addEventListener(GridEvent.GRID_MOUSE_DRAG, gridMouseDragHandler);
}
/**
* handler the column header dragging
*/
protected function gridMouseDragHandler(event:GridEvent):void {
if (DragManager.isDragging)
return;
if (event.rowIndex == -1) {
startColumnDragDrop(event);
}
}
private function startColumnDragDrop(event:GridEvent):void {
dragColumn = event.column;
var ds:DragSource = new DragSource();
ds.addData(dragColumn, "spark.components.gridClasses.GridColumn");
var factory:IFactory = dragColumn.headerRenderer;
if (!factory)
factory = this.columnHeaderGroup.headerRenderer;
var renderer:IGridItemRenderer = IGridItemRenderer(factory.newInstance());
renderer.visible = true;
renderer.column = dragColumn;
renderer.label = dragColumn.headerText;
var currentRenderer:IGridItemRenderer = this.columnHeaderGroup.getHeaderRendererAt(event.columnIndex);
renderer.width = currentRenderer.width;
renderer.height = currentRenderer.height;
renderer.prepare(false);
var pt:Point = new Point(0, 0);
pt = DisplayObject(currentRenderer).localToGlobal(pt);
pt = DisplayObject(this).globalToLocal(pt);
DragManager.doDrag(this as UIComponent, ds, event, renderer as IFlexDisplayObject, -pt.x, -pt.y);
renderer.owner = this;
this.columnHeaderGroup.addEventListener(DragEvent.DRAG_ENTER, columnDragEnterHandler);
this.columnHeaderGroup.addEventListener(DragEvent.DRAG_COMPLETE, columnDragCompleteHandler);
this.addEventListener(DragEvent.DRAG_COMPLETE,columnDragCompleteHandler);
dragTimer.addEventListener(TimerEvent.TIMER, timerActivityHandler);
dragTimer.start();
}
protected function timerActivityHandler(event:TimerEvent):void{
if(stage.mouseX < 130){
if(stage.mouseX > 90){
grid.horizontalScrollPosition -= 150;
}else
grid.horizontalScrollPosition -= 90;
this.invalidateDisplayList();
}else if(stage.mouseX > stage.stageWidth - 130){
if(stage.mouseX > stage.stageWidth - 90){
grid.horizontalScrollPosition += 150;
}else
grid.horizontalScrollPosition += 90;
};
}
private function stopdragTimer(str:String=null):void{
dragTimer.stop();
}
/**
*
* @param event
* Stops the scrolling timer when a column gets droped on the datagrid
*
*/
private function parentGridDragCompleteHandler(event:Event):void{
stopdragTimer("parentGridDragCompleteHandler");
}
private function columnDragEnterHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("spark.components.gridClasses.GridColumn")) {
this.columnHeaderGroup.addEventListener(DragEvent.DRAG_OVER, columnDragOverHandler);
this.columnHeaderGroup.addEventListener(DragEvent.DRAG_EXIT, columnDragExitHandler);
this.columnHeaderGroup.addEventListener(DragEvent.DRAG_DROP, columnDragDropHandler);
}
// remove this condition if you want cross table drag and drop
if(DataGrid(event.dragInitiator).columnHeaderGroup == this.columnHeaderGroup){
showColumnDropFeedback(event);
DragManager.acceptDragDrop(this.columnHeaderGroup);
}
}
private function columnDragOverHandler(event:DragEvent):void {
showColumnDropFeedback(event);
DragManager.acceptDragDrop(this.columnHeaderGroup);
}
private function columnDragExitHandler(event:DragEvent):void {
this.columnHeaderGroup.removeEventListener(DragEvent.DRAG_OVER, columnDragOverHandler);
this.columnHeaderGroup.removeEventListener(DragEvent.DRAG_EXIT, columnDragExitHandler);
this.columnHeaderGroup.removeEventListener(DragEvent.DRAG_DROP, columnDragDropHandler);
cleanUpDropIndicator();
}
private function columnDragDropHandler(event:DragEvent):void {
this.columnHeaderGroup.removeEventListener(DragEvent.DRAG_OVER, columnDragOverHandler);
this.columnHeaderGroup.removeEventListener(DragEvent.DRAG_EXIT, columnDragExitHandler);
this.columnHeaderGroup.removeEventListener(DragEvent.DRAG_DROP, columnDragDropHandler);
dropColumn(event);
}
private function columnDragCompleteHandler(event:DragEvent):void {
this.columnHeaderGroup.removeEventListener(DragEvent.DRAG_ENTER, columnDragEnterHandler);
cleanUpDropIndicator();
stopdragTimer("columndragcomplete");
}
private function cleanUpDropIndicator():void {
if (dropIndicator) {
dropIndex == -1;
this.columnHeaderGroup.overlay.removeDisplayObject(dropIndicator);
dropIndicator = null;
}
}
private function showColumnDropFeedback(event:DragEvent):void {
var pt:Point = new Point(event.stageX, event.stageY);
pt = this.columnHeaderGroup.globalToLocal(pt);
var newDropIndex:int = this.columnHeaderGroup.getHeaderIndexAt(pt.x, pt.y);
if (newDropIndex != -1) {
var renderer:IGridItemRenderer = this.columnHeaderGroup.getHeaderRendererAt(newDropIndex);
if (!dropIndicator) {
dropIndicator = new DragDropIndicator;
this.columnHeaderGroup.overlay.addDisplayObject(dropIndicator);
}
if (pt.x < renderer.x + renderer.width / 2)
dropIndicator.x = renderer.x - dropIndicator.width / 2;
else {
dropIndicator.x = renderer.x + renderer.width - dropIndicator.width / 2;
newDropIndex++;
}
dropIndex = newDropIndex;
}
else {
cleanUpDropIndicator();
}
}
/**
*
* @param event
* Backup the horizontalScrollPosition
*/
protected function saveScrollerPositionForCallLater(event:DragEvent):void{
_horizontalScrollBeforeDrop = this.grid.horizontalScrollPosition;
}
/**
* Sets the scroller to the initial value
* before the columnDrop as it looses it during the reordering
*/
protected function setScrollBackWhereItWas():void{
this.grid.horizontalScrollPosition = _horizontalScrollBeforeDrop;
}
private function dropColumn(event:DragEvent):void {
if (dropIndex != dragColumn.columnIndex) {
var oldIndex:int = dragColumn.columnIndex;
this.columns.removeItemAt(dragColumn.columnIndex);
if (dropIndex > oldIndex)
dropIndex--;
this.columns.addItemAt(dragColumn, dropIndex);
saveScrollerPositionForCallLater(event);
callLater(setScrollBackWhereItWas);
}
cleanUpDropIndicator();
stopdragTimer("dropColumn");
}
/**
* @private
* Used to sort the selected indices during drag and drop operations.
*/
private function compareValues(a:int, b:int):int {
return a - b;
}
/**
* @private
*/
private function copySelectedItemsForDragDrop():Vector.<Object> {
// Copy the vector so that we don't modify the original
// since selectedIndices returns a reference.
var draggedIndices:Vector.<int> = this.selectedIndices.slice(0, this.selectedIndices.length);
var result:Vector.<Object> = new Vector.<Object>(draggedIndices.length);
// Sort in the order of the data source
draggedIndices.sort(compareValues);
// Copy the items
var count:int = draggedIndices.length;
for (var i:int = 0; i < count; i++)
result[i] = this.dataProvider.getItemAt(draggedIndices[i]);
return result;
}
}
}
import spark.components.Group;
class DragDropIndicator extends Group {
/*
* Default constructior for the drop indicator
*/
public function DragDropIndicator() {
this.graphics.beginFill(0);
this.graphics.drawRect(0, 0, 4, 20);
this.graphics.endFill();
this.width = 4;
}
}
Here is the github link with the demo:
https://github.com/rbrn/apache-flex-drag-drop-columndatagrid
Flex and Apache Fundation – flex finally accepted
December 31, 2011Flex has been finally accepted to the incubator with the apache foundation, happy new year flex.