@Override public Serialiser<Object, byte[]> getSerialisation() { MultiSerialiser multiSerialiser; try { multiSerialiser = JSONSerialiser.deserialise(StreamUtil.openStream(getClass(), path), MultiSerialiser.class); } catch (SerialisationException e) { throw new RuntimeException(e); } return multiSerialiser; }
@Override public Schema run() throws OperationException { /// [graph] create a graph using our schema and store properties // --------------------------------------------------------- final Graph graph = new Graph.Builder() .graphId("graph1") .addSchemas(StreamUtil.openStreams(getClass(), "SchemaExample/schema")) .storeProperties(StreamUtil.openStream(getClass(), "mockaccumulostore.properties")) .build(); // --------------------------------------------------------- final Schema schema = graph.getSchema(); try { log("SCHEMA", new String(schema.toJson(true), CommonConstants.UTF_8)); } catch (final UnsupportedEncodingException e) { throw new RuntimeException(e); } return schema; }
/** * Create an array of {@link InputStream}s from the provided list of {@link URI}s. * * @param uris the URIs to open as input streams * @return an array of input streams * @throws IOException if there was an error opening the streams */ public static InputStream[] openStreams(final URI... uris) throws IOException { final InputStream[] schemas = new InputStream[uris.length]; for (int pos = 0; pos < uris.length; pos++) { try { schemas[pos] = openStream(uris[pos]); } catch (final Exception e) { int closedStreamsCount = closeStreams(schemas); LOGGER.info("Closed {} input streams", closedStreamsCount); throw e; } } return schemas; }
String folderPathChecked = getFormattedPath(folderPath); .forEach(file -> { try { inputStreams.add(openStream(clazz, file)); } catch (final Exception e) { int closedStreamsCount = closeStreams(inputStreams.toArray(new InputStream[inputStreams.size()])); LOGGER.info(String.format("Closed %s input streams", closedStreamsCount));
/** * Open the schema files located at the same location as the provided * class. * * @param clazz the class determining the location of the schema files * @return an array of {@link InputStream}s representing the schema files */ public static InputStream[] schemas(final Class clazz) { return openStreams(clazz, SCHEMA_FOLDER); }
/** * Open the file found at the the specified path under the location of the given * class. * * @param clazz the class location * @param path the path in the class location * @return an input stream representating the requested file * @throws IllegalArgumentException if there was an error opening the stream */ public static InputStream openStream(final Class clazz, final String path) throws IllegalArgumentException { final String checkedPath = formatPathForOpenStream(path); final InputStream resourceAsStream = clazz.getResourceAsStream(checkedPath); return (null != resourceAsStream) ? resourceAsStream : processException(path); }
/** * Format a path to ensure that it begins with a '/' character. * * @param path the path to format * @return a correctly formatted path string */ public static String formatPathForOpenStream(final String path) { if (StringUtils.isEmpty(path)) { processException(path); } return path.startsWith("/") ? path : "/" + path; } }
protected Graph createExampleGraph() { final Graph graph = new Graph.Builder() .addSchemas(StreamUtil.openStreams(getClass(), "/example/operation/schema")) .storeProperties(StreamUtil.openStream(getClass(), "/example/operation/mockaccumulostore.properties")) .build(); // Create data generator final DataGenerator dataGenerator = new DataGenerator(); // Load data into memory final List<String> data = DataUtils.loadData(StreamUtil.openStream(getClass(), "/example/operation/data.txt", true)); //add the edges to the graph using an operation chain consisting of: //generateElements - generating edges from the data (note these are directed edges) //addElements - add the edges to the graph final OperationChain addOpChain = new OperationChain.Builder() .first(new GenerateElements.Builder<String>() .generator(dataGenerator) .objects(data) .build()) .then(new AddElements()) .build(); try { graph.execute(addOpChain, new User()); } catch (OperationException e) { throw new RuntimeException(e); } return graph; }
protected InputStream[] getSchemas() { return StreamUtil.openStreams(LoadAndQuery.class, schemaFolderLocation, true); }
protected Graph createExampleGraph() { final Graph graph = new Graph.Builder() .graphId("graph1") .addSchemas(StreamUtil.openStreams(getClass(), "operation/schema")) .storeProperties(StreamUtil.openStream(getClass(), "mockaccumulostore.properties")) .build(); data = IOUtils.readLines(StreamUtil.openStream(getClass(), "operation/data.txt")); } catch (IOException e) { throw new RuntimeException(e);
@Test public void shouldMatchHistoricalSerialisation() throws IOException, GafferCheckedException { final String fromDisk = IOUtils.readLines(StreamUtil.openStream(getClass(), path)) .stream() .collect(Collectors.joining("\n")); final MultiSerialiser multiSerialiser = new MultiSerialiser() .addSerialiser((byte) 0, new StringSerialiser(), String.class) .addSerialiser((byte) 1, new CompactRawLongSerialiser(), Long.class) .addSerialiser((byte) 2, new CompactRawIntegerSerialiser(), Integer.class); final String fromCode = new String(JSONSerialiser.serialise(multiSerialiser, true)); assertEquals(fromDisk, fromCode); }
public Builder addSchemas(final URI... schemaURI) { if (null != schemaURI) { try { addSchemas(StreamUtil.openStreams(schemaURI)); } catch (final IOException e) { throw new SchemaException(UNABLE_TO_READ_SCHEMA_FROM_URI, e); } } return this; }
public void simpleExportWithCustomGraph() { // --------------------------------------------------------- final Schema schema = Schema.fromJson(StreamUtil.openStreams(getClass(), "operation/schema")); final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(getClass(), "othermockaccumulostore.properties")); final OperationChain<Iterable<? extends Element>> opChain = new OperationChain.Builder() .first(new GetAllElements.Builder() .view(new View.Builder() .edge("edge") .build()) .build()) .then(new ExportToOtherGraph.Builder() .graphId("newGraphId") .schema(schema) .storeProperties(storeProperties) .build()) .build(); // --------------------------------------------------------- showExample(opChain, "This example will export all Edges with group 'edge' to another Gaffer graph with new ID 'newGraphId'. " + "The new graph will have the custom provided schema (note it must contain the same Edge group 'edge' otherwise the exported edges will be invalid') and custom store properties. " + "The store properties could be any store properties e.g. Accumulo, HBase, Map, Proxy store properties."); }
/** * Open the schema file located at the same location as the provided * class. * * @param clazz the class determining the location of the schema file * @return an {@link InputStream} representing the schema file */ public static InputStream schema(final Class clazz) { return openStream(clazz, SCHEMA); }
public static Schema createSchema(final Long timeToLive) { final Schema.Builder builder = new Schema.Builder() .json(StreamUtil.openStreams(GafferResultCacheUtil.class, "gafferResultCache/schema")); if (null != timeToLive) { builder.merge(new Schema.Builder() .type("timestamp", new TypeDefinition.Builder() .validateFunctions(new AgeOff(timeToLive)) .build()) .build()); } return builder.build(); } }
for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadUse/data.txt"))) { elements.add(dataGenerator._apply(line)); .addSchemas(StreamUtil.openStreams(getClass(), "RoadUse/schema")) .storeProperties(StreamUtil.openStream(getClass(), "mockaccumulostore.properties")) .build();
/** * Open the elements schema file located at the same location as the provided * class. * * @param clazz the class determining the location of the elements schema file * @return an {@link InputStream} representing the elements schema file */ public static InputStream elementsSchema(final Class clazz) { return openStream(clazz, ELEMENTS_SCHEMA); }
.addSchemas(StreamUtil.openStreams(getClass(), "properties/boundedTimestampSet/schema")) .storeProperties(StreamUtil.openStream(getClass(), "mockaccumulostore.properties")) .build();
/** * Open the view file located at the same location as the provided class. * * @param clazz the class determining the location of the view file * @return an {@link InputStream} representing the view file */ public static InputStream view(final Class clazz) { return openStream(clazz, VIEW); }
for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUse/data.txt"))) { Iterables.addAll(elements, dataGenerator._apply(line)); .addSchemas(StreamUtil.openStreams(getClass(), "RoadAndRoadUse/schema")) .storeProperties(StreamUtil.openStream(getClass(), "mockaccumulostore.properties")) .build();