AdventureWorks2012用作数据库,可以在这里下载。 您需要在代码工作之前安装数据库。
应用程序创建一个新的控制台应用程序,并从NuGet下载ElasticsearchCRUD和Entity Framework。
向项目添加一个新项,选择ADO.NET实体数据模型:
现在从数据库选项首先选择代码。 数据库已经存在。
从Person架构添加所有表。 Address表和Person表将用作文档根。
创建的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.Schema; 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 DateTime 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": { "mapPings": { "address": { "propertIEs": { "addressID": { "type": "long" },"addressline1": { "type": "string" },"addressline2": { "type": "string" },"city": { "type": "string" },"modifIEddate": { "type": "date","format": "dateOptionalTime" },"postalcode": { "type": "string" },"rowguID": { "type": "string" },"stateprovince": { "propertIEs": { "countryregion": { "propertIEs": { "countryregioncode": { "type": "string" },"name": { "type": "string" } } },"countryregioncode": { "type": "string" },"isonlystateprovinceflag": { "type": "boolean" },"name": { "type": "string" },"stateprovincecode": { "type": "string" },"stateprovinceID": { "type": "long" },"territoryID": { "type": "long" } } },"stateprovinceID": { "type": "long" } } } } },"persons": { "mapPings": { "person": { "propertIEs": { "additionalcontactinfo": { "type": "string" },"businessentityID": { "type": "long" },"demographics": { "type": "string" },"emailaddress": { "propertIEs": { "businessentityID": { "type": "long" },"emailaddress1": { "type": "string" },"emailaddressID": { "type": "long" },"rowguID": { "type": "string" } } },"emailpromotion": { "type": "long" },"firstname": { "type": "string" },"lastname": { "type": "string" },"mIDdlename": { "type": "string" },"namestyle": { "type": "boolean" },"personphone": { "propertIEs": { "businessentityID": { "type": "long" },"phonenumber": { "type": "string" },"phonenumbertype": { "propertIEs": { "modifIEddate": { "type": "date","phonenumbertypeID": { "type": "long" } } },"persontype": { "type": "string" },"suffix": { "type": "string" },"Title": { "type": "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 @R_301_5563@ here...}总结
以上是内存溢出为你收集整理的ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】全部内容,希望文章能够帮你解决ElasticsearchCRUD使用(四)【使用EF从SQLServer到Elasticsearch的数据传输】所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)