package net.juniper.cs;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.ignite.lang.IgniteCallable;
import org.apache.phoenix.compile.QueryPlan;
import org.apache.phoenix.jdbc.PhoenixConnection;
import org.apache.phoenix.mapreduce.PhoenixInputSplit;

public class IgniteLoader<Boolean> implements IgniteCallable<Boolean>, Externalizable{
	private String tableName;
	private Scan scan;
	private Configuration conf;
	
	
	/*public IgniteRegionLoader(Configuration conf, String tableName, Scan scan){
		this.connection = connection;
		this.inputsplit = ip;
		
		this.conf = conf;
		this.tableName = tableName;
		this.scan = scan;
	}*/
	
	public IgniteLoader(){
		
	}
	public IgniteLoader(String tableName, Scan scan, Configuration conf){		
		this.tableName = tableName;
		this.scan = scan;
		this.conf = conf;
	}
	
	@Override
	public Boolean call() throws Exception {
		try {
			Connection connection = (Connection) ConnectionFactory.createConnection(conf);
			Table table = connection.getTable(TableName.valueOf(tableName));
			ResultScanner scanner = table.getScanner(scan);
			Result next = null;
			while(null != (next = scanner.next())){			
				System.out.println(Bytes.toString(next.getRow()));
			}	
		}catch (Exception ex){
			ex.printStackTrace();
		}
		
		
		return null;
		
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		/*out.writeObject(queryPlan);
		out.writeObject(tableName);
		out.writeObject(inputsplit);*/
		
		out.writeObject(tableName);
		out.writeObject(scan);
		out.writeObject(conf);
		
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		tableName = (String) in.readObject();
		scan = (Scan)in.readObject();		
		conf = (Configuration) in.readObject();
	}

}
