NLPchina / elasticsearch-sql

Use SQL to query Elasticsearch
Apache License 2.0
7.01k stars 1.54k forks source link

集成elasticsearch-sql和mybatis,但是执行Mpper中的SQL时,一直报错... #768

Closed whzhangxw closed 6 years ago

whzhangxw commented 6 years ago

[DEBUG] [2018-09-20 09:44:58 338] [http-bio-80-exec-3] (JakartaCommonsLoggingImpl.java:27) {conn-100017} Preparing Statement: select from house_data_plot_index
[DEBUG] [2018-09-20 09:44:58 341] [http-bio-80-exec-3] (JakartaCommonsLoggingImpl.java:27) {pstm-100018} Executing Statement: select
from house_data_plot_index
[DEBUG] [2018-09-20 09:44:58 342] [http-bio-80-exec-3] (JakartaCommonsLoggingImpl.java:27) {pstm-100018} Parameters: [] [DEBUG] [2018-09-20 09:44:58 342] [http-bio-80-exec-3] (JakartaCommonsLoggingImpl.java:27) {pstm-100018} Types: [] org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in sqlmap/dataRt/Base.xml.
--- The error occurred while applying a parameter map.
--- Check the base.queryForListBySql-InlineParameterMap.
--- Check the results (failed to retrieve results).
--- Cause: java.lang.NullPointerException; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in sqlmap/dataRt/Base.xml.
--- The error occurred while applying a parameter map.
--- Check the base.queryForListBySql-InlineParameterMap.
--- Check the results (failed to retrieve results).
--- Cause: java.lang.NullPointerException at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80) at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:203) at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:293) at com.jd.data.rt.dao.common.component.ComponentBaseDao.queryForListBySql(ComponentBaseDao.java:50) at com.jd.data.rt.service.impl.EsNlpcnServiceImpl.query(EsNlpcnServiceImpl.java:33) at com.jd.data.rt.controller.TestController.esnlpcn(TestController.java:388) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in sqlmap/dataRt/Base.xml.
--- The error occurred while applying a parameter map.
--- Check the base.queryForListBySql-InlineParameterMap.
--- Check the results (failed to retrieve results).
--- Cause: java.lang.NullPointerException at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:204) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:567) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:541) at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118) at org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:295) at org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:1) at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:200) ... 44 more Caused by: java.lang.NullPointerException at com.ibatis.sqlmap.engine.execution.SqlExecutor.moveToNextResultsSafely(SqlExecutor.java:353) at com.ibatis.sqlmap.engine.execution.SqlExecutor.moveToNextResultsIfPresent(SqlExecutor.java:348) at com.ibatis.sqlmap.engine.execution.SqlExecutor.getFirstResultSet(SqlExecutor.java:340) at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:298) at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:189) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteQuery(MappedStatement.java:221) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:189) ... 51 more

whzhangxw commented 6 years ago

这是我的ES数据源配置

whzhangxw commented 6 years ago

`

    <property name="initialSize" value="${ds.initialSize}" />
</bean>`
shi-yuan commented 6 years ago

单独用spring的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="esDataSource" class="com.alibaba.druid.pool.ElasticSearchDruidDataSource" init-method="init"
          destroy-method="close">
        <property name="url" value="jdbc:elasticsearch://127.0.0.1:9300/order_info"/>
    </bean>
</beans>

然后调用,是没问题的:

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:application.xml");
        DruidDataSource dds = context.getBean(ElasticSearchDruidDataSource.class);
        Connection connection = dds.getConnection();
        PreparedStatement ps = connection.prepareStatement("SELECT * from order_info");
        ResultSet resultSet = ps.executeQuery();
        List<String> result = new ArrayList<String>();
        while (resultSet.next()) {
            result.add(resultSet.getString("name"));
        }
        System.out.println(result);

        ps.close();
        connection.close();
        dds.close();
shi-yuan commented 6 years ago

是不是哪儿的配置不对呢,能贴一下完整的配置吗,涉及mybatis的?

whzhangxw commented 6 years ago

image image image image image

shi-yuan commented 6 years ago

集成测试:配置文件 + java代码

application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="esDataSource" class="com.alibaba.druid.pool.ElasticSearchDruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="jdbc:elasticsearch://127.0.0.1:9300/order_info"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="esDataSource"/>
        <property name="configLocation" value="SqlMapConfig.xml"/>
    </bean>

    <bean id="testDao" class="TestDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>

SqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <plugins>
        <plugin interceptor="MyInterceptor"/>
    </plugins>

    <mappers>
        <mapper resource="Test.xml"/>
    </mappers>

</configuration>

Test.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="test">

    <select id="test">
        SELECT COUNT(*) from order_info
    </select>

</mapper>

MyInterceptor.java:

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.*;

@Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class})})
public class MyInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        PreparedStatement ps = (PreparedStatement) invocation.getArgs()[0];
        try (ResultSet resultSet = ps.executeQuery()) {
            String name;
            ResultSetMetaData md = resultSet.getMetaData();
            int count = md.getColumnCount(), i;
            List<Map<String, Object>> result = new ArrayList<>();
            Map<String, Object> map;
            while (resultSet.next()) {
                map = new HashMap<>();
                for (i = 1; i <= count; ++i) {
                    map.put(name = md.getColumnName(i), resultSet.getObject(name));
                }

                result.add(map);
            }
            return result;
        }
    }

    @Override
    public Object plugin(Object target) {
        return (target instanceof StatementHandler) ? Plugin.wrap(target, this) : target;
    }

    @Override
    public void setProperties(Properties properties) {
    }
}

TestDao.java

public interface TestDao {

    Object test() throws Exception;

}

TestDaoImpl.java

import org.mybatis.spring.support.SqlSessionDaoSupport;

public class TestDaoImpl extends SqlSessionDaoSupport implements TestDao {

    @Override
    public Object test() throws Exception {
        return this.getSqlSession().selectOne("test.test");
    }

}

Main.java

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) throws Exception {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:application.xml");
        TestDao testDao = context.getBean("testDao", TestDao.class);

        System.out.println(testDao.test());
    }
}

输出:

{COUNT(*)=1.0}
shi-yuan commented 5 years ago

最新版本,可以不用写拦截器了哈

liujianrong commented 5 years ago

最新版本,可以不用写拦截器了哈

springboot 整合 elasticsearch-sql 6.2.3.0 版本和mybatis 还是会无限打印<== 控制台还是会无限打印<== Updates: 0 的问题