All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nflabs.zeppelin.zengine.ZQL Maven / Gradle / Ivy

Go to download

Zengine is java framework for data analysis on Hadoop. see http://nflabs.github.io/zeppelin/#/zengine

There is a newer version: 0.3.3
Show newest version
package com.nflabs.zeppelin.zengine;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;

import com.nflabs.zeppelin.util.Util;
/**
 * class ZQL parses Zeppelin Query Language (http://nflabs.github.io/zeppelin/#zql) and generate logical plan
 * @author moon
 *
 */
public class ZQL {
	String [] op = new String[]{";", "|", ">>", ">"};
	StringBuilder sb = new StringBuilder();

	/**
	 * Constructor
	 * @throws ZException
	 */
	public ZQL() throws ZException{
		Z.configure();
	}

	/**
	 * Load ZQL statements from string
	 * @param zql Zeppelin query language statements
	 * @throws ZException
	 */
	public ZQL(String zql) throws ZException{
		Z.configure();
		append(zql);
	}
	
	
	/**
	 * Load ZQL statements from URI location
	 * @param uri URI location of text resource which contains ZQL
	 * @throws IOException
	 * @throws ZException
	 */
	public ZQL(URI uri) throws IOException, ZException{
		Z.configure();
		load(uri);
	}
	
	/**
	 * Load ZQL statements from File
	 * @param file File contains text representation of ZQL
	 * @throws ZException
	 * @throws IOException
	 */
	public ZQL(File file) throws ZException, IOException{
		Z.configure();
		load(file);
	}
	
	/**
	 * Load ZQL statements from input stream
	 * @param ins input stream which streams ZQL
	 * @throws IOException
	 * @throws ZException
	 */
	public ZQL(InputStream ins) throws IOException, ZException{
		Z.configure();
		load(ins);
	}
	

	/**
	 * Load ZQL statements from URI location
	 * @param uri URI location of text resource which contains ZQL
	 * @throws IOException
	 * @throws ZException
	 */
	public void load(URI uri) throws IOException{
		FSDataInputStream ins = Z.fs().open(new Path(uri));
		load(ins);
		ins.close();
	}
	
	/**
	 * Load ZQL statements from File
	 * @param file File contains text representation of ZQL
	 * @throws ZException
	 * @throws IOException
	 */
	public void load(File file) throws IOException{ 
		FileInputStream ins = new FileInputStream(file);
		load(ins);
		ins.close();
	}
	
	/**
	 * Load ZQL statements from input stream
	 * @param ins input stream which streams ZQL
	 * @throws IOException
	 * @throws ZException
	 */
	public void load(InputStream ins) throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(ins));
		String line = null;
		while((line = in.readLine())!=null){
			sb.append(line);
		}		
	}
	
	/**
	 * Append ZQL statement
	 * @param s statmetn to add
	 */
	public void append(String s){
		sb.append(s);
	}
	
	/**
	 * Clear ZQL statments
	 */
	public void clear(){
		sb = new StringBuilder();
	}
	
	/**
	 * Compile ZQL statements and return logical plan
	 * @return List of Z. Each Z represent single statement. 
	 * @throws ZQLException
	 */
	public List compile() throws ZQLException{
		return compileZql(sb.toString());
	}
	
	private List compileZql(String stmts) throws ZQLException{
		List zList = new LinkedList();
		Z currentZ = null;
		
		String [] t = Util.split(stmts, op, true);
		String currentOp = null;
		for(int i=0; i0){
			String libName = m.group(1);

			
			String args = m.group(3);
			L l = new L(libName, args);
			
			String params = m.group(2);
			
			if(params!=null){
				params = params.trim();
				params = params.substring(1, params.length() - 1);
				params = params.trim();

				String[] paramKVs = Util.split(params, ',');
				if(paramKVs!=null){					
					for(String kvPair : paramKVs){
						if(kvPair.trim().length()==0) continue;
						
						 String[] kv = Util.split(kvPair, '=');
						 if(kv.length==1){
							 l.withParam(kv[0].trim(), null); 	 
						 } else if(kv.length==2){
							 System.out.println("Param="+kv[0]+","+kv[1]);
							 l.withParam(kv[0].trim(), kv[1].trim()); 	 
						 }						 
					}
				}
			}
			
			return l;
			
		} else {
			return null;
		}
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy