Search in sources :

Example 1 with Concat

use of uk.gov.gchq.koryphe.impl.function.Concat in project gaffer-doc by gchq.

the class ConcatExample method concatObjects.

public void concatObjects() {
    // ---------------------------------------------------------
    final Concat function = new Concat();
    // ---------------------------------------------------------
    runExample(function, null, new Tuple2<>("foo", "bar"), new Tuple2<>("foo", null), new Tuple2<>("foo", ""), new Tuple2<>("foo", 1.2), new Tuple2<>(null, "bar"), new Tuple2<>(1, 2));
}
Also used : Concat(uk.gov.gchq.koryphe.impl.function.Concat)

Example 2 with Concat

use of uk.gov.gchq.koryphe.impl.function.Concat in project gaffer-doc by gchq.

the class GetElementsExample method getEntitiesAndReturnOnlySomeProperties.

public CloseableIterable<? extends Element> getEntitiesAndReturnOnlySomeProperties() {
    // ---------------------------------------------------------
    final Concat concat = new Concat();
    concat.setSeparator("|");
    final GetElements operation = new GetElements.Builder().input(new EntitySeed(2)).view(new View.Builder().edge("edge", new ViewElementDefinition.Builder().transientProperty("vertex|count", String.class).properties("vertex|count").transformer(new ElementTransformer.Builder().select(IdentifierType.SOURCE.name(), "count").execute(concat).project("vertex|count").build()).build()).build()).build();
    return runExample(operation, null);
}
Also used : Concat(uk.gov.gchq.koryphe.impl.function.Concat) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements)

Example 3 with Concat

use of uk.gov.gchq.koryphe.impl.function.Concat in project Gaffer by gchq.

the class GetRDDOfAllElementsHandlerIT method testGetAllElementsInRDDWithView.

private void testGetAllElementsInRDDWithView(final Graph graph, final GetRDDOfAllElements getRDD) throws OperationException {
    final Set<Element> expectedElements = new HashSet<>();
    getElements().stream().filter(e -> e.getGroup().equals(TestGroups.EDGE)).map(e -> (Edge) e).map(e -> {
        e.putProperty("newProperty", e.getSource().toString() + "," + e.getProperty(TestPropertyNames.COUNT));
        return e;
    }).filter(e -> e.getProperty("newProperty").equals("0,2")).forEach(expectedElements::add);
    getRDD.setView(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().transientProperty("newProperty", String.class).transformer(new ElementTransformer.Builder().select(IdentifierType.SOURCE.name(), TestPropertyNames.COUNT).execute(new Concat()).project("newProperty").build()).postTransformFilter(new ElementFilter.Builder().select("newProperty").execute(new IsEqual("0,2")).build()).build()).build());
    final RDD<Element> rdd = graph.execute(getRDD, USER);
    if (rdd == null) {
        fail("No RDD returned");
    }
    final Element[] returnedElements = (Element[]) rdd.collect();
    final Set<Element> results = new HashSet<>(Arrays.asList(returnedElements));
    assertEquals(expectedElements, results);
}
Also used : ByteEntityAccumuloElementConverter(uk.gov.gchq.gaffer.accumulostore.key.core.impl.byteEntity.ByteEntityAccumuloElementConverter) AccumuloElementConverter(uk.gov.gchq.gaffer.accumulostore.key.AccumuloElementConverter) Arrays(java.util.Arrays) CachableBlockFile(org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile) FileSystem(org.apache.hadoop.fs.FileSystem) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) Element(uk.gov.gchq.gaffer.data.element.Element) Graph(uk.gov.gchq.gaffer.graph.Graph) Configuration(org.apache.hadoop.conf.Configuration) Concat(uk.gov.gchq.koryphe.impl.function.Concat) Path(org.apache.hadoop.fs.Path) Value(org.apache.accumulo.core.data.Value) Edge(uk.gov.gchq.gaffer.data.element.Edge) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) AccumuloProperties(uk.gov.gchq.gaffer.accumulostore.AccumuloProperties) Compression(org.apache.accumulo.core.file.rfile.bcfile.Compression) TestPropertyNames(uk.gov.gchq.gaffer.commonutil.TestPropertyNames) Set(java.util.Set) ClassicAccumuloElementConverter(uk.gov.gchq.gaffer.accumulostore.key.core.impl.classic.ClassicAccumuloElementConverter) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) TempDir(org.junit.jupiter.api.io.TempDir) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) GetRDDOfAllElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfAllElements) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) OperationException(uk.gov.gchq.gaffer.operation.OperationException) RDD(org.apache.spark.rdd.RDD) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Assertions.fail(org.junit.jupiter.api.Assertions.fail) StoreException(uk.gov.gchq.gaffer.store.StoreException) User(uk.gov.gchq.gaffer.user.User) CompactionConfig(org.apache.accumulo.core.client.admin.CompactionConfig) EnumSource(org.junit.jupiter.params.provider.EnumSource) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) AccumuloStore(uk.gov.gchq.gaffer.accumulostore.AccumuloStore) ByteEntityKeyPackage(uk.gov.gchq.gaffer.accumulostore.key.core.impl.byteEntity.ByteEntityKeyPackage) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) Key(org.apache.accumulo.core.data.Key) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ClassicKeyPackage(uk.gov.gchq.gaffer.accumulostore.key.core.impl.classic.ClassicKeyPackage) Logger(org.slf4j.Logger) IOException(java.io.IOException) Entity(uk.gov.gchq.gaffer.data.element.Entity) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) AccumuloException(org.apache.accumulo.core.client.AccumuloException) File(java.io.File) AccumuloConfiguration(org.apache.accumulo.core.conf.AccumuloConfiguration) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) RFile(org.apache.accumulo.core.file.rfile.RFile) Schema(uk.gov.gchq.gaffer.store.schema.Schema) AbstractGetRDDHandler(uk.gov.gchq.gaffer.sparkaccumulo.operation.handler.AbstractGetRDDHandler) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Element(uk.gov.gchq.gaffer.data.element.Element) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) Concat(uk.gov.gchq.koryphe.impl.function.Concat) Edge(uk.gov.gchq.gaffer.data.element.Edge) HashSet(java.util.HashSet)

Example 4 with Concat

use of uk.gov.gchq.koryphe.impl.function.Concat in project Gaffer by gchq.

the class RoadTrafficCsvElementGenerator2Test method shouldParseSampleDataWithGenericFunctions.

@Test
public void shouldParseSampleDataWithGenericFunctions() throws IOException {
    // Given
    CsvLinesToMaps parseCsv = new CsvLinesToMaps().header("Region Name (GO)", "ONS LACode", "ONS LA Name", "CP", "S Ref E", "S Ref N", "Road", "A-Junction", "A Ref E", "A Ref N", "B-Junction", "B Ref E", "B Ref N", "RCat", "iDir", "Year", "dCount", "Hour", "PC", "2WMV", "CAR", "BUS", "LGV", "HGVR2", "HGVR3", "HGVR4", "HGVA3", "HGVA5", "HGVA6", "HGV", "AMV").firstRow(1);
    IterableFunction<Map<String, Object>, Tuple<String>> toTuples = new IterableFunction<>(new MapToTuple<String>());
    IterableFunction<Tuple<String>, Tuple<String>> transformTuples = new IterableFunction(new FunctionChain.Builder<>().execute(new String[] { "Road", "A-Junction" }, new Concat(":"), new String[] { "A-Junction" }).execute(new String[] { "Road", "B-Junction" }, new Concat(":"), new String[] { "B-Junction" }).execute(new String[] { "A Ref E", "A Ref N" }, new Concat(), new String[] { "A-Location" }).execute(new String[] { "B Ref E", "B Ref N" }, new Concat(), new String[] { "B-Location" }).execute(new String[] { "THIS" }, new CreateRoadTrafficFreqMap(), new String[] { "countByVehicleType" }).execute(new String[] { "countByVehicleType" }, new CallMethod("getTotal"), new String[] { "total-count" }).execute(new String[] { "dCount" }, new ParseTime().timeZone("UTC"), new String[] { "timestamp" }).execute(new String[] { "Hour" }, new FunctionChain(new ToInteger(), new MultiplyBy(60 * 60 * 1000), new ToLong()), new String[] { "hoursInMilliseconds" }).execute(new String[] { "timestamp", "hoursInMilliseconds" }, new FunctionChain(new ApplyBiFunction<>(new Sum()), new ToString(), new ParseDate()), new String[] { "startDate" }).execute(new String[] { "startDate" }, new DateToTimeBucketEnd(TimeBucket.HOUR), new String[] { "endDate" }).build());
    TuplesToElements toElements = new TuplesToElements().element(new ElementTupleDefinition("RegionContainsLocation").source("Region Name (GO)").destination("ONS LA Name")).element(new ElementTupleDefinition("LocationContainsRoad").source("ONS LA Name").destination("Road")).element(new ElementTupleDefinition("RoadHasJunction").source("Road").destination("A-Junction")).element(new ElementTupleDefinition("RoadHasJunction").source("Road").destination("B-Junction")).element(new ElementTupleDefinition("JunctionLocatedAt").source("A-Junction").destination("A-Location")).element(new ElementTupleDefinition("JunctionLocatedAt").source("B-Junction").destination("B-Location")).element(new ElementTupleDefinition("RoadUse").source("A-Junction").destination("B-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count")).element(new ElementTupleDefinition("JunctionUse").vertex("A-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count")).element(new ElementTupleDefinition("JunctionUse").vertex("B-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count"));
    HyperLogLogPlusEntityGenerator addCardinalities = new HyperLogLogPlusEntityGenerator().countProperty("count").edgeGroupProperty("edgeGroup").cardinalityPropertyName("hllp");
    // Apply functions
    final FunctionChain<List<String>, Iterable<Element>> generator2 = new FunctionChain.Builder<List<String>, Iterable<Element>>().execute(parseCsv).execute(toTuples).execute(transformTuples).execute(toElements).execute(addCardinalities).build();
    // Uncomment the following for debugging
    // System.out.println(new String(JSONSerialiser.serialise(generator2, true)));
    final List<String> lines = IOUtils.readLines(createInputStream());
    final List<Element> elements2 = Lists.newArrayList(generator2.apply(lines));
    // Then - the results should be the same as those generated using the original element generator
    final RoadTrafficStringElementGenerator generator1 = new RoadTrafficStringElementGenerator();
    final List<Element> elements1;
    try (final InputStream inputStream = createInputStream()) {
        elements1 = Lists.newArrayList(generator1.apply(() -> new LineIterator(new InputStreamReader(inputStream))));
    }
    JSONSerialiser.getMapper();
    SimpleClassNameCache.setUseFullNameForSerialisation(false);
    elements1.forEach(e -> e.removeProperty("hllp"));
    elements2.forEach(e -> e.removeProperty("hllp"));
    ElementUtil.assertElementEquals(elements1, elements2);
}
Also used : MultiplyBy(uk.gov.gchq.koryphe.impl.function.MultiplyBy) DateToTimeBucketEnd(uk.gov.gchq.gaffer.time.function.DateToTimeBucketEnd) Element(uk.gov.gchq.gaffer.data.element.Element) ParseDate(uk.gov.gchq.koryphe.impl.function.ParseDate) ToString(uk.gov.gchq.koryphe.impl.function.ToString) LineIterator(org.apache.commons.io.LineIterator) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) Concat(uk.gov.gchq.koryphe.impl.function.Concat) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) CsvLinesToMaps(uk.gov.gchq.koryphe.impl.function.CsvLinesToMaps) List(java.util.List) TuplesToElements(uk.gov.gchq.gaffer.data.element.function.TuplesToElements) FunctionChain(uk.gov.gchq.koryphe.impl.function.FunctionChain) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) HyperLogLogPlusEntityGenerator(uk.gov.gchq.gaffer.sketches.clearspring.cardinality.HyperLogLogPlusEntityGenerator) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ToInteger(uk.gov.gchq.koryphe.impl.function.ToInteger) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) ElementTupleDefinition(uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition) ParseTime(uk.gov.gchq.koryphe.impl.function.ParseTime) ToString(uk.gov.gchq.koryphe.impl.function.ToString) FreqMap(uk.gov.gchq.gaffer.types.FreqMap) Map(java.util.Map) MapToTuple(uk.gov.gchq.koryphe.impl.function.MapToTuple) Tuple(uk.gov.gchq.koryphe.tuple.Tuple) Test(org.junit.Test)

Example 5 with Concat

use of uk.gov.gchq.koryphe.impl.function.Concat in project Gaffer by gchq.

the class TransformationIT method shouldCreateTransientEdgeProperty.

@Test
@TraitRequirement(StoreTrait.TRANSFORMATION)
public void shouldCreateTransientEdgeProperty() throws OperationException {
    // Given
    final GetElements getEdges = new GetElements.Builder().input(new EdgeSeed(SOURCE_1, DEST_1, false)).view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().transientProperty(TestPropertyNames.TRANSIENT_1, String.class).transformer(new ElementTransformer.Builder().select(IdentifierType.SOURCE.name(), TestPropertyNames.INT).execute(new Concat()).project(TestPropertyNames.TRANSIENT_1).build()).build()).build()).build();
    // When
    final List<Element> results = Lists.newArrayList(graph.execute(getEdges, getUser()));
    assertThat(results).isNotNull();
    for (final Element result : results) {
        assertThat(result.getProperty(TestPropertyNames.TRANSIENT_1)).isEqualTo(SOURCE_1 + "," + result.getProperty(TestPropertyNames.INT));
    }
}
Also used : Concat(uk.gov.gchq.koryphe.impl.function.Concat) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Test(org.junit.Test) TraitRequirement(uk.gov.gchq.gaffer.integration.TraitRequirement)

Aggregations

Concat (uk.gov.gchq.koryphe.impl.function.Concat)9 Element (uk.gov.gchq.gaffer.data.element.Element)6 Test (org.junit.Test)5 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)5 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)3 TraitRequirement (uk.gov.gchq.gaffer.integration.TraitRequirement)3 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)3 List (java.util.List)2 Sets (com.google.common.collect.Sets)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 AccumuloException (org.apache.accumulo.core.client.AccumuloException)1