在Sqoop往mysql导出数据的时候报了这个错误,一开始还以为是jar包没有打进去或者打错位置了,未解便上网查询。
Error reading from database: java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@27ce24aa is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries. java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@27ce24aa is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.
1.通过百度寻找此问题的解决方式,找到了如下答案:
由于mysql-connector-java的bug造成的,出错时我用的是mysql-connector-java-5.1.12-bin.jar,更新成mysql-connector-java-5.1.32-bin.jar就可以了。
但是我通过wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.32.tar.gz,下载了tar.gz包 解压之后得到mysql-connector-java-5.1.32-bin.jar
注意mysql-connector-java-5.1.32-bin.jar,看见没有是-bin.jar,maven项目的pom.xml中添加mysql依赖,会产出mysql-connector-java-5.1.32.jar 没有-bin,注意这是两个不同的包。
千万不要直接从项目中拷过来。。。最后替换掉sqoop目录原有jar包,我的目录是/opt/software/sqoop/lib/
但是!!!并没有解决这个问题。。。
2. 最终解答
查阅多方资料后,在StackOverFlow上找到了答案:
Try including the option --driver com.mysql.jdbc.Driver in the import command.
Worked for me...Thanks Man
sqoop export \ --connect $STAT_JDBC_CONNECT \ --username $JDBC_USERNAME \ --password $JDBC_PASSWORD \ --table $TABLE \ --update-mode allowinsert \ --driver com.mysql.jdbc.Driver \ --export-dir $SQOOP_HDFS_RESULT_DAY \ --columns stat_ds,command,answer,counts
3.SparkSql中使用round内置函数完成四舍五入保留两位小数
/** * 统计音箱型号分布情况 * * @param dataFrame * @return */ def echoModelDistributeStat(dataFrame: DataFrame, preDay: String): List[String] = { var numdf = dataFrame.groupBy("model").agg(count("model").as("model_count")) // 统计出总共的设备数 var sumdf = numdf.groupBy().agg(sum("model_count")) // 计算设备型号的总数量 var modelSum = sumdf.collect()(0)(0) // 计算出需要的的DF var modelInfo = numdf.select(numdf("model"), numdf("model_count"), (numdf("model_count") / modelSum * 100).as("model_percent")) modelInfo.registerTempTable("m") // 将日期转换为时间戳 val timeStamp = TimeUtils.getTimeStamp(preDay + " " + Constant.TIME_ZERO) val timeDf = modelInfo.sqlContext.sql("select " + timeStamp + " as stat_ds,m.model,m.model_count,round(m.model_percent,2) as model_percent from m") // 将DF转成List[Row]的形式 val list = timeDf.collect().toList // 将List[Row]转为List[String] val listStr = listRowConvertListStr(list) return listStr }