摘要:概要通過函數(shù)進(jìn)行數(shù)據(jù)寫入,了解這個重要函數(shù)有助于理解工作流程優(yōu)化寫入性能。舉個例子,的屬性為的客戶端向重復(fù)創(chuàng)建,屬性的值為,每次值為當(dāng)前時間戳而不同,造成元數(shù)據(jù)重復(fù)更新。
概要
Atlas通過AtlasEntityStoreV2.createOrUpdate函數(shù)進(jìn)行數(shù)據(jù)寫入,了解AtlasEntityStoreV2.createOrUpdate這個重要函數(shù)有助于理解Atlas工作流程、優(yōu)化寫入性能。本文主要梳理createOrUpdate中各子模塊的功能和邏輯。
源碼解析
函數(shù)格式:EntityMutationResponse createOrUpdate(EntityStream entityStream, boolean isPartialUpdate, boolean replaceClassifications)
數(shù)據(jù)寫入過程中的上下文信息存于EntityGraphDiscoveryContext context對象中,數(shù)據(jù)格式如下:
public final class EntityGraphDiscoveryContext { private final AtlasTypeRegistry typeRegistry; private final EntityStream entityStream; private final ListreferencedGuids = new ArrayList<>(); private final Set referencedByUniqAttribs = new HashSet<>(); private final Map resolvedGuids = new HashMap<>(); private final Map resolvedIdsByUniqAttribs = new HashMap<>(); private final Set localGuids = new HashSet<>();
step1:通過preCreateOrUpdate函數(shù)對entityStream預(yù)處理:找出entityStream中所有entity的AtlasObjectId,判斷entity需要進(jìn)行創(chuàng)建還是更新操作,創(chuàng)建新vertex
a) AtlasEntityGraphDiscoveryV2.discover,遍歷所有entity的AtlasObjectId,放入到context中:
情況1)AtlasObject.guid!=null時將guid放入context.referencedGuids中; 情況2)若AtlasObject.id為null,則表示該entity已經(jīng)寫入圖數(shù)據(jù)庫,將AtlasObject放入context.referencedByUniqAttribs
b) AtlasEntityGraphDiscoveryV2.resolveReferences,判斷entity是否在圖數(shù)據(jù)庫中存在:
情況1)若context.referencedGuids中的guid在圖數(shù)據(jù)庫中存在對應(yīng)vertex,將guid和vertex放入context.resolvedGuids中; 情況2)若context.referencedGuids中的guid在圖數(shù)據(jù)庫中不存在對應(yīng)vertex,將guid放入context.localGuidReference中; 情況3)根據(jù)context.referencedByUniqAttribs中的AtlasObjectId找到對應(yīng)頂點(diǎn),將頂點(diǎn)放入resolvedIdsByUniqAttribs中,并將AtlasObjectId放入
c) 對不存在對應(yīng)vertex的entity創(chuàng)建vertex,并放入resolvedGuids
d) 將需要創(chuàng)建的entity和需要更新的entity分別放入EntityMutationContext.entitiesCreated和EntityMutationContext.entitiesUpdated中
step2:對比entityStream中的entity和圖數(shù)據(jù)庫中的vertex,判斷是否有屬性發(fā)生變化,忽略不需要更新的entity,核心代碼:
for(AtlasAttribute attribute:entityType.getAllAttributes().values()){ if(!entity.getAttributes().containsKey(attribute.getName())){ // if value is not provided, current value will not be updated continue; } Object newVal=entity.getAttribute(attribute.getName()); Object currVal=entityRetriever.getEntityAttribute(vertex,attribute); if(!attribute.getAttributeType().areEqualValues(currVal,newVal,context.getGuidAssignments())){ hasUpdates=true; if(LOG.isDebugEnabled()){ LOG.debug("found attribute update: entity(guid={}, typeName={}), attrName={}, currValue={}, newValue={}",guid,entity.getTypeName(),attribute.getName(),currVal,newVal); } break; } }
注意:當(dāng)attribute為referred AtlasObjectId時,客戶端定義的guid是UnAssigned的,必然和數(shù)據(jù)庫中存儲的guid值不同,這會造成元數(shù)據(jù)的重復(fù)更新,這個目前Atlas有待改進(jìn)的地方。舉個例子,Hive_Table的db屬性為Hive_Db的AtlasObjectId, 客戶端向Atlas Server重復(fù)創(chuàng)建Hive_Table,db屬性的值為AtlasObjectId{typeName=hive_db, guid=-1554620941},每次guid值為當(dāng)前時間戳而不同,造成table元數(shù)據(jù)重復(fù)更新。
step3:操作權(quán)限驗(yàn)證:通過AtlasAuthorizationUtils.verifyAccess函數(shù)驗(yàn)證發(fā)起請求的用戶是否有權(quán)限對各個entity進(jìn)行寫操作
step4:EntityGraphMapper.mapAttributesAndClassifications為vertex更新attributes,關(guān)于entity和vertex屬性的映射,可以參考文章元數(shù)據(jù)治理框架Atlas研究——JanusGraph圖數(shù)據(jù)庫對象關(guān)系映射
step5:通過entityChangeNotifier.onEntitiesMutated為vertex創(chuàng)建全文索引,并通知audit模塊記錄所有的變更操作
注:在整個數(shù)據(jù)寫入過程中,創(chuàng)建全文索引這一步驟會占用超過60%的時間,如果在實(shí)際使用中不需要用全文索引的功能,可以修改源碼注釋掉相應(yīng)doFullTextMapping函數(shù)
step6:整個數(shù)據(jù)寫入過程中,我們并未提到Atlas如何調(diào)用JanusGraph的api來向圖數(shù)據(jù)庫寫入數(shù)據(jù)。其實(shí),Atlas只需要通過JanusGraph api中的vertex、edge對象維護(hù)數(shù)據(jù)的圖結(jié)構(gòu)即可。Atlas對數(shù)據(jù)讀寫函數(shù)都添加了@GraphTransaction注解,這個注解確保在函數(shù)運(yùn)行結(jié)束后調(diào)用graph.commit()函數(shù)將當(dāng)前事務(wù)內(nèi)的變更提交圖數(shù)據(jù)庫。具體的實(shí)現(xiàn)可以見GraphTransactionInterceptor類。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/74085.html
摘要:把元數(shù)據(jù)信息分成和兩類,其中表示對象自身的屬性如的等,表示對象和其他元數(shù)據(jù)對象的關(guān)系如所屬的。這樣設(shè)計(jì)確保在查詢元數(shù)據(jù)時選擇是否忽略。具體實(shí)現(xiàn)以函數(shù)為例分析如何根據(jù)唯一屬性查找元數(shù)據(jù)對象。 概要Atlas采用了分布式圖數(shù)據(jù)庫JanusGraph作為數(shù)據(jù)存儲,目的在于用有向圖靈活的存儲、查詢數(shù)據(jù)血緣關(guān)系。Atlas定義了一套atlas-graphdb-api,允許采用不同的圖數(shù)據(jù)庫引擎來...
閱讀 1235·2021-11-10 11:35
閱讀 2981·2021-09-24 10:35
閱讀 3007·2021-09-22 15:38
閱讀 2856·2019-08-30 15:43
閱讀 1381·2019-08-29 18:39
閱讀 2637·2019-08-29 15:22
閱讀 2828·2019-08-28 18:17
閱讀 643·2019-08-26 13:37