您的位置:首页 > 编程语言 > Java开发

elasticsearch,spring boot,mybatis项目小结

2017-10-25 14:19 246 查看
最近接手一个活,用springboot整合mybatis获取一批数据,上传到elastic search建索引。小小研究了一下,操作不多,没有很难。
很好的教程

http://www.sojson.com/blog/81.html


https://yq.aliyun.com/articles/70054

遇到比较典型的错:
错1
https://stackoverflow.com/questions/8367312/serializing-with-jackson-json-getting-no-serializer-found
No serializer found for class elasticSearch.LogModel and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
大意是模板类没有set方法,故报错。用对象转json的时候报的。

错2
http://blog.csdn.net/z69183787/article/details/22269131
Jackson2需要引3个maven包
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0.pr3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0.pr3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0.pr3</version>

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9201}]]
这个是因为配置文件中没有配,网上有文章写创建conf,真是乱讲。(后来又报了一次这个错,是因为client连接关闭了还调用连接,spring的@autowire标签初始化类只调用一次构造方法,我创建连接的代码写构造方法里了,

当心client关闭后再次访问,会报找不着节点的错误。)

$ vi config/elasticsearch.yml

#cluster name

cluster.name:
sojson-application

#节点名称

node.name:
node-1

#绑定IP和端口

network.host:123.88.88.88

http.port:9200

 

 

还有在程序中创建连接时,端口写9300

client.addTransportAddress(new
InetSocketTransportAddress(InetAddress.getByName("192.168.0.106"),
9300));

9300端口: ES节点之间通讯使用

9200端口: ES节点 和 外部 通讯使用

我们这里是添加节点,故使用9300端口。

 

启动elastic search报错,用这个命令启动。

failed to delete temp file /Users/lasia/Software/elasticsearch-2.1.0/data/gafisperson/nodes/0/indices/person/1/translog/translog-4113158842387570706.tlog

java.nio.file.NoSuchFileException: /Users/lasia/Software/elasticsearch-2.1.0/data/gafisperson/nodes/0/indices/person/1/translog/translog-4113158842387570706.tlog

 

./elasticsearch -Des.insecure.allow.root=true -d

后台启动

 

做这个项目时还用到了springboot mybatis,操作数据库。

很简单,配置mapping.xml,再在类中添加方法就可以了。需要注意添加方法时sql语句的通配参数需要转换一下。

long
minSeq(@Param("name")String
name);

 

<select
id="minSeq"
resultType="long">

select seq from monad_load_config where name = #{name} and deletag = '1'

</select>

而且,map配置文件中有大于号小于号的时候,需要注意转义。

< >

<![CDATA[]]>

发现数据库查询的时候,要分根据seq分组查询,必须先进行排序,再切割(rownum<10),不然只会排切割的区间···还好发现的早,不然得丢好多数据···

SELECT *
from
(select
lower(p.personid)
as personid, p.fingerrepeatno
as
fingerrepeatno

from gafis_person p
left join
sys_depart d on
p.gather_org_code
= d.code
left join
code_ly ly on
p.data_sources
= ly.code
) m

WHERE m.seq >=
1 and
ROWNUM
<=10
; 对勾

 

select lower(p.personid)
as personid, p.fingerrepeatno
as fingerrepeatno

from gafis_person p
left join
sys_depart d on
p.gather_org_code = d.code left join
code_ly ly on
p.data_sources = ly.code

WHERE p.seq >=
1 and ROWNUM
<=10; ❌

 

不久后又增加功能开放端口,删除整个索引,用标签@RequestMapping("/delete")来实现,在类上和方法上分别加这个标签。参数增加:@RequestParam("indexName")
String indexName

再访问,http://127.0.0.1:8099/elastic/delete?indexName=sss

但要注意,标签内的字符串更改后不rebuild可能会认不出来。

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

由于访问接口后返回404页面,这一点需要修改。

参考资料http://jinnianshilongnian.iteye.com/blog/1997192

将标签@Service改为@RestController

传参标签由@requestparam改为public
String
DeleteElastic(@PathVariable("deleteName")
String deleteName) {配合

@RequestMapping("/delete/{deleteName}")使用

 

 @PathVariable和@RequestParam,分别是从路径里面去获取变量,也就是把路径当做变量,后者是从请求里面获取参数。 

 

/Springmvc/user/page.do?pageSize=3&pageNow=2 

 

pageSize和pageNow应该是属于参数而不是路径,所以应该添加@RequestParam的注解。 

 

如果做成如下URL,则可以使用@PathVariable 

 

/Springmvc/user/page/2/3.do 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

同时还需要注意@Controller和@RestController的区别

@RestController=@Controller+@Reponsebody

只写@Controller会映射到页面,加@Responsebody会直接当作返回结构体来处理,不会对应到页面。

http://blog.csdn.net/gg12365gg/article/details/51345601

,下面spring程序的代码片段可以直接return回数据,不然需要的是html格式的view。

[java] view
plain copy

 print?

@RestController  

public class HelloController {  

  

    @RequestMapping("/")  

    public String index() {  

        return "Greetings from Spring Boot!";  //由于是@RestController,因此直接返回数据,而不是view  

    }  

  

}  

 

@Service和@Controller区别

 

@Service用于标注业务层组件

@Controller用于标注控制层组件(如struts中的action)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: