Friday, February 22, 2013

Extract a value of an XML type attribute using WSO2 ESB

Let's say you have an XML input with some elements as below.

<SOAP-ENV:Body>
        <Publish>
            <Notification
                message="&lt;RegistraionNotificationEvent NotificationType=&quot;Place&quot;>&lt;DeviceLocPlace
                timestamp=&quot;1361210060855&quot; accuracy=&quot;344.0&quot;
                macAddress=&quot;7b:29:a8:8b:e4:f8&quot; lastSeen=&quot;7&quot;
                sourceTimestamp=&quot;1361209904211&quot; x=&quot;171.34&quot; y=&quot;104.513&quot;/>&lt;/RegistraionNotificationEvent>" />
        </Publish>
    </SOAP-ENV:Body>

 You need to extract out the macAddress which is inside the message attribute. How can we do that?


If you closely look at this input, you would see, the macAddress stays inside the value of the attribute "message". But this message attribute carries another XML element which comes as a String. 

Now what? 

This is where WSO2 ESB Script mediator comes into rescue.
With just the following piece of configuration, you can extract out the  Mac Address.

<script language="js">
      <![CDATA[mc.setPayloadXML(new XML(mc.getPayloadXML()..*::Notification.@message.toXMLString()));]]></script>

The above segment converts the value of the attribute message into XML.

<property xmlns:ns="http://org.apache.synapse/xsd" name="mac" expression="//@macAddress" scope="default" type="STRING"/>

Using this segment, from the XML obtained above, you can extract out the macAddress part easily.

As handy as that...

Tuesday, February 5, 2013

Write Data to Cassandra with UTF-8 (String) validators

Cluster cassandraCluster = HFactory.createCluster("TestCluster",
                new CassandraHostConfigurator("localhost:9161"), credentials);
        Keyspace keyspace = HFactory.createKeyspace(keyspaceName, cassandraCluster);
       
        BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();
        columnFamilyDefinition.setKeyspaceName(keyspaceName);
        columnFamilyDefinition.setName(columnFamily);   
      columnFamilyDefinition.setComparatorType(ComparatorType.UTF8TYPE); 
 columnFamilyDefinition.setDefaultValidationClass(ComparatorType.UTF8TYPE.getClassName());
columnFamilyDefinition.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());
       
        ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition);
       
        KeyspaceDefinition keyspaceDefinition =
            HFactory.createKeyspaceDefinition(keyspaceName, "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef));
        cassandraCluster.addKeyspace(keyspaceDefinition);
       
       
        KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace(keyspaceName);
        cfDef = fromCluster.getCfDefs().get(0);
       
        columnFamilyDefinition = new BasicColumnFamilyDefinition(cfDef);
       
        BasicColumnDefinition columnDefinition = new BasicColumnDefinition();
       
        columnDefinition.setName(StringSerializer.get().toByteBuffer("lat"));
        columnDefinition.setIndexType(ColumnIndexType.KEYS);
        columnDefinition.setIndexName("lat");

columnDefinition.setValidationClass(ComparatorType.UTF8TYPE.getClassName());
        columnFamilyDefinition.addColumnDefinition(columnDefinition);
       
        columnDefinition = new BasicColumnDefinition();
        columnDefinition.setName(StringSerializer.get().toByteBuffer("lon"));   
        columnDefinition.setIndexType(ColumnIndexType.KEYS);
        columnDefinition.setIndexName("lon");

 columnDefinition.setValidationClass(ComparatorType.UTF8TYPE.getClassName());
        columnFamilyDefinition.addColumnDefinition(columnDefinition);
       
        columnDefinition = new BasicColumnDefinition();
        columnDefinition.setName(StringSerializer.get().toByteBuffer("timestamp"));   
  columnDefinition.setIndexType(ColumnIndexType.KEYS);
        columnDefinition.setIndexName("timestamp");
       columnDefinition.setValidationClass(ComparatorType.UTF8TYPE.getClassName());

        columnFamilyDefinition.addColumnDefinition(columnDefinition);
       
        cassandraCluster.updateColumnFamily(new ThriftCfDef(columnFamilyDefinition));
       
        for (int i = 90; i < 100; i++) {
            Mutator<String> mutator = HFactory.createMutator(keyspace, sser);
            String dID = Integer.toString(i);
            mutator.insert(dID, columnFamily, HFactory.createStringColumn("lat", lat));
            mutator.insert(dID, columnFamily, HFactory.createStringColumn("lon", lon));
            mutator.insert(dID, columnFamily,
                    HFactory.createStringColumn("timestamp", timestamp));
            mutator.execute();
        }



 You can check whether the created CFs are in proper and according to the required validation methods, by using a Cassandra-CLI tool. How to use this tool?
Refer my previous blog post       

A Smart and Quick Cassandra Client

Recently I wanted to get the internal details of a created Column Family. Here how I did that. 
  • Download the latest Cassandra server from here.
  • Extract to a preferred location. This will be known as CASS_HOME
  • Start up the Cassandra Server. For my task, I was using the built-in cassandra support that comes with WSO2 CEP. (The Cassandra server was running in localhost:9161)
  • Navigate to CASS_HOME/bin. From there execute the command below.
 sh cassandra-cli -h localhost -p 9161 -u admin -pw admin
  • The above command will take you to the Cassandra CLI console. Type the command below to get the key spaces:
show keyspaces;
  • That would show up fine details like validation class and comparator type etc. as follows:
Keyspace: LocationKeySpaceTestDemo_25:
  Replication Strategy: org.apache.cassandra.locator.SimpleStrategy
  Durable Writes: true
    Options: [replication_factor:1]
  Column Families:
    ColumnFamily: LocationDataTestDemo_25
      Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type
      Default column value validator: org.apache.cassandra.db.marshal.UTF8Type
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
      DC Local Read repair chance: 0.0
      Replicate on write: false
      Caching: KEYS_ONLY
      Bloom Filter FP chance: default
      Column Metadata:
        Column Name: lon
          Validation Class: org.apache.cassandra.db.marshal.UTF8Type
        Column Name: lat
          Validation Class: org.apache.cassandra.db.marshal.UTF8Type
        Column Name: timestamp
          Validation Class: org.apache.cassandra.db.marshal.UTF8Type
      Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy
      Compression Options:
        sstable_compression: org.apache.cassandra.io.compress.SnappyCompressor
WARNING: Could not connect to the JMX on localhost:7199, information won't be shown.