├── .gitignore ├── README ├── examples ├── hive-sql-checker-0.1.jar └── run_checher.sh ├── pom.xml └── src └── main ├── java └── net │ └── hql │ └── checker │ └── HiveQueryChecker.java └── resources └── run_checher.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.ipr 2 | *.iws 3 | *.iml 4 | .idea 5 | .classpath 6 | out/* 7 | logs/* 8 | tests/log/* 9 | sorl/logs/* 10 | *.log 11 | .DS_Store 12 | *.swp 13 | *~ 14 | .project 15 | *.kpf 16 | webapps/solr.war 17 | .settings 18 | target 19 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Usage: 2 | 1. push dir examples to the machine which set up HIVE. 3 | 2. go into examples 4 | 3. use shell to check hive query, like command: sh run_checker.sh "select * from test.uuid limit 100;" 5 | 6 | -------------------------------------------------------------------------------- /examples/hive-sql-checker-0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayanhui/Hive-SQL-Syntax-Checker/a28ffcdefb91c65a2896681c31a2653196b443cd/examples/hive-sql-checker-0.1.jar -------------------------------------------------------------------------------- /examples/run_checher.sh: -------------------------------------------------------------------------------- 1 | HIVE_QUERY=$1 2 | echo "java -cp hive-sql-checker-0.1.jar net.hql.checker.HiveQueryChecker \"$HIVE_QUERY\"" 3 | 4 | java -cp hive-sql-checker-0.1.jar net.hql.checker.HiveQueryChecker "$HIVE_QUERY" 5 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | hive-sql 6 | hive-sql-checker 7 | 0.1 8 | jar 9 | 10 | hive-sql-checker 11 | 12 | 13 | 14 | org.apache.thrift 15 | libthrift 16 | 0.9.0 17 | 18 | 19 | org.apache.hive 20 | hive-common 21 | 0.9.0 22 | 23 | 24 | org.apache.hive 25 | hive-service 26 | 0.9.0 27 | 28 | 29 | commons-codec 30 | commons-codec 31 | 1.3 32 | 33 | 34 | commons-fileupload 35 | commons-fileupload 36 | 1.2.1 37 | 38 | 39 | commons-httpclient 40 | commons-httpclient 41 | 3.1 42 | 43 | 44 | commons-io 45 | commons-io 46 | 1.4 47 | 48 | 49 | org.slf4j 50 | jcl-over-slf4j 51 | 1.5.5 52 | 53 | 54 | org.slf4j 55 | slf4j-api 56 | 1.5.5 57 | 58 | 59 | org.slf4j 60 | slf4j-jdk14 61 | 1.5.5 62 | 63 | 64 | org.codehaus.woodstox 65 | wstx-lgpl 66 | 3.2.7 67 | 68 | 69 | javax.xml.stream 70 | stax-api 71 | 1.0 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-jar-plugin 80 | 81 | 82 | 1.6 83 | 1.6 84 | 88 | 89 | 90 | zh.solr.se.indexer.IndexerMain 91 | true 92 | 93 | 94 | 95 | 96 | 97 | org.apache.maven.plugins 98 | maven-dependency-plugin 99 | 100 | 101 | 102 | unpack-indexer-dependencies 103 | process-classes 104 | 105 | unpack 106 | 107 | 108 | 109 | 110 | commons-codec 111 | commons-codec 112 | 1.3 113 | 114 | 115 | commons-fileupload 116 | commons-fileupload 117 | 1.2.1 118 | 119 | 120 | commons-httpclient 121 | commons-httpclient 122 | 3.1 123 | 124 | 125 | commons-io 126 | commons-io 127 | 1.4 128 | 129 | 130 | org.slf4j 131 | jcl-over-slf4j 132 | 1.5.5 133 | 134 | 135 | org.slf4j 136 | slf4j-api 137 | 1.5.5 138 | 139 | 140 | org.slf4j 141 | slf4j-jdk14 142 | 1.5.5 143 | 144 | 145 | org.codehaus.woodstox 146 | wstx-lgpl 147 | 3.2.7 148 | 149 | 150 | javax.xml.stream 151 | stax-api 152 | 1.0 153 | 154 | 155 | org.apache.thrift 156 | libthrift 157 | 0.9.0 158 | 159 | 160 | org.apache.hive 161 | hive-common 162 | 0.9.0 163 | 164 | 165 | org.apache.hive 166 | hive-service 167 | 0.9.0 168 | 169 | 170 | org.apache.hive 171 | hive-metastore 172 | 0.9.0 173 | 174 | 175 | org.apache.thrift 176 | libfb303 177 | 0.7.0 178 | 179 | 180 | ${project.build.outputDirectory} 181 | 182 | 183 | 184 | 185 | 186 | maven-compiler-plugin 187 | 188 | 1.6 189 | 1.6 190 | 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /src/main/java/net/hql/checker/HiveQueryChecker.java: -------------------------------------------------------------------------------- 1 | package net.hql.checker; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.hadoop.hive.service.ThriftHive; 6 | import org.apache.thrift.protocol.TBinaryProtocol; 7 | import org.apache.thrift.transport.TSocket; 8 | 9 | public class HiveQueryChecker { 10 | public static final String HOST = "127.0.0.1"; 11 | public static final int PORT = 10000; 12 | public static final String HQL_PREFFIX = "EXPLAIN EXTENDED "; 13 | 14 | public void run(String hql) { 15 | TSocket transport = null; 16 | try { 17 | transport = new TSocket(HOST, PORT); 18 | transport.open(); 19 | TBinaryProtocol protocol = new TBinaryProtocol(transport); 20 | 21 | if (null != hql && hql.trim().length() > 1) { 22 | hql = hql.trim(); 23 | if (hql.endsWith(";")) 24 | hql = hql.substring(0, hql.length() - 1); 25 | hql = HQL_PREFFIX + hql; 26 | ThriftHive.Client client = new ThriftHive.Client(protocol); 27 | 28 | client.execute(hql); 29 | List rst = client.fetchAll(); 30 | 31 | System.out.println("HQL Syntax is OK!"); 32 | for (int i = 0; i < rst.size(); i++) { 33 | System.out.println(rst.get(i)); 34 | } 35 | } else 36 | System.out.println("HQL is NULL!"); 37 | 38 | } catch (Exception e) { 39 | System.out.println("HQL Semantic Analysis Exception:"); 40 | e.printStackTrace(); 41 | } finally { 42 | if (null != transport) 43 | transport.close(); 44 | } 45 | 46 | } 47 | 48 | public static void main(String[] args) { 49 | if (args.length != 1) { 50 | System.out.println("sh run_checker.sh \"select * from action.action_20121010 limit 100;\""); 51 | System.exit(1); 52 | } 53 | new HiveQueryChecker().run(args[0]); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/resources/run_checher.sh: -------------------------------------------------------------------------------- 1 | HIVE_QUERY=$1 2 | echo "java -cp hive-sql-checker-0.1.jar net.hql.checker.HiveQueryChecker \"$HIVE_QUERY\"" 3 | 4 | java -cp hive-sql-checker-0.1.jar net.hql.checker.HiveQueryChecker "$HIVE_QUERY" 5 | --------------------------------------------------------------------------------