MsSQL   发布时间:2022-05-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

AdventureWorks2012用作数据库,可以在这里下载。 您需要在代码工作之前安装数据库。

应用程序

创建一个新的控制台应用程序,并从NuGet下载ElasticsearchCRUD和Entity Framework。

ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】

从AdventureWorks数据库创建代码第一个数据库

向项目添加一个新项,选择ADO.NET实体数据模型:

ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】


现在从数据库选项首先选择代码。 数据库已经存在。

ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】


从Person架构添加所有表。 Address表和Person表将用作文档根。

ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】

创建的Address类需要更改。 必须删除DbGeography SpatialLOCATIOn,因为这不是支持的类型。 在ElasticsearchCRUD V1.0.8或更高版本中,可以使用JsonIgnore属性忽略这一点。

namespace DataTransfersqlToEl.sqlDomainModel
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.scheR_45_11845@a;
    using System.Data.Entity.Spatial;

    [Table("Person.Address")]
    public partial class Address
    {
        public int AddressID { get; set; }

        [required]
        [StringLength(60)]
        public String AddressLine1 { get; set; }

        [StringLength(60)]
        public String AddressLine2 { get; set; }

        [required]
        [StringLength(30)]
        public String City { get; set; }

        public int StateprovinceID { get; set; }

        [required]
        [StringLength(15)]
        public String PostalCode { get; set; }

        // This type is not supported yet...
        //public DbGeography SpatialLOCATIOn { get; set; }

        public Guid rowguid { get; set; }

        public @R_675_7538@me ModifiedDate { get; set; }

        public virtual Stateprovince Stateprovince { get; set; }
    }
}

选择实体并将文档添加到Elasticsearch。 Address 传输实现如下:

public void SavetoElasticsearchAddress()
{
    IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();
    using (var elasticsearchContext = new ElasticsearchContext("http://localhost:9200/",elasticsearchMappingResolver))
    {
        //elasticsearchContext.TraceProvider = new ConsoleTraceProvider();
        using (var modelPerson = new ModelPerson())
        {
            int pointer = 0;
            const int interval = 100;
            int length = modelPerson.CountryRegion.Count();

            while (pointer < length)
            {
                stopwatch.Start();
                var collection = modelPerson.Address.orderBy(t => t.AddressID).Skip(pointer).Take(interval).ToList<Address>();
                stopwatch.Stop();
                Console.WriteLine("Time taken for SELEct {0} AddressID: {1}",interval,stopwatch.Elapsed);
                stopwatch.Reset();

                foreach (var item in collection)
                {
                    elasticsearchContext.AddupdateDocument(item,item.AddressID);
                    String t = "yes";
                }

                stopwatch.Start();
                elasticsearchContext.SaveChanges();
                stopwatch.Stop();
                Console.WriteLine("Time taken to insert {0} AddressID documents: {1}",stopwatch.Elapsed);
                stopwatch.Reset();
                pointer = pointer + interval;
                Console.WriteLine("Transferred: {0} items",pointer);
            }
        }
    }
}

一次选择了一百个实体,并将其添加到ElasticsearchCRUD上下文中。 这只是将对象添加到内存集合中。 调用SaveChanges方法时,将每个实体序列化为JSON对象。 当所有项目都被序列化时,httpClient实例将http批量POST请求中的所有对象发送到Elasticsearch。 直到所有项都被传输为止。 ElasticsearchCRUD将所有子元素序列化为1-N。 对已经转换的父对象的任何引用都将被忽略并保存为空属性。

创建的Elasticsearch 映射如下(对于Person和Address文档):

{
    "addresss": @H_616_254@{ "@H_901_252@mappings": @H_616_254@{ "address": @H_616_254@{ "properties": @H_616_254@{ "addressid": @H_616_254@{ "type": @H_616_254@"long" },"addressline1": @H_616_254@{ "type": @H_616_254@"String" },"addressline2": @H_616_254@{ "type": @H_616_254@"String" },"city": @H_616_254@{ "type": @H_616_254@"String" },"@H_901_252@modifieddate": @H_616_254@{ "type": @H_616_254@"date","format": @H_616_254@"dateOptionalTime" },"postalcode": @H_616_254@{ "type": @H_616_254@"String" },"rowguid": @H_616_254@{ "type": @H_616_254@"String" },"stateprovince": @H_616_254@{ "properties": @H_616_254@{ "countryregion": @H_616_254@{ "properties": @H_616_254@{ "countryregioncode": @H_616_254@{ "type": @H_616_254@"String" },"name": @H_616_254@{ "type": @H_616_254@"String" } } },"countryregioncode": @H_616_254@{ "type": @H_616_254@"String" },"isonlystateprovinceflag": @H_616_254@{ "type": @H_616_254@"Boolean" },"name": @H_616_254@{ "type": @H_616_254@"String" },"stateprovincecode": @H_616_254@{ "type": @H_616_254@"String" },"stateprovinceid": @H_616_254@{ "type": @H_616_254@"long" },"territoryid": @H_616_254@{ "type": @H_616_254@"long" } } },"stateprovinceid": @H_616_254@{ "type": @H_616_254@"long" } } } } },"persons": @H_616_254@{ "@H_901_252@mappings": @H_616_254@{ "person": @H_616_254@{ "properties": @H_616_254@{ "additionalcontacTinfo": @H_616_254@{ "type": @H_616_254@"String" },"businessentityid": @H_616_254@{ "type": @H_616_254@"long" },"demographics": @H_616_254@{ "type": @H_616_254@"String" },"emailaddress": @H_616_254@{ "properties": @H_616_254@{ "businessentityid": @H_616_254@{ "type": @H_616_254@"long" },"emailaddress1": @H_616_254@{ "type": @H_616_254@"String" },"emailaddressid": @H_616_254@{ "type": @H_616_254@"long" },"rowguid": @H_616_254@{ "type": @H_616_254@"String" } } },"emailpromotion": @H_616_254@{ "type": @H_616_254@"long" },"firstname": @H_616_254@{ "type": @H_616_254@"String" },"lastname": @H_616_254@{ "type": @H_616_254@"String" },"@H_901_252@middlename": @H_616_254@{ "type": @H_616_254@"String" },"namestyle": @H_616_254@{ "type": @H_616_254@"Boolean" },"personphone": @H_616_254@{ "properties": @H_616_254@{ "businessentityid": @H_616_254@{ "type": @H_616_254@"long" },"@H_901_252@phonenumber": @H_616_254@{ "type": @H_616_254@"String" },"@H_901_252@phonenumbertype": @H_616_254@{ "properties": @H_616_254@{ "@H_901_252@modifieddate": @H_616_254@{ "type": @H_616_254@"date","@H_901_252@phonenumbertypEID": @H_616_254@{ "type": @H_616_254@"long" } } },"persontype": @H_616_254@{ "type": @H_616_254@"String" },"suffix": @H_616_254@{ "type": @H_616_254@"String" },"title": @H_616_254@{ "type": @H_616_254@"String" } } } } } }

可以使用ElasticsearchCRUD如下读取保存的对象。

ublic Address GetAddressFromElasticsearch(int id)
{
    Address address;
    IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();
    using (var elasticsearchContext = new ElasticsearchContext("http://localhost:9200/",elasticsearchMappingResolver))
    {
        address = elasticsearchContext.GetDocument<Address>(id);
    }

    return address;
}

然后可以在控制台应用程序中使用。 完成后,具有嵌套子对象的所有对象将作为文档保存在Elasticsearch中。

using System;

namespace DataTransfersqlToEl {
    class Program {
        static void Main(String[] args)
        {
            var repo = new Repo();
            repo.SavetoElasticsearchPerson();
            repo.SavetoElasticsearchAddress();

            var personX = repo.GetPersonFromElasticsearch(345);
            var addressX = repo.GetAddressFromElasticsearch(22);
            Console.WriteLine(addressX);
            Console.WriteLine(personX);
        }
    }
}

如果您只希望保存父实体,并且不包含子实体(Elasticsearch中的nesTED对象),则可以在ElasticsearchCRUD上下文构造函数中设置此选项:

bool saveChildEntitiesAsnestedobjects = false;

using (var elasticSearchContext = 
              new ElasticsearchContext(
                 "http://localhost:9200/",elasticsearchMappingResolver,saveChildEntitiesAsnestedobjects 
              )
      )
{
  // Do you coding here...
}

大佬总结

以上是大佬教程为你收集整理的ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】全部内容,希望文章能够帮你解决ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: