You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by gr...@apache.org on 2015/04/14 01:16:07 UTC
[1/2] incubator-usergrid git commit: Cleanup of ExportAdmins
Repository: incubator-usergrid
Updated Branches:
refs/heads/exportImportTool 8eed12f35 -> 5d3866904
Cleanup of ExportAdmins
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/47c850da
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/47c850da
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/47c850da
Branch: refs/heads/exportImportTool
Commit: 47c850daa957f1a30be3ac1993e8659f7c880449
Parents: 8eed12f
Author: GERey <gr...@apigee.com>
Authored: Mon Apr 13 16:14:33 2015 -0700
Committer: GERey <gr...@apigee.com>
Committed: Mon Apr 13 16:14:33 2015 -0700
----------------------------------------------------------------------
.../org/apache/usergrid/tools/ExportAdmins.java | 314 ++++---------------
1 file changed, 53 insertions(+), 261 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/47c850da/stack/tools/src/main/java/org/apache/usergrid/tools/ExportAdmins.java
----------------------------------------------------------------------
diff --git a/stack/tools/src/main/java/org/apache/usergrid/tools/ExportAdmins.java b/stack/tools/src/main/java/org/apache/usergrid/tools/ExportAdmins.java
index 67d6103..490df4a 100644
--- a/stack/tools/src/main/java/org/apache/usergrid/tools/ExportAdmins.java
+++ b/stack/tools/src/main/java/org/apache/usergrid/tools/ExportAdmins.java
@@ -1,5 +1,3 @@
-
-
/**
* Created by ApigeeCorporation on 4/9/15.
*/
@@ -22,24 +20,18 @@
package org.apache.usergrid.tools;
-import java.io.File;
-import java.io.UnsupportedEncodingException;
-import java.util.HashMap;
+
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
+import org.codehaus.jackson.JsonGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.apache.usergrid.tools.bean.ExportOrg;
-import org.codehaus.jackson.JsonEncoding;
-import org.codehaus.jackson.JsonFactory;
-import org.codehaus.jackson.JsonGenerator;
+import org.apache.commons.cli.CommandLine;
-import org.apache.usergrid.management.OrganizationInfo;
-import org.apache.usergrid.management.UserInfo;
import org.apache.usergrid.persistence.ConnectionRef;
import org.apache.usergrid.persistence.Entity;
import org.apache.usergrid.persistence.EntityManager;
@@ -47,20 +39,13 @@ import org.apache.usergrid.persistence.Query;
import org.apache.usergrid.persistence.Results;
import org.apache.usergrid.persistence.Results.Level;
import org.apache.usergrid.persistence.cassandra.CassandraService;
-import org.apache.usergrid.tools.bean.ExportOrg;
import org.apache.usergrid.utils.JsonUtils;
-import org.apache.commons.cli.CommandLine;
-
-import com.google.common.collect.BiMap;
-
public class ExportAdmins extends ExportingToolBase {
static final Logger logger = LoggerFactory.getLogger( Export.class );
- JsonFactory jsonFactory = new JsonFactory();
-
@Override
public void runTool( CommandLine line ) throws Exception {
@@ -68,156 +53,72 @@ public class ExportAdmins extends ExportingToolBase {
setVerbose( line );
- // ExportDataCreator dataCreator = new ExportDataCreator(emf,
- // managementService);
- // dataCreator.createTestData();
-
applyOrgId( line );
prepareBaseOutputFileName( line );
outputDir = createOutputParentDir();
logger.info( "Export directory: " + outputDir.getAbsolutePath() );
- // Export organizations separately.
-// exportOrganizations();
-//
-// // Loop through the organizations
-// Map<UUID, String> organizations = getOrgs();
-// for ( Map.Entry<UUID, String> organization : organizations.entrySet() ) {
-//
-// if ( organization.equals( properties.getProperty( "usergrid.test-account.organization" ) ) ) {
-// // Skip test data from being exported.
-// continue;
-// }
-
- exportApplicationsForOrg( null );
- // }
+ exportApplicationsForOrg( null );
}
- private Map<UUID, String> getOrgs() throws Exception {
- // Loop through the organizations
- Map<UUID, String> organizationNames = null;
+ private void exportApplicationsForOrg( Map.Entry<UUID, String> organization ) throws Exception {
- if ( orgId == null ) {
- organizationNames = managementService.getOrganizations();
- }
+ EntityManager em = emf.getEntityManager( CassandraService.MANAGEMENT_APPLICATION_ID );
- else {
- OrganizationInfo info = managementService.getOrganizationByUuid( orgId );
+ // Get the JSon serializer.
+ JsonGenerator jg = getJsonGenerator( createOutputFile( "application", em.getApplication().getName() ) );
- if ( info == null ) {
- logger.error( "Organization info is null!" );
- System.exit( 1 );
- }
+ jg.writeStartArray();
- organizationNames = new HashMap<UUID, String>();
- organizationNames.put( orgId, info.getName() );
- }
+ // Create a GENERATOR for the application collections.
+ JsonGenerator collectionsJg =
+ getJsonGenerator( createOutputFile( "collections", em.getApplication().getName() ) );
+ collectionsJg.writeStartObject();
+ Map<String, Object> metadata = em.getApplicationCollectionMetadata();
+ echo( JsonUtils.mapToFormattedJsonString( metadata ) );
- return organizationNames;
- }
+ // Loop through the collections. This is the only way to loop
+ // through the entities in the application (former namespace).
+ // for ( String collectionName : metadata.keySet() ) {
+ Query query = new Query();
+ query.setLimit( MAX_ENTITY_FETCH );
+ query.setResultsLevel( Results.Level.ALL_PROPERTIES );
- private void exportApplicationsForOrg( Map.Entry<UUID, String> organization ) throws Exception {
- logger.info( "" + organization );
+ Results entities = em.searchCollection( em.getApplicationRef(), "users", query );
- // Loop through the applications per organization
- //BiMap<UUID, String> applications = managementService.getApplicationsForOrganization( organization.getKey() );
- //for ( Map.Entry<UUID, String> application : applications.entrySet() ) {
+ while ( entities.size() > 0 ) {
- // logger.info( application.getValue() + " : " + application.getKey() );
+ for ( Entity entity : entities ) {
+ // Export the entity first and later the collections for
+ // this entity.
+ jg.writeObject( entity );
+ echo( entity );
- EntityManager em = emf.getEntityManager( CassandraService.MANAGEMENT_APPLICATION_ID );
+ saveCollectionMembers( collectionsJg, em, null, entity );
+ }
- // Get the JSon serializer.
- JsonGenerator jg = getJsonGenerator( createOutputFile( "application", em.getApplication().getName()) );
-//
-// // load the dictionary
-// EntityManager rootEm = emf.getEntityManager( CassandraService.MANAGEMENT_APPLICATION_ID );
-//
-// Entity appEntity = rootEm.get( application.getKey() );
-//
-// Map<String, Object> dictionaries = new HashMap<String, Object>();
-//
-// for ( String dictionary : rootEm.getDictionaries( appEntity ) ) {
-// Map<Object, Object> dict = rootEm.getDictionaryAsMap( appEntity, dictionary );
-//
-// // nothing to do
-// if ( dict.isEmpty() ) {
-// continue;
-// }
-//
-// dictionaries.put( dictionary, dict );
-// }
-////
-//
-// // Get application
-// Entity nsEntity = em.get( application.getKey() );
-//
-// Set<String> collections = em.getApplicationCollections();
-//
-// // load app counters
-//
-// Map<String, Long> entityCounters = em.getApplicationCounters();
-//
-// nsEntity.setMetadata( "organization", organization );
-// nsEntity.setMetadata( "dictionaries", dictionaries );
-// // counters for collections
-// nsEntity.setMetadata( "counters", entityCounters );
-// nsEntity.setMetadata( "collections", collections );
-
- jg.writeStartArray();
-// jg.writeObject( nsEntity );
-
- // Create a GENERATOR for the application collections.
- JsonGenerator collectionsJg = getJsonGenerator( createOutputFile( "collections", em.getApplication().getName() ) );
- collectionsJg.writeStartObject();
-
- Map<String, Object> metadata = em.getApplicationCollectionMetadata();
- echo( JsonUtils.mapToFormattedJsonString( metadata ) );
-
- // Loop through the collections. This is the only way to loop
- // through the entities in the application (former namespace).
- // for ( String collectionName : metadata.keySet() ) {
-
- Query query = new Query();
- query.setLimit( MAX_ENTITY_FETCH );
- query.setResultsLevel( Results.Level.ALL_PROPERTIES );
-
- Results entities = em.searchCollection( em.getApplicationRef(), "users", query );
-
- while ( entities.size() > 0 ) {
-
- for ( Entity entity : entities ) {
- // Export the entity first and later the collections for
- // this entity.
- jg.writeObject( entity );
- echo( entity );
-
- saveCollectionMembers( collectionsJg, em, null, entity );
- }
-
- //we're done
- if ( entities.getCursor() == null ) {
- break;
- }
-
-
- query.setCursor( entities.getCursor() );
-
- entities = em.searchCollection( em.getApplicationRef(), "users", query );
- }
- //}
+ //we're done
+ if ( entities.getCursor() == null ) {
+ break;
+ }
- // Close writer for the collections for this application.
- collectionsJg.writeEndObject();
- collectionsJg.close();
+ query.setCursor( entities.getCursor() );
- // Close writer and file for this application.
- jg.writeEndArray();
- jg.close();
- // }
+ entities = em.searchCollection( em.getApplicationRef(), "users", query );
+ }
+ //}
+
+ // Close writer for the collections for this application.
+ collectionsJg.writeEndObject();
+ collectionsJg.close();
+
+ // Close writer and file for this application.
+ jg.writeEndArray();
+ jg.close();
+ // }
}
@@ -271,8 +172,9 @@ public class ExportAdmins extends ExportingToolBase {
jg.writeEndObject();
}
-
- /** Persists the connection for this entity. */
+ /**
+ * Persists the connection for this entity.
+ */
private void saveDictionaries( Entity entity, EntityManager em, JsonGenerator jg ) throws Exception {
jg.writeFieldName( "dictionaries" );
@@ -302,8 +204,9 @@ public class ExportAdmins extends ExportingToolBase {
jg.writeEndObject();
}
-
- /** Persists the connection for this entity. */
+ /**
+ * Persists the connection for this entity.
+ */
private void saveConnections( Entity entity, EntityManager em, JsonGenerator jg ) throws Exception {
jg.writeFieldName( "connections" );
@@ -326,116 +229,5 @@ public class ExportAdmins extends ExportingToolBase {
}
jg.writeEndObject();
}
-
- /*-
- * Set<String> collections = em.getCollections(entity);
- * for (String collection : collections) {
- * Results collectionMembers = em.getCollection(
- * entity, collection, null,
- * MAX_ENTITY_FETCH, Level.IDS, false);
- * write entity_id : { "collectionName" : [ids]
- * }
- * }
- *
- *
- * {
- * entity_id :
- * { collection_name :
- * [
- * collected_entity_id,
- * collected_entity_id
- * ]
- * },
- * f47ac10b-58cc-4372-a567-0e02b2c3d479 :
- * { "activtites" :
- * [
- * f47ac10b-58cc-4372-a567-0e02b2c3d47A,
- * f47ac10b-58cc-4372-a567-0e02b2c3d47B
- * ]
- * }
- * }
- *
- * http://jackson.codehaus.org/1.8.0/javadoc/org/codehaus/jackson/JsonGenerator.html
- *
- *
- *-
- * List<ConnectedEntityRef> connections = em.getConnections(entityId, query);
- */
-
-
- private void exportOrganizations() throws Exception, UnsupportedEncodingException {
-
-
- for ( Map.Entry<UUID, String> organizationName : getOrgs().entrySet() ) {
-
- // Let's skip the test entities.
- if ( organizationName.equals( properties.getProperty( "usergrid.test-account.organization" ) ) ) {
- continue;
- }
-
- OrganizationInfo acc = managementService.getOrganizationByUuid( organizationName.getKey() );
- logger.info( "Exporting Organization: " + acc.getName() );
-
- ExportOrg exportOrg = new ExportOrg( acc );
-
- List<UserInfo> users = managementService.getAdminUsersForOrganization( organizationName.getKey() );
-
- for ( UserInfo user : users ) {
- exportOrg.addAdmin( user.getUsername() );
- }
-
- // One file per Organization.
- saveOrganizationInFile( exportOrg );
- }
- }
-
-
- /**
- * Serialize an Organization into a json file.
- *
- * @param acc OrganizationInfo
- */
- private void saveOrganizationInFile( ExportOrg acc ) {
- try {
-
- File outFile = createOutputFile( "organization", acc.getName() );
- JsonGenerator jg = getJsonGenerator( outFile );
- jg.writeObject( acc );
- jg.close();
- }
- catch ( Exception e ) {
- throw new RuntimeException( e );
- }
- }
-
-
- public void streamOutput( File file, List<Entity> entities ) throws Exception {
- JsonFactory jsonFactory = new JsonFactory();
- // or, for data binding,
- // org.codehaus.jackson.mapper.MappingJsonFactory
- JsonGenerator jg = jsonFactory.createJsonGenerator( file, JsonEncoding.UTF8 );
- // or Stream, Reader
-
- jg.writeStartArray();
- for ( Entity entity : entities ) {
- jg.writeObject( entity );
- }
- jg.writeEndArray();
-
- jg.close();
- }
-
- // to generate the activities and user relationship, follow this:
-
- // write field name (id)
- // write start object
- // write field name (collection name)
- // write start array
- // write object/string
- // write another object
- // write end array
- // write end object
- // ...... more objects
- //
}
[2/2] incubator-usergrid git commit: Cleanup of ImportAdmins
Posted by gr...@apache.org.
Cleanup of ImportAdmins
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/5d386690
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/5d386690
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/5d386690
Branch: refs/heads/exportImportTool
Commit: 5d38669041426edf376634d115c49a1c466e2339
Parents: 47c850d
Author: GERey <gr...@apigee.com>
Authored: Mon Apr 13 16:16:05 2015 -0700
Committer: GERey <gr...@apigee.com>
Committed: Mon Apr 13 16:16:05 2015 -0700
----------------------------------------------------------------------
.../org/apache/usergrid/tools/ImportAdmins.java | 100 ++++++-------------
1 file changed, 31 insertions(+), 69 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5d386690/stack/tools/src/main/java/org/apache/usergrid/tools/ImportAdmins.java
----------------------------------------------------------------------
diff --git a/stack/tools/src/main/java/org/apache/usergrid/tools/ImportAdmins.java b/stack/tools/src/main/java/org/apache/usergrid/tools/ImportAdmins.java
index 1f78e5b..71dc8cb 100644
--- a/stack/tools/src/main/java/org/apache/usergrid/tools/ImportAdmins.java
+++ b/stack/tools/src/main/java/org/apache/usergrid/tools/ImportAdmins.java
@@ -19,8 +19,6 @@ package org.apache.usergrid.tools;
import java.io.File;
import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
@@ -32,25 +30,21 @@ import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.apache.usergrid.management.ApplicationInfo;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.io.filefilter.PrefixFileFilter;
+
import org.apache.usergrid.management.OrganizationInfo;
-import org.apache.usergrid.management.UserInfo;
import org.apache.usergrid.persistence.Entity;
import org.apache.usergrid.persistence.EntityManager;
import org.apache.usergrid.persistence.EntityRef;
import org.apache.usergrid.persistence.entities.Application;
-import org.apache.usergrid.persistence.exceptions.ApplicationAlreadyExistsException;
import org.apache.usergrid.persistence.exceptions.DuplicateUniquePropertyExistsException;
-import org.apache.usergrid.tools.bean.ExportOrg;
import org.apache.usergrid.utils.JsonUtils;
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionBuilder;
-import org.apache.commons.cli.Options;
-import org.apache.commons.io.filefilter.PrefixFileFilter;
-import org.apache.commons.lang.NullArgumentException;
-
import static org.apache.usergrid.persistence.Schema.PROPERTY_TYPE;
import static org.apache.usergrid.persistence.Schema.PROPERTY_UUID;
import static org.apache.usergrid.persistence.cassandra.CassandraService.MANAGEMENT_APPLICATION_ID;
@@ -63,7 +57,9 @@ public class ImportAdmins extends ToolBase {
private static final Logger logger = LoggerFactory.getLogger( ImportAdmins.class );
- /** Input directory where the .json export files are */
+ /**
+ * Input directory where the .json export files are
+ */
static final String INPUT_DIR = "inputDir";
static File importDir;
@@ -74,7 +70,7 @@ public class ImportAdmins extends ToolBase {
@Override
- @SuppressWarnings("static-access")
+ @SuppressWarnings( "static-access" )
public Options createOptions() {
Option hostOption =
@@ -114,7 +110,9 @@ public class ImportAdmins extends ToolBase {
}
- /** Import applications */
+ /**
+ * Import applications
+ */
private void importApplications() throws Exception {
String[] nanemspaceFileNames = importDir.list( new PrefixFileFilter( "application." ) );
logger.info( "Applications to read: " + nanemspaceFileNames.length );
@@ -153,22 +151,8 @@ public class ImportAdmins extends ToolBase {
Application application = jp.readValueAs( Application.class );
-
UUID appId = MANAGEMENT_APPLICATION_ID;
-// try {
-// appId = managementService.importApplication( info.getUuid(), application );
-// }
-// catch ( ApplicationAlreadyExistsException aaee ) {
-// ApplicationInfo appInfo = managementService.getApplicationInfo( application.getName() );
-//
-// if ( appInfo != null ) {
-// appId = appInfo.getId();
-// }
-// }
-//
-// echo( application );
-
EntityManager em = emf.getEntityManager( appId );
// we now need to remove all roles, they'll be imported again below
@@ -178,7 +162,7 @@ public class ImportAdmins extends ToolBase {
}
//load all the dictionaries
- @SuppressWarnings("unchecked") Map<String, Object> dictionaries =
+ @SuppressWarnings( "unchecked" ) Map<String, Object> dictionaries =
( Map<String, Object> ) application.getMetadata( "dictionaries" );
if ( dictionaries != null ) {
@@ -188,42 +172,15 @@ public class ImportAdmins extends ToolBase {
for ( Entry<String, Object> dictionary : dictionaries.entrySet() ) {
- @SuppressWarnings("unchecked") Map<Object, Object> value =
+ @SuppressWarnings( "unchecked" ) Map<Object, Object> value =
( Map<Object, Object> ) dictionary.getValue();
em.addMapToDictionary( appEntity, dictionary.getKey(), value );
}
}
-
- //load all counts and stats
- // @SuppressWarnings("unchecked")
- // Map<String, Object> stats = (Map<String, Object>) application.getMetadata("counters");
- //
- // for(Entry<String, Object> stat: stats.entrySet()){
- // String entryName = stat.getKey();
- // long amount = Long.parseLong(stat.getValue().toString());
- //
- //
- // //anything that deals with collections or entities, we set to 0 since they'll be incremented
- // during import
- // if(!entryName.startsWith("application.collection") && !entryName.equals("application.entities")){
- // em.incrementApplicationCounter(entryName, amount);
- // }
- //
- // }
-
- //explicity import all collections
-// @SuppressWarnings("unchecked") List<String> collections =
-// ( List<String> ) application.getMetadata( "collections" );
-//
-// for ( String collectionName : collections ) {
-// em.createApplicationCollection( collectionName );
-// }
-
-
while ( jp.nextValue() != JsonToken.END_ARRAY ) {
- @SuppressWarnings("unchecked") Map<String, Object> entityProps = jp.readValueAs( HashMap.class );
+ @SuppressWarnings( "unchecked" ) Map<String, Object> entityProps = jp.readValueAs( HashMap.class );
// Import/create the entity
UUID uuid = getId( entityProps );
String type = getType( entityProps );
@@ -268,6 +225,7 @@ public class ImportAdmins extends ToolBase {
}
}
+
private JsonParser getJsonParserForFile( File organizationFile ) throws Exception {
JsonParser jp = jsonFactory.createJsonParser( organizationFile );
jp.setCodec( new ObjectMapper() );
@@ -275,7 +233,9 @@ public class ImportAdmins extends ToolBase {
}
- /** Import collections. Collections files are named: collections.<application_name>.Timestamp.json */
+ /**
+ * Import collections. Collections files are named: collections.<application_name>.Timestamp.json
+ */
private void importCollections() throws Exception {
String[] collectionsFileNames = importDir.list( new PrefixFileFilter( "collections." ) );
logger.info( "Collections to read: " + collectionsFileNames.length );
@@ -326,7 +286,7 @@ public class ImportAdmins extends ToolBase {
jp.close();
- @SuppressWarnings("unchecked") String orgName =
+ @SuppressWarnings( "unchecked" ) String orgName =
( ( Map<String, String> ) application.getMetadata( "organization" ) ).get( "value" );
OrganizationInfo info = managementService.getOrganizationByName( orgName );
@@ -360,8 +320,9 @@ public class ImportAdmins extends ToolBase {
while ( jp.nextToken() != JsonToken.END_OBJECT ) {
try {
- importEntitysStuff( jp, em );
- }catch(NullPointerException nae){
+ importEntitysStuff( jp, em );
+ }
+ catch ( NullPointerException nae ) {
//consume the read of the value so we can move on to process the next token.
jp.readValueAs( JsonNode.class );
}
@@ -383,13 +344,12 @@ public class ImportAdmins extends ToolBase {
EntityRef ownerEntityRef = em.getRef( UUID.fromString( entityOwnerId ) );
-
jp.nextToken(); // start object
//this is done after getting the next token so that we only capture the JsonNode object
// and not the entire file response.
- if(ownerEntityRef==null){
- throw new NullPointerException("Couldn't retrieve entity ref from: "+entityOwnerId );
+ if ( ownerEntityRef == null ) {
+ throw new NullPointerException( "Couldn't retrieve entity ref from: " + entityOwnerId );
}
// Go inside the value after getting the owner entity id.
@@ -421,7 +381,7 @@ public class ImportAdmins extends ToolBase {
jp.nextToken();
- @SuppressWarnings("unchecked") Map<String, Object> dictionary = jp.readValueAs( HashMap.class );
+ @SuppressWarnings( "unchecked" ) Map<String, Object> dictionary = jp.readValueAs( HashMap.class );
em.addMapToDictionary( ownerEntityRef, dictionaryName, dictionary );
}
@@ -469,7 +429,9 @@ public class ImportAdmins extends ToolBase {
}
- /** Open up the import directory based on <code>importDir</code> */
+ /**
+ * Open up the import directory based on <code>importDir</code>
+ */
private void openImportDirectory( CommandLine line ) {
boolean hasInputDir = line.hasOption( INPUT_DIR );