com.olapdb.obase.data.HPC Maven / Gradle / Ivy
The newest version!
package com.olapdb.obase.data;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import javax.script.ScriptException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
/**
* a.b.c.d = a;
* r = a.b;
* a.b = r;
*
*/
public class HPC{
private StringBuffer sb = new StringBuffer();
public HPC append(String sentence){
sentence = sentence.replaceAll(" ", "");
sb.append(sentence);
if(!sentence.endsWith(";")){
sb.append(";");
}
return this;
}
private HashMap inputs = new HashMap();
public void put(String name, Object v){
inputs.put(name, v);
}
public Object get(String name){
return inputs.get(name);
}
public HPC clear(){
return clearScript().clearVars();
}
public HPC clearScript(){
sb = new StringBuffer();
return this;
}
public HPC clearVars(){
inputs.clear();
return this;
}
public byte[] getBytes(String name) throws ScriptException{
Object value = inputs.get(name);
if(value instanceof Cell){
return CellUtil.cloneValue((Cell)value);
}
if(value instanceof byte[])
return (byte[])value;
throw new ScriptException("var ["+ name +"] can't convert to byte[]");
}
public long getTimeStamp(String name) throws ScriptException{
Object value = inputs.get(name);
if(value instanceof Cell){
return ((Cell)value).getTimestamp();
}
throw new ScriptException("var ["+ name +"] not instance of Cell");
}
public Table getTable(String name) throws ScriptException{
Object value = inputs.get(name);
if(value instanceof Table)
return (Table)value;
throw new ScriptException("var ["+ name +"] can't convert to Table");
}
@SuppressWarnings("unchecked")
public List getList(String name) throws ScriptException{
Object value = inputs.get(name);
if(value instanceof List)
return (List)value;
throw new ScriptException("var ["+ name +"] can't convert to List");
}
public boolean exist(String name){
return inputs.containsKey(name) && get(name)!=null;
}
public boolean isBytes(String name){
return exist(name) && ((get(name) instanceof byte[]) || (get(name) instanceof Cell)) ;
}
public boolean isList(String name){
return exist(name) && (get(name) instanceof List);
}
public boolean isTable(String name){
return exist(name) && (get(name) instanceof Table);
}
// private static Hashtable tables = new Hashtable();
public HPC set(String name, Table v){
put(name, v);
return this;
}
public HPC set(String name, byte[] v){
put(name, v);
return this;
}
public HPC set(String name){
return set(name, name);
}
public HPC setList(String name){
return set(name, new Vector());
}
public HPC set(String name, String v){
put(name, Bytes.toBytes(v));
return this;
}
public HPC set(String name, boolean v){
put(name, Bytes.toBytes(v));
return this;
}
public HPC set(String name, short v){
put(name, Bytes.toBytes(v));
return this;
}
public HPC set(String name, int v){
put(name, Bytes.toBytes(v));
return this;
}
public HPC set(String name, long v){
put(name, Bytes.toBytes(v));
return this;
}
public HPC set(String name, float v){
put(name, Bytes.toBytes(v));
return this;
}
public HPC set(String name, double v){
put(name, Bytes.toBytes(v));
return this;
}
public HPC set(String name, Result v){
put(name, v.listCells());
return this;
}
public HPC set(String name, List v){
put(name, v);
return this;
}
public HPC set(String name, Put v) throws IOException{
List cells = new Vector();
CellScanner cs = v.cellScanner();
while(cs.advance()){
cells.add(cs.current());
}
put(name, cells);
return this;
}
// public void put(String def, byte[] bytes){
// }
//
// public Result get(String def){
// return null;
// }
public HPC exec() throws ScriptException{
String script = sb.toString();
String[] commands = script.split(";");
for(int i=0; i pures = new Vector();
for(String s : vars){
if(s != null){
pures.add(s);
}
}
if(pures.size()>4){
throw new ScriptException("too much segs: " + exp);
}
Ana ret = new Ana();
ret.vars = pures.toArray(new String[0]);
ret.ts = ts;
if(isTable(vars[0])){
ret.remote = true;
}
return ret;
}
private Object eval(Ana ana) throws ScriptException{
if(ana.vars.length == 1 && ana.vars[0].equals("null"))
return null;
//add
// if(ana.vars.length == 1 && get(ana.vars[0]) == null)
// return null;
//
for(String var : ana.vars){
if(!exist(var))
throw new ScriptException("not found: " + var);
}
if(ana.remote)
return evalRemote(ana);
else
return evalLocal(ana);
}
private Object evalRemote(Ana ana) throws ScriptException{
if(ana.vars.length<2 || ana.vars.length >4)
throw new ScriptException("evalRemote: paras count not correct: " + ana.vars.length);
for(int i=1; i raws = res.listCells();
if(raws == null)return null;
if(ana.vars.length == 4){
if(raws.size() == 0)
return null;
if(raws.size() >1)
throw new ScriptException("evalRemote: error");
return raws.get(0);
}
return raws;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ScriptException(e.toString());
}
}
private Object evalLocal(Ana ana) throws ScriptException{
if(ana.vars.length == 0)
return null;
if(ana.vars.length == 1)
return get(ana.vars[0]);
if(!isList(ana.vars[0]))
throw new ScriptException("expression ["+ana.exp+"] var "+ana.vars[0]+" not List object");
for(int i=1; i4)
throw new ScriptException("assiRemote: paras number not correct: " + ana.vars.length);
for(int i=1; i)value, ana.ts);
break;
case 3: //table row cf
fillPut(put, getBytes(ana.vars[2]), (List)value, ana.ts);
break;
case 4: //table row cf column
fillPut(put, getBytes(ana.vars[2]), getBytes(ana.vars[3]), (byte[])value, ana.ts);
break;
}
try {
// put.setDurability(Durability.SKIP_WAL);
getTable(ana.vars[0]).put(put);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ScriptException(e.toString());
}
}
private void fillPut(Put put, List value, Long ts){
for(Cell c : value){
if(ts == null)
put.addColumn(CellUtil.cloneFamily(c), CellUtil.cloneQualifier(c), CellUtil.cloneValue(c));
else
put.addColumn(CellUtil.cloneFamily(c), CellUtil.cloneQualifier(c), ts, CellUtil.cloneValue(c));
}
}
private void fillPut(Put put, byte[] family, List value, Long ts){
for(Cell c : value){
if(ts == null)
put.addColumn(family, CellUtil.cloneQualifier(c), CellUtil.cloneValue(c));
else
put.addColumn(family, CellUtil.cloneQualifier(c), ts, CellUtil.cloneValue(c));
}
}
private void fillPut(Put put, byte[] family, byte[] column, byte[] value, Long ts){
if(ts == null || ts<0)
put.addColumn(family, column, value);
else
put.addColumn(family, column, ts, value);
}
private void assignmentLocal(Ana ana, Object value) throws ScriptException{
if(ana.vars.length == 1){
put(ana.vars[0], value);
return;
}
for(int i=1; i4)
throw new ScriptException("assiRemote: error " + ana.vars.length);
List container = getList(ana.vars[0]);
switch(ana.vars.length){
case 2: //write row
CUtil.assiRow(container, getBytes(ana.vars[1]), value, ana.ts);
break;
case 3: //table row cf
CUtil.assiFamily(container, getBytes(ana.vars[1]), getBytes(ana.vars[2]), value, ana.ts);
break;
case 4: //table row cf column
CUtil.assiColumn(container, getBytes(ana.vars[1]), getBytes(ana.vars[2]), getBytes(ana.vars[3]), value, ana.ts);
break;
}
}
private class Ana {
String exp;
boolean remote = false;
String[] vars;
Long ts;
}
}
| | | | | | | | | | |
© 2015 - 2025 Weber Informatics LLC | Privacy Policy