diff --git a/src/main/java/org/finra/hiveqlunit/syntax/Check.java b/src/main/java/org/finra/hiveqlunit/syntax/Check.java new file mode 100644 index 0000000..a2a9558 --- /dev/null +++ b/src/main/java/org/finra/hiveqlunit/syntax/Check.java @@ -0,0 +1,74 @@ +package org.finra.hiveqlunit.syntax; + +import org.apache.commons.io.FileUtils; +import org.apache.spark.SparkConf; +import org.apache.spark.api.java.JavaSparkContext; +import org.apache.spark.sql.hive.HiveContext; + +import java.io.File; +import java.io.IOException; + +/** + * With a column heder defintion and a sql statement this class can validate the syntax of the sql statement will parse. + * It takes approximately 20-30 seconds since it starts an intance of hiverserver through spark context. + */ +public class Check { + + HiveContext hc; + + + public Check() { + + String header; + String sql; + + SparkConf sparkConf = new SparkConf().setAppName("HiveQLUnit").setMaster("local[1]"); + JavaSparkContext sparkContext = new JavaSparkContext(sparkConf); + + + hc = new HiveContext(sparkContext) ; + + + //Blow away hive meta store before execution + try { + FileUtils.deleteDirectory(new File("/tmp/foo")); + } catch (IOException e) { + e.printStackTrace(); + } + + hc.setConf("hive.metastore.warehouse.dir","/tmp/foo"); + + + } + + public Check(HiveContext hiveContext) { + + String header; + String sql; + + hc = hiveContext; + + } + + + public static boolean verify(String createTableStatement, String sqlStatement ) { + + Boolean isGood = true; + + Check check = new Check(); + + check.hc.runSqlHive(createTableStatement); + + check.hc.runSqlHive(sqlStatement); + + try { + check.hc.runSqlHive(sqlStatement); + } catch (Exception e) { + e.printStackTrace(); + isGood = false; + } + + return isGood; + } + +} diff --git a/src/test/java/org/finra/hiveqlunit/syntax/CheckTest.java b/src/test/java/org/finra/hiveqlunit/syntax/CheckTest.java new file mode 100644 index 0000000..709442b --- /dev/null +++ b/src/test/java/org/finra/hiveqlunit/syntax/CheckTest.java @@ -0,0 +1,74 @@ +package org.finra.hiveqlunit.syntax; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import static org.finra.hiveqlunit.syntax.Check.verify; + +/** + * Created by ciuccioj on 5/8/17. + */ +public class CheckTest { + + /** + * Check Tester. + * + * @author + * @since
May 8, 2017
+ * @version 1.0 + */ + + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: verify(String createTableStatement, String sqlStatement) + * dont run becuase no memeory on github build server + */ + @Ignore + public void testVerify() throws Exception { + + Boolean isValid = verify("Create table if not exists trial (cola string, colb string) " + + "ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ", + "select * from trial"); + + assert isValid; + + + + } + + //failure case with bad syntax + @Ignore + public void testVerify2() throws Exception { + + Boolean isValid = null; + try { + isValid = verify("Create table if not exists trial (cola string, colb string) " + + "ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ", + "select z from trial"); + } catch (Exception e) { + System.out.println("this should Fail"); + e.printStackTrace(); + isValid = false; + } + + assert !isValid; + + + + } + + + } + +