Skip to content

Commit f49a1af

Browse files
authored
Merge pull request #26 from hkuich/attributes_in_relation
Attributes in relation
2 parents 81b0e4e + 7fc3cb6 commit f49a1af

27 files changed

+521
-251
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Use GraMi (**Gra**kn**Mi**grator) to take care of your data migration for you. G
2222
- supports any tabular data file with your separator of choice (i.e.: csv, tsv, whatever-sv...)
2323
- supports gzipped files
2424
- ignores unnecessary columns
25-
- [Entity](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Entities), [Relation](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Relations), and [Nested Relations](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Nested-Relations) Migration:
25+
- [Attribute](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Attributes), [Entity](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Entities), [Relation](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Relations), [Nested Relations](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Nested-Relations), [Attribute-Player Relations](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Attribute-Player-Relations) Migration:
2626
- migrate required/optional attributes of any grakn type (string, boolean, long, double, datetime)
2727
- migrate required/optional role players (entity & relations)
2828
- migrate list-like attribute columns as n attributes (recommended procedure until attribute lists are fully supported by Grakn)
@@ -53,9 +53,11 @@ The processor configuration file describes how you want data to be migrated give
5353

5454
Depending on what you would like to migrate, see here:
5555

56+
- [Attribute Processor Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Attributes#processor-config)
5657
- [Entity Processor Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Entities#processor-config)
5758
- [Relation Processor Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Relations#processor-config)
5859
- [Nested Relation Processor Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Nested-Relations#processor-config)
60+
- [Attribute-Player Relation](https://github.com/bayer-science-for-a-better-life/grami/wiki/Attribute-Player-Relations#processor-config)
5961

6062
### Data Configuration
6163

@@ -65,10 +67,13 @@ A good point to start the performance optimization is to set the number of threa
6567

6668
See Example here:
6769

70+
- [Attribute Data Config Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Attributes#data-config)
6871
- [Entity Data Config Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Entities#data-config)
6972
- [Relation Data Config Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Relations#data-config)
7073
- [Nested Relation - Match by Attribute(s) - Data Config Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Nested-Relations#data-config---attribute-matching)
7174
- [Nested Relation - Match by Player(s) - Data Config Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Migrating-Nested-Relations#data-config---player-matching)
75+
- [Attribute-Player Relation - Data Config Example](https://github.com/bayer-science-for-a-better-life/grami/wiki/Attribute-Player-Relations#data-config)
76+
- [Custom Migration Order](https://github.com/bayer-science-for-a-better-life/grami/wiki/Custom-Migration-Order)
7277

7378
### Migrate Data
7479

@@ -104,7 +109,7 @@ public class Migration {
104109

105110
public static void main(String[] args) throws IOException {
106111
GraknMigrator mig = new GraknMigrator(migrationConfig, migrationStatus, true);
107-
mig.migrate(true, true, true, true);
112+
mig.migrate();
108113
}
109114
}
110115
```
@@ -122,13 +127,11 @@ There is this [example repository](https://github.com/bayer-science-for-a-better
122127

123128
## Compatibility
124129

125-
GraMi version >= 0.1.0-alpha-12 is tested for:
126-
- [grakn-core](https://github.com/graknlabs/grakn) >= 2.0-alpha-9
127-
- using [client-java](https://github.com/graknlabs/client-java) >= 2.0.0-alpha-12
130+
GraMi version == 0.1.1 is tested for:
131+
- [grakn-core](https://github.com/graknlabs/grakn) == 2.0.1
128132

129133
GraMi version < 0.1.0 is tested for:
130-
- [grakn-core](https://github.com/graknlabs/grakn) >= 1.8.2
131-
- [client-java](https://github.com/graknlabs/client-java) >= 1.8.3
134+
- [grakn-core](https://github.com/graknlabs/grakn) >= 1.8.2
132135

133136
Find the Readme for GraMi for grakn < 2.0 [here](https://github.com/bayer-science-for-a-better-life/grami/blob/b3d6d272c409d6c40254354027b49f90b255e1c3/README.md)
134137

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'com.github.bayer-science-for-a-better-life'
8-
version '0.1.0'
8+
version '0.1.1'
99

1010
repositories {
1111
mavenCentral()

src/main/java/cli/GramiCLI.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.io.IOException;
1010

11-
@CommandLine.Command(description="Welcome to the CLI of GraMi - your grakn data migration tool", name = "grami", version = "0.1.0-alpha-12", mixinStandardHelpOptions = true)
11+
@CommandLine.Command(description="Welcome to the CLI of GraMi - your grakn data migration tool", name = "grami", version = "0.1.1", mixinStandardHelpOptions = true)
1212
public class GramiCLI {
1313

1414
public static void main(String[] args) {
@@ -46,9 +46,6 @@ class MigrateCommand implements Runnable {
4646
@CommandLine.Option(names = {"-cm", "--cleanMigration"}, description = "optional - delete old schema and data and restart migration from scratch - default: continue previous migration, if exists")
4747
private boolean cleanMigration;
4848

49-
@CommandLine.Option(names = {"-sc", "--scope"}, description = "optional - set migration scope: 0 - apply schema only (Note: this has no effect unless you also set the cleanMigration flag to true.); 1 - migrate entities; 2 - migrate entities & relations; 3 - migrate entites, relations, & relation-with-relations; everything else defaults to 4 - migrate all (entities, relations, relation-with-relations, append-attributes")
50-
private int scope = 4;
51-
5249
@Override
5350
public void run() {
5451
spec.commandLine().getOut().println("############## GraMi migration ###############");
@@ -60,24 +57,12 @@ public void run() {
6057
spec.commandLine().getOut().println("\tdatabase: " + databaseName);
6158
spec.commandLine().getOut().println("\tgrakn server: " + graknURI);
6259
spec.commandLine().getOut().println("\tdelete database and all data in it for a clean new migration?: " + cleanMigration);
63-
spec.commandLine().getOut().println("\tmigration scope: " + scope);
6460

6561
final MigrationConfig migrationConfig = new MigrationConfig(graknURI, databaseName, schemaFilePath, dataConfigFilePath, processorConfigFilePath);
6662

6763
try {
6864
GraknMigrator mig = new GraknMigrator(migrationConfig, migrationStatusFilePath, cleanMigration);
69-
70-
if (scope != 0 && scope != 1 && scope != 2 && scope != 3) {
71-
scope = 4;
72-
}
73-
74-
switch (scope) {
75-
case 0: mig.migrate(false, false, false, false); break;
76-
case 1: mig.migrate(true, false, false, false); break;
77-
case 2: mig.migrate(true, true, false, false); break;
78-
case 3: mig.migrate(true, true, true, false); break;
79-
case 4: mig.migrate(true, true, true, true); break;
80-
}
65+
mig.migrate();
8166
} catch (IOException e) {
8267
e.printStackTrace();
8368
}

src/main/java/configuration/DataConfigEntry.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class DataConfigEntry {
1313
private DataConfigGeneratorMapping[] relationPlayers;
1414
private int batchSize;
1515
private int threads;
16+
private Integer orderBefore;
17+
private Integer orderAfter;
1618

1719
public String getDataPath() {
1820
return dataPath;
@@ -46,6 +48,14 @@ public int getThreads() {
4648
return threads;
4749
}
4850

51+
public Integer getOrderBefore() {
52+
return orderBefore;
53+
}
54+
55+
public Integer getOrderAfter() {
56+
return orderAfter;
57+
}
58+
4959
public ArrayList<DataConfigEntry.DataConfigGeneratorMapping> getMatchAttributes() {
5060
ArrayList<DataConfigEntry.DataConfigGeneratorMapping> matchAttributes = new ArrayList<>();
5161
for (DataConfigEntry.DataConfigGeneratorMapping attributeMapping: getAttributes()) {

src/main/java/configuration/ProcessorConfigEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ConceptGenerator getRelationPlayerGenerator(String key) {
6666

6767
public HashMap<String,ConceptGenerator> getRelationRequiredPlayers() {
6868
HashMap<String,ConceptGenerator> relationPlayers = new HashMap<>();
69-
if (processorType.equals("relation") || processorType.equals("relation-with-relation")) {
69+
if (processorType.equals("relation") || processorType.equals("nested-relation") || processorType.equals("attribute-relation")) {
7070
HashMap<String, ConceptGenerator> playerGenerators = getConceptGenerators().get("players");
7171
for (Map.Entry<String, ConceptGenerator> pg: playerGenerators.entrySet()) {
7272
if (pg.getValue().isRequired()) {

src/main/java/generator/AppendAttributeGenerator.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
import java.util.HashMap;
1616
import java.util.Map;
1717

18-
import static generator.GeneratorUtil.*;
18+
import static generator.GeneratorUtil.addAttribute;
19+
import static generator.GeneratorUtil.malformedRow;
1920

2021
public class AppendAttributeGenerator extends InsertGenerator {
2122

22-
private final DataConfigEntry dce;
23-
private final ProcessorConfigEntry pce;
2423
private static final Logger appLogger = LogManager.getLogger("com.bayer.dt.grami");
2524
private static final Logger dataLogger = LogManager.getLogger("com.bayer.dt.grami.data");
25+
private final DataConfigEntry dce;
26+
private final ProcessorConfigEntry pce;
2627

2728
public AppendAttributeGenerator(DataConfigEntry dataConfigEntry,
2829
ProcessorConfigEntry processorConfigEntry) {
@@ -33,24 +34,25 @@ public AppendAttributeGenerator(DataConfigEntry dataConfigEntry,
3334
}
3435

3536
public HashMap<String, ArrayList<ArrayList<ThingVariable<?>>>> graknAppendAttributeInsert(ArrayList<String> rows,
36-
String header) throws Exception {
37+
String header, int rowCounter) throws Exception {
3738
HashMap<String, ArrayList<ArrayList<ThingVariable<?>>>> matchInsertPatterns = new HashMap<>();
3839

3940
ArrayList<ArrayList<ThingVariable<?>>> matchPatterns = new ArrayList<>();
4041
ArrayList<ArrayList<ThingVariable<?>>> insertPatterns = new ArrayList<>();
4142

4243
int insertCounter = 0;
43-
44+
int batchCounter = 1;
4445
for (String row : rows) {
45-
ArrayList<ArrayList<ThingVariable<?>>> tmp = graknAppendAttributeQueryFromRow(row, header, insertCounter);
46+
47+
ArrayList<ArrayList<ThingVariable<?>>> tmp = graknAppendAttributeQueryFromRow(row, header, insertCounter, rowCounter + batchCounter);
4648
if (tmp != null) {
4749
if (tmp.get(0) != null && tmp.get(1) != null) {
4850
matchPatterns.add(tmp.get(0));
4951
insertPatterns.add(tmp.get(1));
5052
insertCounter++;
5153
}
5254
}
53-
55+
batchCounter = batchCounter + 1;
5456
}
5557
matchInsertPatterns.put("match", matchPatterns);
5658
matchInsertPatterns.put("insert", insertPatterns);
@@ -59,7 +61,8 @@ public HashMap<String, ArrayList<ArrayList<ThingVariable<?>>>> graknAppendAttrib
5961

6062
public ArrayList<ArrayList<ThingVariable<?>>> graknAppendAttributeQueryFromRow(String row,
6163
String header,
62-
int insertCounter) throws Exception {
64+
int insertCounter,
65+
int rowCounter) throws Exception {
6366
String fileSeparator = dce.getSeparator();
6467
String[] rowTokens = row.split(fileSeparator);
6568
String[] columnNames = header.split(fileSeparator);
@@ -77,7 +80,7 @@ public ArrayList<ArrayList<ThingVariable<?>>> graknAppendAttributeQueryFromRow(S
7780
Thing appendAttributeMatchPattern = addEntityToMatchPattern(insertCounter);
7881
for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForMatchAttribute : dce.getAttributes()) {
7982
if (generatorMappingForMatchAttribute.isMatch()) {
80-
appendAttributeMatchPattern = addAttribute(rowTokens, appendAttributeMatchPattern, columnNames, generatorMappingForMatchAttribute, pce, generatorMappingForMatchAttribute.getPreprocessor());
83+
appendAttributeMatchPattern = addAttribute(rowTokens, appendAttributeMatchPattern, columnNames, rowCounter, generatorMappingForMatchAttribute, pce, generatorMappingForMatchAttribute.getPreprocessor());
8184
}
8285
}
8386
matchPatterns.add(appendAttributeMatchPattern);
@@ -87,7 +90,7 @@ public ArrayList<ArrayList<ThingVariable<?>>> graknAppendAttributeQueryFromRow(S
8790
Thing appendAttributeInsertPattern = null;
8891
for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForAppendAttribute : dce.getAttributes()) {
8992
if (!generatorMappingForAppendAttribute.isMatch()) {
90-
appendAttributeInsertPattern = addAttribute(rowTokens, thingVar, columnNames, generatorMappingForAppendAttribute, pce, generatorMappingForAppendAttribute.getPreprocessor());
93+
appendAttributeInsertPattern = addAttribute(rowTokens, thingVar, rowCounter, columnNames, generatorMappingForAppendAttribute, pce, generatorMappingForAppendAttribute.getPreprocessor());
9194
}
9295
}
9396
if (appendAttributeInsertPattern != null) {
@@ -100,10 +103,10 @@ public ArrayList<ArrayList<ThingVariable<?>>> graknAppendAttributeQueryFromRow(S
100103

101104

102105
if (isValid(assembledPatterns)) {
103-
appLogger.debug("valid query: <" + assembleQuery(assembledPatterns).toString() + ">");
106+
appLogger.debug("valid query: <" + assembleQuery(assembledPatterns) + ">");
104107
return assembledPatterns;
105108
} else {
106-
dataLogger.warn("in datapath <" + dce.getDataPath() + ">: skipped row b/c does not contain at least one match attribute and one insert attribute. Faulty tokenized row: " + Arrays.toString(rowTokens));
109+
dataLogger.warn("in datapath <" + dce.getDataPath() + ">: skipped row " + rowCounter + " b/c does not contain at least one match attribute and one insert attribute. Faulty tokenized row: " + Arrays.toString(rowTokens));
107110
return null;
108111
}
109112
}

src/main/java/generator/AttributeInsertGenerator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@ public AttributeInsertGenerator(DataConfigEntry dataConfigEntry, ProcessorConfig
3131
}
3232

3333
public ArrayList<ThingVariable<?>> graknAttributeInsert(ArrayList<String> rows,
34-
String header) throws IllegalArgumentException {
34+
String header, int rowCounter) throws IllegalArgumentException {
3535
ArrayList<ThingVariable<?>> patterns = new ArrayList<>();
36+
int batchCount = 1;
3637
for (String row : rows) {
3738
try {
38-
ThingVariable<?> temp = graknAttributeQueryFromRow(row, header);
39+
ThingVariable<?> temp = graknAttributeQueryFromRow(row, header, rowCounter + batchCount);
3940
if (temp != null) {
4041
patterns.add(temp);
4142
}
4243
} catch (Exception e) {
4344
e.printStackTrace();
4445
}
46+
batchCount = batchCount + 1;
4547
}
4648
return patterns;
4749
}
4850

4951
public ThingVariable<Attribute> graknAttributeQueryFromRow(String row,
50-
String header) throws Exception {
52+
String header, int rowCounter) throws Exception {
5153
String fileSeparator = dce.getSeparator();
5254
String[] rowTokens = row.split(fileSeparator);
5355
String[] columnNames = header.split(fileSeparator);
@@ -58,7 +60,7 @@ public ThingVariable<Attribute> graknAttributeQueryFromRow(String row,
5860
Attribute attributeInsertStatement = null;
5961

6062
for (DataConfigEntry.DataConfigGeneratorMapping generatorMappingForAttribute : dce.getAttributes()) {
61-
attributeInsertStatement = addValue(rowTokens, attributeInitialStatement, columnNames, generatorMappingForAttribute, pce, generatorMappingForAttribute.getPreprocessor());
63+
attributeInsertStatement = addValue(rowTokens, attributeInitialStatement, rowCounter, columnNames, generatorMappingForAttribute, pce, generatorMappingForAttribute.getPreprocessor());
6264
}
6365

6466
if (attributeInsertStatement != null) {
@@ -68,7 +70,7 @@ public ThingVariable<Attribute> graknAttributeQueryFromRow(String row,
6870
appLogger.debug("valid query: <insert " + attributeInsertStatement.toString() + ";>");
6971
return attributeInsertStatement;
7072
} else {
71-
dataLogger.warn("in datapath <" + dce.getDataPath() + ">: skipped row b/c does not have a proper <isa> statement or is missing required attributes. Faulty tokenized row: " + Arrays.toString(rowTokens));
73+
dataLogger.warn("in datapath <" + dce.getDataPath() + ">: skipped row " + rowCounter + " b/c does not have a proper <isa> statement or is missing required attributes. Faulty tokenized row: " + Arrays.toString(rowTokens));
7274
return null;
7375
}
7476
} else {

0 commit comments

Comments
 (0)