最后
由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档
还有更多面试复习笔记分享如下
*/
@Test
public void addIndex1() throws IOException {
IndexResponse response = client .prepareIndex( “msg” , “tweet” , “1” ).setSource(XContentFactory. jsonBuilder ()
.startObject().field( “name” , “linzhiqiang” )
.field( “date” , new Date())
.field( “msg” , “hello world” )
.endObject()).get();
System. out .println( “索引名称:” + response.getIndex() + " \n 类型:" + response.getType()
+ " \n 文档ID:" + response.getId() + " \n 当前实例状态:" + response.status());
}
/**
* 添加索引:传入json字符串
* @return void
* @Title: addIndex2
*/
@Test
public void addIndex2() {
String jsonStr = “{” +
" \" userName \" : \" 张三 \" ," +
" \" sendDate \" : \" 2017-11-30 \" ," +
" \" msg \" : \" 你好李四 \" " +
“}” ;
IndexResponse response = client .prepareIndex( “weixin” , “tweet” ).setSource(jsonStr, XContentType. JSON ).get();
System. out .println( “json索引名称:” + response.getIndex() + " \n json类型:" + response.getType()
+ " \n json文档ID:" + response.getId() + " \n 当前实例json状态:" + response.status());
}
/**
* 创建索引-传入Map对象
* @return void
* @Title: addIndex3
*/
@Test
public void addIndex3() {
Map<String, Object> map = new HashMap<String, Object>();
map.put( “name” , “小妹” );
map.put( “age” , 18 );
map.put( “sex” , “女” );
map.put( “address” , “广东省广州市天河区上社” );
map.put( “phone” , “15521202233” );
map.put( “height” , “175” );
map.put( “weight” , “60” );
IndexResponse response = client .prepareIndex( “species” , “person” ).setSource(map).get();
System. out .println( “map索引名称:” + response.getIndex() + " \n map类型:" + response.getType()
+ " \n map文档ID:" + response.getId() + " \n 当前实例map状态:" + response.status());
}
/**
* 传递json对象
* 需要添加依赖:gson
* @return void
* @Title: addIndex4
*/
@Test
public void addIndex4() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty( “userName” , “张三” );
jsonObject.addProperty( “sendDate” , “2017-11-23” );
jsonObject.addProperty( “msg” , “你好李四” );
IndexResponse response = client .prepareIndex( “qq” , “tweet” ).setSource(jsonObject, XContentType. JSON ).get();
System. out .println( “jsonObject索引名称:” + response.getIndex() + " \n jsonObject类型:" + response.getType()
+ " \n jsonObject文档ID:" + response.getId() + " \n 当前实例jsonObject状态:" + response.status());
}
我们可以创建Json、Map、JsonObject、自定义字段等,创建好之后可以去ES系统中查看是否添加成功。如果不进行分片设置每次创建默认有五个分片数量,具体情况如下图所示。
2.创建好之后,我们可以通过ES提供的API进行相应的查询操作,具体代码如下所示:
/**
* 从索引库获取数据
*
* @return void
* @Title: query
*/
@Test
public void query() {
GetResponse getResponse = client .prepareGet( “species” , “person” , “AWNtYjiVjqSYg4HhYcQZ” ).get();
System. out .println( “索引库的数据:” + getResponse.getSourceAsString());
}
查询的结果如下所示:
ES上面对应索引的数据如下所示:
3.下面我们将上面查询到的id(AWNtYjiVjqSYg4HhYcQZ)进行相应的修改,修改索引的API操作如下代码所示:
/**
* 更新索引库数据
* @Title: updateData
* @return void
*/
@Test
public void updateData() {
Map<String, Object> map = new HashMap<String, Object>();
map.put( “name” , “大妹” );
map.put( “age” , 20 );
map.put( “sex” , “女” );
map.put( “address” , “广东省广州市天河区上社” );
map.put( “phone” , “15521202233” );
map.put( “height” , “180” );
map.put( “weight” , “70” );
UpdateResponse updateResponse = client .prepareUpdate( “species” , “person” , “AWNtYjiVjqSYg4HhYcQZ” )
.setDoc(map).get();
System. out .println( “updateResponse索引名称:” + updateResponse.getIndex() + " \n updateResponse类型:" + updateResponse.getType()
+ " \n updateResponse文档ID:" + updateResponse.getId() + " \n 当前实例updateResponse状态:" + updateResponse.status());
}
我们在重新查询一下id为: AWNtYjiVjqSYg4HhYcQZ 的索引文档,看一下数据是否已经修改。结果如下所示:
我们从上面的截图可以看出数据确实已经修改完毕了,证明修改的API操作是成功的。
4.最后我们看一下最后的删除索引操作,具体的代码如下所示:
最后
分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。
这些面试题相对应的技术点:
- JVM
- MySQL
- Mybatis
- MongoDB
- Redis
- Spring
- Spring boot
- Spring cloud
- Kafka
- RabbitMQ
- Nginx
- …
大类就是:
- Java基础
- 数据结构与算法
- 并发编程
- 数据库
- 设计模式
- 微服务
- 消息中间件
1715370328588)]
[外链图片转存中…(img-Zy0ENKNI-1715370328588)]
[外链图片转存中…(img-WmcXm6Tp-1715370328589)]
[外链图片转存中…(img-oQnsQct0-1715370328589)]
[外链图片转存中…(img-cQXLplRe-1715370328589)]
[外链图片转存中…(img-GNbTwnAb-1715370328589)]
[外链图片转存中…(img-r7gRjuhO-1715370328590)]