主页 > 新闻资讯 > 大数据学习:ElasticSearch索引建立

大数据学习:ElasticSearch索引建立

作者:张老师 浏览次数: 2021-02-25 17:52
对于ElasticSearch而言,其提供的全文搜索功能,很大程度上来说,是来自于其索引建立的思想,尤其是针对于非结构化的大规模数据,全文搜索能够极大地提升效率。今天的大数据学习分享,我们就主要来讲讲ElasticSearch索引建立的基础知识。

ES的索引类似于Mysql的数据库,每个数据库可以建立多张表;

每个索引有自己的 Mapping 用于定义文档的字段名和字段类型;

每个索引有自己的 Settings 用于定义不同的数据分布,也就是索引使用分片的情况。

大数据学习:ElasticSearch索引建立

ElasticSearch建立索引的步骤

读取配置json内容第一步

@Override
public String getLocalSetting() {
    String filePath = String.format("essetting/%s-Setting.json", getIndexPrefix());
    String json = getFileContext(filePath);
    Assert.hasText(json, String.format("本地%s为空", filePath));
    return json;
}

@Override
public String getLocalMapping() {
    String filePath = String.format("essetting/%s-Mapping.json", getIndexPrefix());
    String json = getFileContext(filePath);
    Assert.hasText(json, String.format("本地%s为空", filePath));
    return json;
}

读取配置json内容第二步

protected String getFileContext(String filePath) {
        try {
            ClassPathResource resource = new ClassPathResource(filePath);
            if (!resource.exists()) {
                resource = new ClassPathResource(String.format("config/%s", filePath));
                log.info("配置文件路径:{}", resource.getPath());
                if (!resource.exists()) {
                    resource = new ClassPathResource(String.format("../config/%s", filePath));
                    log.info("配置文件路径:{}", resource.getPath());
                }
            }
            log.info("配置文件路径:{}", resource.getPath());
            InputStream inputStream = resource.getInputStream();
            if (inputStream.available() > 0) {
                byte[] b = new byte[inputStream.available()];
                inputStream.read(b, 0, inputStream.available());
                inputStream.close();
                return new String(b);
            }
            return null;
        } catch (IOException e) {
            log.error("读取配置文件异常{}:", filePath, e);
        }
        return null;
}

创建索引

@Override
    public boolean createIndex(String index) {
        Assert.hasText(index, String.format("%s不能为空", index));
        CreateIndexRequest request = new CreateIndexRequest(index);//创建索引
        //创建的每个索引都可以有与之关联的特定设置。
        String setting = getLocalSetting();
        request.settings(setting, XContentType.JSON);
        String mapping = getLocalMapping();
        //创建索引时创建文档类型映射
        request.mapping(mapping, XContentType.JSON);

        //为索引设置一个别名
        request.alias(
                new Alias(String.format("%s-alias", index))
        );
        //可选参数
        request.setTimeout(TimeValue.timeValueMinutes(2));//超时,等待所有节点被确认(使用TimeValue方式)
        request.setMasterTimeout(TimeValue.timeValueMinutes(1));//连接master节点的超时时间(使用TimeValue方式)
        //request.waitForActiveShards(ActiveShardCount.DEFAULT);
        request.waitForActiveShards(ActiveShardCount.DEFAULT);//在创建索引API返回响应之前等待的活动分片副本的数量,以ActiveShardCount形式表示。
        try {
            //同步执行
            CreateIndexResponse createIndexResponse = esRestUtils.getClient().indices().create(request, RequestOptions.DEFAULT);
            return true;
        } catch (IOException e) {
            log.error("创建index异常{}:", index, e);
        }
        return false;
}

其中Setting和Mapping的json内容格式如下:

perimeteralarm-Setting.json

{
  "index": {
    "number_of_shards": "3",
    "blocks": {
      "read_only_allow_delete": "false"
    },
    "number_of_replicas": "1",
    "max_result_window": "2147483647"
  }
}
perimeteralarm-Mapping.json

{
  "properties": {
    "placeCode": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "alarmType": {
      "type": "text",
      "index": false
    },
    "alarmTime": {
      "type": "long"
    },
    "mrowTime": {
      "type": "long",
      "index": false
    }
  }
}

索引创建成功后登录Kibana管理界面查看索引模板,利用Kibana进行Restful增删改查。

关于大数据学习,ElasticSearch索引建立,以上就为大家做了简单的介绍了。ElasticSearch组件,之前也有系列的文章介绍,在初步接触这个组件时,可以作为参考。成都加米谷大数据,专业大数据培训机构,大数据开发、数据分析与挖掘,零基础班本月正在招生中,课程大纲及学习视频,可联系获取!
热点排行
推荐文章
立即申请>>