Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/main/java/org/finra/hiveqlunit/syntax/Check.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
74 changes: 74 additions & 0 deletions src/test/java/org/finra/hiveqlunit/syntax/CheckTest.java
Original file line number Diff line number Diff line change
@@ -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 <Authors name>
* @since <pre>May 8, 2017</pre>
* @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;



}


}