前提条件
工作时间久了,发现自己的注意力都会汇集到项目的业务上,特别是创业型的公司,除了吃饭、睡觉、回家路上,其他时间基本上都是工作了,自己学习的时间都被暂用了,这可能是创业型、外包型的常态吧!
年轻的时候太浮躁、太自信,只追求代码上的成就感,忽略了基础和理论原理的学习,后果就是:不光面试会被虐得体无完肤,也会错过升阶的时机---
当我真正去了解(被面试逼的)那些基础和理论时,发现“面试造火箭,工作拧螺丝”这个理论是错的,知道原理后从大牛哪里学来的编程习惯得到了解释,如果是真正热爱编程的话,值得去深究基础,它会潜移默化的规范你的编程习惯---
安装步骤
1.安装nodejs
wget https://nodejs.org/dist/v12.18.4/node-v12.18.4-linux-x64.tar.xz
tar -zxvf node-v12.18.4-linux-x64.tar.xz
配置环境变量:
vim /etc/profile ,添加如下内容,
export NODE_HOME=/root/node-v12.16.2-linux-x64
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules
## 让配置生效
source /etc/profile
# 检查是否成功
node -v
2.elasticsearch-head安装
git clone git://github.com/mobz/elasticsearch-head.git //git下载相关资源
cd elasticsearch-head //进入相关的目录
npm install
npm run start
# 设置es允许跨域访问
#设置外网可以访问(这一条设置为真实的ip也行)
network.host: 0.0.0.0
# 监听端口(默认)
http.port: 9200
# 增加参数,使head插件可以访问es
http.cors.enabled: true
http.cors.allow-origin: "*"
# 重启ES
# ----注意:冒号后面有个空格
4.head的启动工具:grunt安装
npm install -g grunt-cli
##检测是否安装成功,如果执行命令后出现版本号就表明成功
grunt -version
# 在head的配置文件Gruntfile.js中添加host正则匹配项:
connect: {
server: {
options: {
port: 9100,
base: '.',
keepalive: true,
host: '*' 添加这一项
}
}
}
5.head目录下执行启动
##安装npm 服务
npm install
##启动插件
grunt server
或者npm run start
使用简介
1.位置1是es的访问地址
2.xxx_index是索引名称,node-x是节点,像位置2中的0/1/2就是标明该索引的数据在该node-x中的分片位置
第五种Java api:queryDsl的使用
1.pom
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
2.pom - build - plugin,生成QEntity的位置(可以将QEntity copy到entity中,也可以不copy,打包的时候会自动放到entity文件中)
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
3.有基于hibernate-jpa的api,但我这里没有用,以后不上;
4.另外想写一个es查询语句到java查询对象的转化,但是工作太忙,没有写完,关注项目,会抽时间补上的。
5.config
/**
* 让Spring管理JPAQueryFactory
*/
@Bean
public JPAQueryFactory jpaQueryFactory(EntityManager entityManager){
return new JPAQueryFactory(entityManager);
}
6.entity(这里生成QEntity的时候需要注意,上面加入hibernate的包的原因,不加生成不了QEntity对象)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "item", shards = 1, replicas = 0)
@Entity // 必须要加
public class Student {
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
private long id;
private String name;
private int age;
private long schoolId;
}
7.Java调用
public interface MyRepository extends QuerydslPredicateExecutor<Student>, QuerydslBinderCustomizer<QStudent> {
}
@Resource
private MyRepository myRepository;
public void search(@QuerydslPredicate(root = Student.class) Predicate predicate, final Pageable pageable) {
QStudent qStudent = QStudent.student;
if (predicate == null) predicate = new BooleanBuilder();
// 举一例子,多级拼接
ExpressionUtils.and(predicate, qStudent.name.like(""));
// 执行结果
Page<Student> all = myRepository.findAll(predicate, pageable);
Pageable page = all.getPageable();
List<Student> content = all.getContent();
}
@Resource
private JPAQueryFactory queryFactory;
public void selectStudent(){
QStudent qStudent = QStudent.student;
List<Student> students = queryFactory.selectFrom(qStudent).fetch();
System.out.println(students);
}
public void selectStudentWithWhere() {
QStudent qStudent = QStudent.student;
//单表单条件查询
List<Student> students = queryFactory.selectFrom(qStudent).where(qStudent.age.goe(2)).fetch();
//单表多条件and查询
List<Student> studentList = queryFactory.selectFrom(qStudent).where(qStudent.age.goe(2).and(qStudent.name.eq("vincent"))).fetch();
//单表多条件or查询
List<Student> list = queryFactory.selectFrom(qStudent).where(qStudent.age.goe(20).or(qStudent.name.eq("vincent"))).fetch();
//单表排序分页查询
List<Student> list1 = queryFactory.selectFrom(qStudent).offset(1).limit(3).orderBy(qStudent.id.desc()).fetch();
System.out.println(students);
System.out.println(studentList);
System.out.println(list);
System.out.println(list1);
}
项目地址
https://gitee.com/hzy100java/springboot-elasticsearch.git
网友评论