本文介绍了在使用Hive JDBC执行程序时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我已经为Hive JDBC编写了这个小程序。最初它运行的很好,但是当我现在试图运行时突然出现错误。 程序: import java.io.FileWriter; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; 公共类SampleHiveProgram { String lyear =; String lquarter =; 字符串driverName =org.apache.hadoop.hive.jdbc.HiveDriver; public static void main(String [] args) { SampleHiveProgram s = new SampleHiveProgram(); s.startHiveThriftServer(); s.quarterTable(); } public void startHiveThriftServer() { try { String cmd =/home/hadoop/sqoop-1.3。 0-cdh3u1 / bin中/ StartHiveThriftServer.sh; //这是在Unix shell中执行的命令 //为shell创建一个进程 ProcessBuilder pb = new ProcessBuilder(bash,-c,cmd); pb.redirectErrorStream(true); //使用它来捕获发送到stderr的消息 Process shell = pb.start(); InputStream shellIn = shell.getInputStream(); //捕获命令的输出 //等待shell完成并获取返回码 //此时,您可以处理由命令 //发出的输出例如,它读取输出并将其写入System.out: int c; ((c = shellIn.read())!= -1) { System.out.write(c); } int shellExitStatus = shell.waitFor(); //关闭流 shellIn.close(); catch(Exception e) { e.printStackTrace(); System.exit(1); public void quarterTable() { try { String start =2010-01- 01\" ; String end =2011-01-01; System.out.println(in quarter table ...); //创建数据库连接 Class.forName(driverName); Connection con = DriverManager.getConnection(jdbc:hive:// localhost:10000 / default,,); String sql = null; Statement stmt = con.createStatement(); ResultSet res = null; sql =drop table TmpQuarterTable; System.out.println(删除季表...); res = stmt.executeQuery(sql); $ b $ //创建季度表 sql =create table TmpQuarterTable(年字符串,季度字符串,季度字符串,季度字符串,季度名字符串)+ 行格式限定字段''终止',\\ '012'存为文本文件; System.out.println(创建季度表...); res = stmt.executeQuery(sql); //创建文件 FileWriter fw = new FileWriter(/ home / hadoop / Quarter.txt); //将字符串日期转换为日历日期 DateFormat formatter = new SimpleDateFormat(yyyy-MM-dd); Date sdate =(Date)formatter.parse(start); 日期edate =(日期)formatter.parse(end); 日历c1 = Calendar.getInstance(); 日历c2 = Calendar.getInstance(); c1.setTime(sdate); c2.setTime(edate); int q = 0; String QuarterEndDate = null; int resultMonthCount = 0; int resultYear = 0; int resultMonth = 0; 日历c3 = Calendar.getInstance(); c3.setTime(c1.getTime()); (c3.compareTo(c2)< = 0) { if(c3.get(Calendar.MONTH)> = 0&& c3.get(Calendar。 MONTH) {q = 1; QuarterEndDate = Integer.toString(c3.get(Calendar.YEAR))+ - 04-01; } else if(c3.get(Calendar.MONTH)> = 3&& c3.get(Calendar.MONTH)< = 5) {q = 2; QuarterEndDate = Integer.toString(c3.get(Calendar.YEAR))+ - 07-01; } else if(c3.get(Calendar.MONTH)> = 6&& c3.get(Calendar.MONTH)< = 8) {q = 3; QuarterEndDate = Integer.toString(c3.get(Calendar.YEAR))+ - 10-01; } else if(c3.get(Calendar.MONTH)> = 9&& c3.get(Calendar.MONTH)< = 11) {q = 4; QuarterEndDate = Integer.toString(c3.get(Calendar.YEAR)+1)+ - 01-01; } //得到QuarterEndDate(YYYY-MM-DD) //将QuarterEndDate分成qdate并创建quarter_end_date String [] qdate = QuarterEndDate.split( - ); 日历quarter_end_date = Calendar.getInstance(); quarter_end_date.set(Integer.parseInt(qdate [0]),Integer.parseInt(qdate [1]),Integer.parseInt(qdate [2])); System.out.println(quarter_end_date:+ quarter_end_date); // YY String YY = Integer.toString(c3.get(Calendar.YEAR)); //季度开始日期=季度结束日期 - 1 日历quarter_start_date = Calendar.getInstance(); quarter_start_date.set(quarter_end_date.get(Calendar.YEAR),quarter_end_date.get(Calendar.MONTH),quarter_end_date.get(Calendar.DATE)); quarter_start_date.add(Calendar.YEAR,-1); //年份字符串年份= Integer.toString(quarter_start_date.get(Calendar.YEAR)); System.out.println(year:+ year); //月份字符串月份=空; (日历.MONTH)< 10) months =0+ Integer.toString(quarter_start_date.get(Calendar.MONTH)); if(quarter_start_date.get(Calendar.MONTH) else months = Integer.toString(quarter_start_date.get(Calendar.MONTH)); System.out.println(month:+ months); //日 String day = null; if(quarter_start_date.get(Calendar.DATE)< 10) day =0+ Integer.toString(quarter_start_date.get(Calendar.DATE)); else day = Integer.toString(quarter_start_date.get(Calendar.DATE)); System.out.println(day:+ day); //添加3个月 resultMonthCount = c3.get(Calendar.MONTH)+ 3; resultYear = c3.get(Calendar.YEAR); resultMonth = resultMonthCount - resultYear * 12; System.out.println(YEAR:+ resultYear); c3.set(Calendar.YEAR,resultYear); System.out.println(MONTH:+ resultMonthCount); c3.set(Calendar.MONTH,resultMonthCount); System.out.println(c3:+ c3.getTime()); // YY,q,startDate,endDate,quartername ------->>写入文件 String QuarterStartDate = year + - + months + - + day; 字符串quarterName = YY +\Q\+ q; fw.write(YY +,+ q +,+ QuarterStartDate +,+ QuarterEndDate +,+ quarterName +\\\); } // 结尾fw.close(); String filepath =/home/hadoop/Quarter.txt; sql =load data local inpath'+ filepath +'覆盖到表TmpQuarterTable; System.out.println(Running:+ sql); stmt.executeUpdate(sql); catch(Exception e) { e.printStackTrace(); System.exit(1); } } } 错误类似于:线程main中的异常java.lang.IncompatibleClassChangeError:class com.facebook.fb303.FacebookService $ Client具有接口org.apache.thrift.TServiceClient作为超类 错误为: 线程main中的异常java.lang.IncompatibleClassChangeError:class com.facebook.fb303.FacebookService $客户端具有接口org.apache.thrift.TServiceClient作为超类在java.lang.ClassLoader.defineClass1(本地方法)在java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)。在需要java.lang.ClassLoader.defineClass(ClassLoader.java:615)在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)在java.net.URLClassLoader.defineClass(的URLClassLoader .java:283)在java.net.URLClassLoader.access处(URLClassLoader.java:58)在java.net.URLClassLoader处 $ 1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method)$ b $ at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader。 java:306)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)$ b $在java.lang.ClassLoader.loadClass(ClassLoader.java:247)在java .lang.ClassLoader.defineClass1(本机方法)在java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)。在需要java.lang.ClassLoader.defineClass(ClassLoader.java:615)。在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)在java.net.URLClassLoader.defineClass(URLClassLoader.java:283)在java.net.URLClassLoader.access $ 000( URLClassLoader.java:58)java.net.URLClassLoader上的 $ 1.run(URLClassLoader.java:197)$ java.util.AccessController.doPrivileged(本地方法)在java.net处 .URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306)sun.misc.Launcher上的 $ java.lang.ClassLoader.loadClass上的(ClassLoader。 java:247) at java.lang.ClassLoader.defineClass1(Native Method)$ b $ java.util.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader。的defineClass(ClassLoader.java:615)在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)在java.net.URLClassLoader.defineClass(URLClassLoader.java:283)在java.net.URLClassLoader.access $ 000(URLClassLoader.java:58)$ b $处java.security.AccessController.doPrivileged java.net.URLClassLoader的$ 1.run(URLClassLoader.java:197) b(纯方法)在java.net.URLClassLoader.findClass处(URLClassLoader.java:190)$ java.util.ClassLoader.loadClass处的(ClassLoader.java:306)$ sun $ mb.Launcher $ AppClassLoader.loadClass(Launcher.java:301)在java.lang.Class Loader.loadClass(ClassLoader.java:247)$ b $在java.lang.ClassLoader.defineClass1(本地方法)$ b $在java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at需要java.lang.ClassLoader.defineClass(ClassLoader.java:615)在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)在java.net.URLClassLoader.defineClass(URLClassLoader.java: 283)在java.net.URLClassLoader.access处(URLClassLoader.java:58)在java.net.URLClassLoader处 $ 1.run(URLClassLoader.java:197)在java.security处 .AccessController.doPrivileged(本地方法)$ b $在java.net.URLClassLoader.findClass(URLClassLoader.java:190)在java.lang.ClassLoader.loadClass(ClassLoader.java:306)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)在java.lang.ClassLoader.loadClass(ClassLoader.java:247)在org.apache.hadoop.hive.jdbc .HiveConnection。< init>(HiveConnection.java:93) at org.apache.h adoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)在java.sql.DriverManager.getConnection(DriverManager.java:582)在java.sql.DriverManager.getConnection(DriverManager。在SampleHiveProgram.quarterTable(SampleHiveProgram.java:64)(SampleHiveProgram.java:22) 我有什么问题?我已经添加了Hive所需的所有jar。解决方案任何机会你都有超过不同版本的节俭和libthrift徘徊?这听起来像是用一个版本的节俭生成的类,但试图在运行时使用不同版本的libthrift。 I have written this small program for Hive JDBC. Initially it is executing fine but when I tried to run now suddenly I got error.Program:import java.io.FileWriter;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class SampleHiveProgram{ String lyear=""; String lquarter=""; String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; public static void main(String[] args) { SampleHiveProgram s=new SampleHiveProgram(); s.startHiveThriftServer(); s.quarterTable(); } public void startHiveThriftServer() { try { String cmd = "/home/hadoop/sqoop-1.3.0-cdh3u1/bin/StartHiveThriftServer.sh"; // this is the command to execute in the Unix shell // create a process for the shell ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd); pb.redirectErrorStream(true); // use this to capture messages sent to stderr Process shell = pb.start(); InputStream shellIn = shell.getInputStream(); // this captures the output from the command // wait for the shell to finish and get the return code // at this point you can process the output issued by the command // for instance, this reads the output and writes it to System.out: int c; while ((c = shellIn.read()) != -1) { System.out.write(c); } int shellExitStatus = shell.waitFor(); // close the stream shellIn.close(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } public void quarterTable() { try { String start="2010-01-01"; String end="2011-01-01"; System.out.println("in quarter table..."); //create connection with database Class.forName(driverName); Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", ""); String sql=null; Statement stmt = con.createStatement(); ResultSet res=null; sql="drop table TmpQuarterTable"; System.out.println("Dropping the Quarter Table..."); res = stmt.executeQuery(sql); //Creating Quarter Table sql="create table TmpQuarterTable (year string, quarter string, quarterstart string, quarterend string, quartername string)" + " ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\012' STORED AS TEXTFILE "; System.out.println("Creating the Quarter Table..."); res = stmt.executeQuery(sql); //create the file FileWriter fw=new FileWriter("/home/hadoop/Quarter.txt"); //convert string date to calendar date DateFormat formatter =new SimpleDateFormat("yyyy-MM-dd"); Date sdate=(Date)formatter.parse(start); Date edate=(Date)formatter.parse(end); Calendar c1=Calendar.getInstance(); Calendar c2=Calendar.getInstance(); c1.setTime(sdate); c2.setTime(edate); int q=0; String QuarterEndDate=null; int resultMonthCount=0; int resultYear =0; int resultMonth =0; Calendar c3=Calendar.getInstance(); c3.setTime(c1.getTime()); while(c3.compareTo(c2)<=0) { if(c3.get(Calendar.MONTH)>=0 && c3.get(Calendar.MONTH)<=2) { q=1; QuarterEndDate=Integer.toString(c3.get(Calendar.YEAR))+"-04-01"; } else if(c3.get(Calendar.MONTH)>=3 && c3.get(Calendar.MONTH)<=5) { q=2; QuarterEndDate=Integer.toString(c3.get(Calendar.YEAR))+"-07-01"; } else if(c3.get(Calendar.MONTH)>=6 && c3.get(Calendar.MONTH)<=8) { q=3; QuarterEndDate=Integer.toString(c3.get(Calendar.YEAR))+"-10-01"; } else if(c3.get(Calendar.MONTH)>=9 && c3.get(Calendar.MONTH)<=11) { q=4; QuarterEndDate=Integer.toString(c3.get(Calendar.YEAR)+1)+"-01-01"; } //Got the QuarterEndDate (YYYY-MM-DD) //split the QuarterEndDate into qdate and create quarter_end_date String[] qdate=QuarterEndDate.split("-"); Calendar quarter_end_date=Calendar.getInstance(); quarter_end_date.set(Integer.parseInt(qdate[0]),Integer.parseInt(qdate[1]),Integer.parseInt(qdate[2])); System.out.println("quarter_end_date : "+quarter_end_date); //YY String YY=Integer.toString(c3.get(Calendar.YEAR)); //quarter start date = quarter end date - 1 Calendar quarter_start_date=Calendar.getInstance(); quarter_start_date.set(quarter_end_date.get(Calendar.YEAR),quarter_end_date.get(Calendar.MONTH),quarter_end_date.get(Calendar.DATE)); quarter_start_date.add(Calendar.YEAR, -1); //year String year=Integer.toString(quarter_start_date.get(Calendar.YEAR)); System.out.println("year : "+year); //month String months=null; if(quarter_start_date.get(Calendar.MONTH)<10) months="0"+Integer.toString(quarter_start_date.get(Calendar.MONTH)); else months=Integer.toString(quarter_start_date.get(Calendar.MONTH)); System.out.println("month : "+months); //day String day=null; if(quarter_start_date.get(Calendar.DATE)<10) day="0"+Integer.toString(quarter_start_date.get(Calendar.DATE)); else day=Integer.toString(quarter_start_date.get(Calendar.DATE)); System.out.println("day : "+day); //adding 3 months resultMonthCount = c3.get(Calendar.MONTH) + 3; resultYear = c3.get(Calendar.YEAR); resultMonth = resultMonthCount - resultYear * 12; System.out.println("YEAR : "+resultYear); c3.set(Calendar.YEAR, resultYear); System.out.println("MONTH : "+resultMonthCount); c3.set(Calendar.MONTH, resultMonthCount); System.out.println("c3 : "+c3.getTime()); //YY,q,startDate,endDate,quartername ------->> write to file String QuarterStartDate=year+"-"+months+"-"+day; String quarterName=YY+"\"Q\""+q; fw.write(YY+","+q+","+QuarterStartDate+","+QuarterEndDate+","+quarterName+"\n"); }//end of while fw.close(); String filepath = "/home/hadoop/Quarter.txt"; sql = "load data local inpath '" + filepath + "' overwrite into table TmpQuarterTable"; System.out.println("Running: " + sql); stmt.executeUpdate(sql); } catch(Exception e) { e.printStackTrace(); System.exit(1); } }}The error is like: Exception in thread "main" java.lang.IncompatibleClassChangeError: class com.facebook.fb303.FacebookService$Client has interface org.apache.thrift.TServiceClient as super classError as:Exception in thread "main" java.lang.IncompatibleClassChangeError: class com.facebook.fb303.FacebookService$Client has interface org.apache.thrift.TServiceClient as super class at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveConnection.java:93) at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) at SampleHiveProgram.quarterTable(SampleHiveProgram.java:64) at SampleHiveProgram.main(SampleHiveProgram.java:22)What is wrong with me? I have added all jars required for Hive. 解决方案 By any chance do you have more than different versions of thrift and libthrift hanging around? It sounds like the classes are being generated with one version of thrift, but trying to use a different version of libthrift at runtime. 这篇关于在使用Hive JDBC执行程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 11:22