痛点:
自己在工作中主要使用notepad++进行编辑sql,而notepad++并不具备sql关键字自动大写的功能,所以需要通过程序来完成。有的人会说你可以用dbeaver之类的工具来写sql呀? 这类工作确实可以关键字大写,个人习惯用notepad++。
话不多说,上代码。
代码中涉及的keyword.txt文件存放的是hive sql官方定义的关键字,以逗号分隔。关键字如下:
add,admin,after,all,alter,analyze,and,archive,array,as,asc,authorization,before,between,bigint,binary,boolean,both,bucket,buckets,by,cascade,case,cast,change,char,cluster,clustered,clusterstatus,collection,column,columns,comment,compact,compactions,compute,concatenate,conf,continue,create,cross,cube,current,current_date,current_timestamp,cursor,data,database,databases,date,datetime,day,dbproperties,decimal,deferred,defined,delete,delimited,dependency,desc,describe,directories,directory,disable,distinct,distribute,double,drop,elem_type,else,enable,end,escaped,exchange,exclusive,exists,explain,export,extended,external,false,fetch,fields,file,fileformat,first,float,following,for,format,formatted,from,full,function,functions,grant,group,grouping,having,hold_ddltime,hour,idxproperties,if,ignore,import,in,index,indexes,inner,inpath,inputdriver,inputformat,insert,int,intersect,interval,into,is,items,jar,join,keys,key_type,lateral,left,less,like,limit,lines,load,local,location,lock,locks,logical,long,macro,map,mapjoin,materialized,minus,minute,month,more,msck,none,noscan,not,no_drop,null,of,offline,on,option,or,order,out,outer,outputdriver,outputformat,over,overwrite,owner,partialscan,partition,partitioned,partitions,percent,plus,preceding,preserve,pretty,principals,procedure,protection,purge,range,read,readonly,reads,rebuild,recordreader,recordwriter,reduce,regexp,reload,rename,repair,replace,restrict,revoke,rewrite,right,rlike,role,roles,rollup,row,rows,schema,schemas,second,select,semi,serde,serdeproperties,server,set,sets,shared,show,show_database,skewed,smallint,sort,sorted,ssl,statistics,stored,streamtable,string,struct,table,tables,tablesample,tblproperties,temporary,terminated,then,timestamp,tinyint,to,touch,transactions,transform,trigger,true,truncate,unarchive,unbounded,undo,union,uniontype,uniquejoin,unlock,unset,unsigned,update,uri,use,user,using,utc,utctimestamp,values,value_type,varchar,view,when,where,while,window,with,year
hive sql的关键字小写转大写代码如下:
import java.io.{BufferedWriter, FileInputStream, FileWriter}object SqlFormat { def main(args: Array[String]): Unit = { file("D:\workspace\pinko\sql-parse\src\main\resources\test.sql")// val hql = "select * from a"//// println(replaceHql(hql, "select")) } def read(fileName: String): String = { val lines =scala.io.Source.fromInputStream(new FileInputStream(fileName),"utf-8").getLines val sb = new StringBuilder() lines.foreach(sb.append(_).append("n")) return sb.toString } def replaceHql(hql:String, key:String):String = { return hql.replaceAll("\b" + key + "\b",key.toUpperCase) } def file(fileName: String): Unit = { var hql=read(fileName) val keyWord =read("C:\Users\PINKO\Desktop\gz\keyword.txt") val keyArr=keyWord.split(",") for(w <- keyArr) { hql =replaceHql(hql,w) }// println(hql) val bufferWriter =new BufferedWriter(new FileWriter(fileName)) bufferWriter.write(hql) bufferWriter.close }}