Wednesday, 29 May 2013

Quick fix:/nagios/cgi-bin/statusmap.cgi was not found on this server(nagios on ubuntu)


If you get "/nagios/cgi-bin/statusmap.cgi was not found on this server " error when you click on "Map" option on Nagios web-ui, the reason could be that you are missing following libraries:


libgd2-xpm libgd2-xpm-dev


Following are the steps to fox the issue:


1) Install missing libraries:

 sudo apt-get install libgd2-xpm libgd2-xpm-dev

2) Go to Nagios Folder and re-configure it as follows:
      

     cd /usr/nagios-3.2.3/
   ./configure -with-gd-lib=/usr/local/lib/   

3) make cgis

4) sudo make install cgis


5) sudo /etc/init.d/nagios reload



It will fix the issue


Thursday, 23 May 2013

Cassandra-Map Reduce Over Cassandra

Following is the Map Reduce program that will read a column from Cassandra table and the column values to HDFS .

I have only written a Mapper class , because Reducer is not required .

Here is the Driver Class:
public class CassandraDriver {
    private static final String CASSANDRA_HOST = "192.168.213.1";
    private static final String CASSANDRA_PORT = "9160";
    private static final String KEYSPACE = "mykeyspace";
    private static final String COLUMN_FAMILY = "testhadoop1";
    private static final String CONF_COLUMN_NAME1 = "value1";
    private static final String CONF_COLUMN_NAME2 = "value2";

    public static void main(String[] args) throws IOException,
            InterruptedException, ClassNotFoundException {

        Configuration conf = new Configuration();
        Job job = new Job(conf, "Cassandra");
        job.setJarByClass(CassandraDriver.class);
        job.setMapperClass(CassandraMapper.class);

        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);
        job.setInputFormatClass(ColumnFamilyInputFormat.class);

        FileOutputFormat.setOutputPath(job, new Path(
                args[0]));
        ConfigHelper.setRangeBatchSize(job.getConfiguration(), 99);
        ConfigHelper.setInputRpcPort(job.getConfiguration(), CASSANDRA_PORT);
        ConfigHelper.setInputInitialAddress(job.getConfiguration(),
                CASSANDRA_HOST);
        ConfigHelper.setInputPartitioner(job.getConfiguration(),
                "RandomPartitioner");
        ConfigHelper.setInputColumnFamily(job.getConfiguration(), KEYSPACE,
                COLUMN_FAMILY);
        SlicePredicate predicate = new SlicePredicate().setColumn_names(Arrays
                .asList(ByteBufferUtil.bytes(CONF_COLUMN_NAME1),
                        ByteBufferUtil.bytes(CONF_COLUMN_NAME2)));
        ConfigHelper.setInputSlicePredicate(job.getConfiguration(), predicate);

        job.waitForCompletion(true);
    }
}


Here is the Cassandra Mapper:




public class CassandraMapper extends
        Mapper<ByteBuffer, SortedMap<ByteBuffer, IColumn>, NullWritable, Text> {
    private NullWritable mapkey = NullWritable.get();
    private Text word = new Text();

    public void map(ByteBuffer key, SortedMap<ByteBuffer, IColumn> columns,
            Context context) throws IOException, InterruptedException {
        for (IColumn column : columns.values()) {
        
          
               String  value = ByteBufferUtil.string(column.value());
           

            StringTokenizer itr = new StringTokenizer(value);
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(mapkey, word);
            }
        }
    }

}

Tuesday, 7 May 2013

Hive: Accessing Hive Array: Custom UDF


Following is an example Hive Table:

hive> select * from test_jsontable;
OK
id    name    contact    depts    sal
1    emp1    9999999999    ["IT","Electronics"]    10000
2    emp2    9999999929    ["IT","Mechanical"]    10000
3    emp4    9999999989    ["IT","Electronics"]    10000
4    emp4    9999999979    ["IT","Electronics"]    10000
5    emp5    9999999969    ["IT","Mechanical"]    10000
6    emp6    9999999949    ["IT","Mechanical","Production"]    10000
Time taken: 0.086 seconds


If you want to get all records where array "depts" contains  "Electronics" , it can be done as follows:

hive> select * from test_jsontable where depts[0]="Electronics" or depts[1]="Electronics" or depts[2]="Electronics";

However, what if your array contains 100 elements or more , you have to hard code every element of array.So better way to use a custom UDF as follows:

package com.impetus.hive;
import java.util.ArrayList;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
@Description(name = "arraycontains", value = "arraycontains(ArrayList list,String value) - returns true if list contains value ", extended = "Example:\n"
        + " > SELECT * FROM src where arraycontains(arrayname,stringvalue;\n")
public class ArrayContains extends UDF {
    public boolean evaluate(ArrayList list, String value) {
        if (list == null || list.size() < 1) {
            return false;
        }
        if (list.contains(value)) {
            return true;
        } else {
            return false;
        }
    }
}

Create jar file of the project and add it in hive as follows:

hive>add jar /pathto/jar;

Then you create your custom function as follows:

hive>create temporary function arraycontains as 'com.test.hive.ArrayContains';

Now , your UDF is available to use

hive> select * from test_jsontable where arraycontains(depts,"Production");
id    name    contact    depts    sal
6    emp6    9999999949    ["IT","Mechanical","Production"]    10000
Time taken: 11.807 seconds

Hive:Loading Json Array in Hive Table

To load JSON Data in Hive , you would need to use JsonSerde .
Download JsonSerde source code from following link and build it using Maven. Advantage of using this JsonSerde is that you can also handle Hive Array and Maps which will not be handled if you use default JSonSerde.

Once you have build it ,add this jar to Hive

hive>add jar /home/impadmin/hive/hive-0.8.1/jsonserde/Hive-JSON-Serde-1/target/json-serde-1.1-jar-with-dependencies.jar;

Now, for example you have following Json file on HDFS or your local system consists of Json Array

$ cat file1.json
 { "id": "1",  "name": "emp1",  "contact":"9999999999",  "depts":["IT","Electronics"], "sal":"10000" }
 { "id": "2",  "name": "emp2",  "contact":"9999999929",  "depts":["IT","Mechanical"], "sal":"10000" }
 { "id": "3",  "name": "emp4",  "contact":"9999999989",  "depts":["IT","Electronics"], "sal":"10000" }
 { "id": "4",  "name": "emp4",  "contact":"9999999979",  "depts":["IT","Electronics"], "sal":"10000" }
 { "id": "5",  "name": "emp5",  "contact":"9999999969",  "depts":["IT","Mechanical"], "sal":"10000" }



Now create a Hive Table using JsonSerde as follows  :

hive> create table test_jsontable (id string, name string,contact string,depts array<string>,sal string) ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe';
OK
Time taken: 0.417 seconds

 Now, load the JSON Data into the table as follows:

hive> load data local inpath '/path/to/file.json' into table test_jsontable;                                                                
Copying data from file:'/path/to/file.json'
Copying file: file:'/path/to/file.json'
Loading data to table default.test_jsontable
OK
Time taken: 0.18 seconds

hive> select * from test_jsontable;                                                     
OK
id    name    contact    depts    sal
1    emp1    9999999999    ["IT","Electronics"]    10000
2    emp2    9999999929    ["IT","Mechanical"]    10000
3    emp4    9999999989    ["IT","Electronics"]    10000
4    emp4    9999999979    ["IT","Electronics"]    10000
5    emp5    9999999969    ["IT","Mechanical"]    10000
Time taken: 0.089 seconds


 
If the file resides on HDFS , you can load the data as follows:

hive> load data  inpath '/path/to/file.json' into table test_jsontable;

Also, you can create external table as follows:

hive>create external table test_jsontable (id string, name string,contact string,depts array<string>,sal string)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe' location '/path/to/dir/of/jsonfile';

Hive:Loading Json Data in Hive Table


To load JSON Data in Hive , you would need to use JsonSerde .
Download JsonSerde source code from following link and build it using Maven. Advantage of using this JsonSerde is that you can also handle Hive Array and Maps which will not be handled if you use default JSonSerde.
https://github.com/foursquare/Hive-JSON-Serde-1

Once you have build it ,add this jar to Hive

hive>add jar /home/impadmin/hive/hive-0.8.1/jsonserde/Hive-JSON-Serde-1/target/json-serde-1.1-jar-with-dependencies.jar;

Now, for example you have following Json file on HDFS or your local system

$ cat file.json
 { "id": "1",  "name": "emp1",  "contact":"9999999999",  "dept":"IT", "sal":"10000" }
 { "id": "2",  "name": "emp2",  "contact":"9999999929",  "dept":"IT", "sal":"10000" }
 { "id": "3",  "name": "emp4",  "contact":"9999999989",  "dept":"IT", "sal":"10000" }
 { "id": "4",  "name": "emp4",  "contact":"9999999979",  "dept":"IT", "sal":"10000" }

Now create a Hive Table using JsonSerde as follows  :

hive> create table test_jsontable (id string, name string,contact string,dept string,sal string) ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe';
OK
Time taken: 0.057 seconds

Now, load the JSON Data into the table as follows:

hive> load data local inpath '/patht/to/file.json' into table test_jsontable;                                                                
Copying data from file:'/patht/to/file.json'
Copying file: file:'/patht/to/file.json'
Loading data to table default.test_jsontable
OK
Time taken: 0.18 seconds

hive> select * from test_jsontable;                                                                                                                    
OK
id    name    contact    dept    sal
1    emp1    9999999999    IT    10000
2    emp2    9999999929    IT    10000
3    emp4    9999999989    IT    10000
4    emp4    9999999979    IT    10000
Time taken: 0.081 seconds

If the file resides on HDFS , you can load the data as follows:

hive> load data  inpath '/patht/to/file.json' into table test_jsontable;

Also, you can create external table as follows:

hive>create external table test_jsontable (id string, name string,contact string,dept String,sal string)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe' location '/path/to/dir/of/jsonfile';


Cassandra: Alternative of IN clause in CQL


In CQL, IN clause only works if a column is indexed.
For example, here is a  table in cassandra with "rowkey" as primary key

 
cqlsh:mykeyspace> select * from hbtable;
 rowkey | cf      | quantifier | value
--------+---------+------------+-------
  row-2 | colfam1 |      quan2 |  val2
  row-8 | colfam1 |      quan4 |  val8
  row-6 | colfam1 |      quan2 |  val6
  row-3 | colfam1 |      quan3 |  val3
  row-7 | colfam1 |      quan3 |  val7
  row-1 | colfam1 |      quan1 |  val1
  row-5 | colfam1 |      quan4 |  val5
  row-4 | colfam1 |      quan1 |  val4

 
Now, if you execute IN clause on "rowkey", it will work

cqlsh:mykeyspace> select * from hbtable where rowkey in ('row-2','row-3');
 rowkey | cf      | quantifier | value
--------+---------+------------+-------
  row-2 | colfam1 |      quan2 |  val2
  row-3 | colfam1 |      quan3 |  val3

 
However, if you try it on some other column where index is not created it will not work
 

cqlsh:mykeyspace> select * from hbtable where quantifier in ('quan1');
Bad Request: No indexed columns present in by-columns clause with Equal operator

Since you cannot create index on every table, so I have written a Java code that will help to run IN clause on Cassandra table. This code runs in sets of rows, one set contains 50000 so that process does not go out of memory


import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.beans.OrderedRows;
import me.prettyprint.hector.api.beans.Row;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.RangeSlicesQuery;
/*
 *
 * arguments list should be cluster_name, keyspace_name, table_name,column_name,value1,value2  and so on
 *
 *
 *
 * */
public class InFunction {
    public static void main(String[] args) {
        if (args.length < 5) {
            System.out.println("usage <args>");
            System.out
                    .println("args list should be cluster_name,keyspace_name,table_name,column_name,value1,value2,...");
            System.exit(1);
        }
        Cluster cluster = HFactory.getOrCreateCluster(args[0],"192.168.213.11:9160,192.168.213.24:9160,");
        Keyspace keyspace = HFactory.createKeyspace(args[1], cluster);
        Serializer<String> se = StringSerializer.get();
        String r1 = "";
        int rowcount = 50000;// default row count
        RangeSlicesQuery<String, String, String> rangeSlicesQuery = HFactory.createRangeSlicesQuery(keyspace, se, se, se);
        while (rowcount == 50000) {
            rangeSlicesQuery.setColumnFamily(args[2]);
            rangeSlicesQuery.setKeys(r1, "");
            rangeSlicesQuery.setRange("", "", false, 2000000000); // Maximum
                                                                    // number of
                                                                    // columns
                                                                    // that
                                                                    // Cassandra
                                                                    // permits
                                                                    // in a line
            rangeSlicesQuery.setRowCount(50000);
            QueryResult<OrderedRows<String, String, String>> result = rangeSlicesQuery
                    .execute();
            OrderedRows<String, String, String> orderedRows = result.get();
            for (Row<String, String, String> r : orderedRows) {
                if (r.getColumnSlice().getColumnByName(args[3]) != null) {
                    String value = (r.getColumnSlice().getColumnByName(args[3])
                            .getValue());
                    for (int i = 4; i < args.length; i++) {
                        if (value.compareTo(args[i]) == 0) {
                            System.out.println(" " + r);
                        }
                    }
                }
            }
            rowcount = orderedRows.getCount();
            r1 = orderedRows.peekLast().getKey();
        }
    }
}

Cassandra: Read data from table using Hector API


Here is an example Cassandra Table(column Family) "hbtable" in "mykeyspace" :

cqlsh:mykeyspace>select * from hbtable;
rowkey | cf | quantifier | value
--------+---------+------------+-------
row-2 | colfam1 | quan2 | val2
row-1 | colfam1 | quan1 | val1

 Now lets read the data from this table using Hector API:

 public class SimpleReadHector {                                                  
    public static void main(String[] args) {
       
        Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "192.168.213.1:9160, 192.168.213.2.:9160");
           Keyspace keyspace = HFactory.createKeyspace("mykeyspace", cluster);
        Serializer se= StringSerializer.get() ;
// CqlQuery API lets you execute a cql query
        CqlQuery cqlQuery = new CqlQuery(keyspace, se, se,se);
        cqlQuery.setQuery("select * from hbtable");
        QueryResult> result = cqlQuery.execute();
              OrderedRows orderedRows = result.get(); 
       
        for (Row rrderedRows )
        {
            System.out.println(" "+r);
                   }      
       
// Seond way is to use RangeSlice API , where you can provide keys , ranges etc to read the values
        RangeSlicesQuery rangeSlicesQuery =
                HFactory.createRangeSlicesQuery(keyspace, se,se,se);
            rangeSlicesQuery.setColumnFamily("hbtable");           
            rangeSlicesQuery.setKeys("row-2", "");
              rangeSlicesQuery.setRange("", "", false, 4);
            rangeSlicesQuery.setRowCount(2); //if you need just top two rows to be scanned
            QueryResult> result1 = rangeSlicesQuery.execute();
            OrderedRows orderedRows = result1.get();           
                 
            System.out.println("Contents of rows: \n");                      
            for (Row r : orderedRows) {
                System.out.println("   " + r);
       
    }
   
}
}

Cassandra:Write data into table using Hector API


Lets take an example, we have a following file :

$ cat test
row-1 colfam1 quan1 val1
row-2 colfam1 quan2 val2

 Now , we have a table(columnfamily) in Cassandra "hbtable" in "mykeyspace' ,in which we have to load the data from this file . We have four columns in this table  (rowkey , cf , quantifier,value) with data type varchar .
 
Following is example of loading data in to this table using Cassandra Hector API:
 
public class SimpleWriteHector {
    public static void main(String[] args) throws IOException {
        Cluster cluster = HFactory.getOrCreateCluster("Test Cluster","192.168.213.1:9160,192.168.213.2;9160");
        Keyspace keyspace = HFactory.createKeyspace("mykeyspace", cluster);
        Serializer se= StringSerializer.get() ;
        Mutator mutator = HFactory.createMutator(keyspace, se);
        BufferedReader br = new BufferedReader(new FileReader("/home/user/test"));
        String line;
        while((line=br.readLine())!=null)
        {
            String columns[]=line.split("\\s");
             mutator.addInsertion(columns[0], "hbtable", HFactory.createStringColumn("cf",columns[1]))
             .addInsertion(columns[0], "hbtable", HFactory.createStringColumn("quantifier",columns[2]))
             .addInsertion(columns[0], "hbtable", HFactory.createStringColumn("value",columns[3]));
            
        }
        mutator.execute();
        System.out.println("Records inserted");
    }
   
} 

  
Lets check it now in Cassandra :
 
cqlsh:mykeyspace>select * from hbtable;
rowkey | cf | quantifier | value
--------+---------+------------+-------
row-2 | colfam1 | quan2 | val2
row-1 | colfam1 | quan1 | val1

Cassandra:Read Data from table using Thrift API

Here is an example Cassandra Table(column Family) "hbtable" in "mykeyspace" :

cqlsh:mykeyspace>select * from hbtable;
rowkey | cf | quantifier | value
--------+---------+------------+-------
row-2 | colfam1 | quan2 | val2
row-1 | colfam1 | quan1 | val1

 Lets read a line from this using Cassandra Thrift API:

public class SimpleReadThrift {

private static final String HOST = "192.168.1.11";
private static final int PORT = 9160;
private static final ConsistencyLevel CL = ConsistencyLevel.ONE;

public static void main(String[] args) throws InvalidRequestException, TException, NotFoundException, UnavailableException, TimedOutException {
TTransport tr = new TSocket(HOST,PORT);
TFramedTransport tf = new TFramedTransport(tr);
TProtocol proto = new TBinaryProtocol(tf);
Cassandra.Client client = new Cassandra.Client(proto);

tf.open();
client.set_keyspace("mykeyspace");
String cfName="hbtable";
ColumnParent cp = new ColumnParent(cfName);
SlicePredicate predicate = new SlicePredicate();
predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]),ByteBuffer.wrap(new byte[0]), false, 3));
//Specifying how many columns to read

List results = client.get_slice(ByteBuffer.wrap("row-2".getBytes()), cp, predicate, CL); // Reading row-2
for (ColumnOrSuperColumn result : results)
{
Column column = result.column;
System.out.print(new String(column.getName())+" "+new String(column.getValue()) );
}
}
}

Cassandra:Write data into table using Thrift API

Lets take an example, we have a following file :

$ cat test
row-1 colfam1 quan1 val1
row-2 colfam1 quan2 val2


Now , we have a table(columnfamily) in Cassandra "hbtable" in "mykeyspace' ,in which we have to load the data from this file . We have four columns in this table  (rowkey , cf , quantifier,value) with data type varchar .

Following is example of loading data in to this table using Cassandra Thrift API:

public class SimpleWriteThrift {
private static final String UTF8 = "UTF8";
private static final String HOST = "192.168.1.11";
private static final int PORT = 9160;
private static final ConsistencyLevel CL = ConsistencyLevel.ALL;
private static final String FILEPATH="/home/user/test";


public static void main(String[] args) throws InvalidRequestException, TException, UnavailableException, TimedOutException, IOException {
TTransport tr = new TSocket(HOST,PORT);
TFramedTransport tf = new TFramedTransport(tr);
TProtocol proto = new TBinaryProtocol(tf);
Cassandra.Client client = new Cassandra.Client(proto);
tf.open();

client.set_keyspace("mykeyspace");
String cfName= "hbtable";
long timestamp =System.currentTimeMillis();
ColumnParent cp = new ColumnParent(cfName);
BufferedReader br = new BufferedReader(new FileReader(FILEPATH));

String line;
Column c = new Column();
while ( (line=br.readLine())!=null)
{
String [] columns=line.split("\\s");

ByteBuffer userIDKey=ByteBuffer.wrap(columns[0].getBytes());
c.setName("cf".getBytes());
c.setValue(columns[1].getBytes());
c.setTimestamp(timestamp);
client.insert(userIDKey, cp,c, CL);
c.setName("quantifier".getBytes());
c.setValue(columns[2].getBytes());
c.setTimestamp(timestamp);
client.insert(userIDKey, cp,c, CL);
c.setName("value".getBytes());
c.setValue(columns[3].getBytes());
c.setTimestamp(timestamp);
client.insert(userIDKey, cp,c, CL);
}

tf.close();
System.out.println("Data inserted");


Lets check it now in Cassandra :

cqlsh:mykeyspace>select * from hbtable;
rowkey | cf | quantifier | value
--------+---------+------------+-------
row-2 | colfam1 | quan2 | val2
row-1 | colfam1 | quan1 | val1

Cassandra: Create a cluster in cassandra

Cassandra is well suited when it runs in distributed mode in a cluster. It follows peer to peer cluster mode instead of master/slave architecture. That makes Cassandra really good in terms of availability and scalability.

Suppose , I have two machines with ip "192.168.1.11" and " 192.168.1.24". We can create a cluster of these machines as follows:

On both of the systems do the following changes in cassandra.yaml file:

1) listen_address-

for example,

On first machine, listen_address-192.168.1.11
On second machine, listen_address-192.168.1.24

2) rpc_address-
Similarly as listen_address

3) Add both ips as seed nodes on each of the machine.
For example,


seed - "192.16.1.11,192.168.1.24"

Make the above change in both of the systems.

4) Start the cassandra deamon on both of the machines

$CASSANDRA_HOME/bin/cassandra -f 

5) Run the nodetool on any of the system to check as follows:

$CASSANDRA_HOME/bin/nodetool -h 192.168.1.11 ring

Note: Ownership information does not include topology, please specify a keyspace.
Address DC Rack Status State Load Owns Token
125770647146966923544277099442418897726
192.168.1.24 datacenter1 rack1 Up Normal 11.32 KB 49.51% 39864057911651858052466199027576462686
192.168.1.11 datacenter1 rack1 Up Normal 13.52 KB 50.49% 125770647146966923544277099442418897726

$CASSANDRA_HOME/bin/nodetool -h 192.168.1.24 ring

Note: Ownership information does not include topology, please specify a keyspace.
Address DC Rack Status State Load Owns Token
125770647146966923544277099442418897726
192.168.1.24 datacenter1 rack1 Up Normal 11.32 KB 49.51% 39864057911651858052466199027576462686
192.168.1.11 datacenter1 rack1 Up Normal 13.52 KB 50.49% 125770647146966923544277099442418897726

That defines how easily we can scale cassandra cluster. We can add another node similarly.

Note: Time and date on all the systems should be in sync.