Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
Ways to persist Guava Graph

I'm using the common.graph from Google Guava in Version 21.0. It suits very well to my usecase without one aspect: Persistence. The graph seems to be in-memory only. The graph-classes does not implement Serializable, it was explained in this issue posts.

Google describes three models to store the topology. The third option is:

a separate data repository (for example, a database) stores the topology

But that's all. I didn't found any methods in the package to apply a separate data repository. Is there any way to do this? Or is the only way to use the nodes()and edges() method to get a Set of my nodes and a Set of my edges? I can persist them in a database if I implement Serializable in this classes and restore the graph by calling addNode(Node) and addEdge(Source, Target, Edge) (there are no addAll-methods). But this seems to be a workaround.

Thanks for your support!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

To briefly recap the reason why Guava's common.graph classes aren't Serializable: Java serialization is fragile because it depends on the details of the implementation, and that can change at any time, so we don't support it for the graph types.

In the short term, your proposed workaround is probably your best bet, although you'll need to be careful to store the endpoints (source and target) of the edges alongside the edge objects so that you'll be able to rebuild the graph as you describe. And in fact this may work for you in the longer term, too, if you've got a database that you're happy with and you don't need to worry about interoperation with anyone else.

As I mentioned in that GitHub issue, another option is to persist your graph to some kind of file format. (Guava itself does not provide a mechanism for doing this, but JUNG will for common.graph graphs once I can get 3.0 out the door, which I'm still working on.) Note that most graph file formats (at least the ones I'm familiar with) have fairly limited support for storing node and edge metadata, so you might want your own file format (say, something based on protocol buffers).

over 4 years ago · Santiago Trujillo Relatório

0

One way I found of storing the graph was through the DOT format, like so:

public class DOTWriter<INode, IEdge> {

    public static String write(final Graph graph) {
        StringBuilder sb = new StringBuilder();
        sb.append("strict digraph G {\n");

        for(INode n : graph.nodes()) {
            sb.append("  \"" + n.getId() + "\n");
        }

        for(IEdge e : graph.edges()) {
            sb.append("  \"" + e.getSource().getId() + "\" -> \"" + e.getTarget().getId() + "\" " + "\n");
        }

        sb.append("}");
        return sb.toString();
    }
}

This will produce something like

strict digraph G {
    node_A;
    node_B;
    node_A -> node_B;
}

It's very easy to read this and build the graph in memory again.

If your nodes are complex objects you should store them separately though.

over 4 years ago · Santiago Trujillo Relatório

0

Based on @Maria Ines Parnisari's amazing answer, I modified a little😊. Then drawing with mermaid(a markdown plugin), I get a clear image like this in Idea(>=2021.2, support markdown better)!

code

        //noinspection UnstableApiUsage
        MutableGraph<String> graph = GraphBuilder.directed()
                .allowsSelfLoops(false)
                .build();

        //noinspection UnstableApiUsage
        graph.addNode("root");
        graph.putEdge("root", "s1_1");
        graph.putEdge("root", "s1_2");
        graph.putEdge("root", "s1_3");
        graph.putEdge("s1_2", "s2");
        graph.putEdge("s2", "s3");
        graph.putEdge("s3", "s4");
        graph.putEdge("s3", "s5");
        graph.putEdge("s4", "s6");
        graph.putEdge("s5", "s6");
        graph.putEdge("s1_1", "s6");
        graph.putEdge("s1_1", "s2");

        // print mermaid text , then copy it
        StringBuilder sb = new StringBuilder();
        for (EndpointPair<String> edge : graph.edges()) {
            // shoudle be `-->` to draw with mermaid
            sb.append(edge.nodeU() + " --> " + edge.nodeV() + "\n");
        }
        System.out.println(sb);

sorry As < 10 repution, i can't copy image directly that image will be hidden

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda