|
| 1 | +/* Copyright (c) 2020 vesoft inc. All rights reserved. |
| 2 | + * |
| 3 | + * This source code is licensed under Apache 2.0 License. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.vesoft.nebula.examples; |
| 7 | + |
| 8 | +import com.vesoft.nebula.client.storage.StorageClient; |
| 9 | +import com.vesoft.nebula.client.storage.data.EdgeTableRow; |
| 10 | +import com.vesoft.nebula.client.storage.data.VertexRow; |
| 11 | +import com.vesoft.nebula.client.storage.data.VertexTableRow; |
| 12 | +import com.vesoft.nebula.client.storage.scan.ScanEdgeResult; |
| 13 | +import com.vesoft.nebula.client.storage.scan.ScanEdgeResultIterator; |
| 14 | +import com.vesoft.nebula.client.storage.scan.ScanVertexResult; |
| 15 | +import com.vesoft.nebula.client.storage.scan.ScanVertexResultIterator; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +public class SpecialAddressStorageClientExample { |
| 24 | + private static final Logger LOGGER = |
| 25 | + LoggerFactory.getLogger(SpecialAddressStorageClientExample.class); |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + Map<String,String> storageAddressMapping = new HashMap<>(); |
| 29 | + storageAddressMapping.put("127.0.0.1:9559","10.xx.xx.xx:9559"); |
| 30 | + // input params are the metad's ip and port |
| 31 | + StorageClient client = new StorageClient("127.0.0.1", 9559); |
| 32 | + // set storage address mapping |
| 33 | + client.setStorageAddressMapping(storageAddressMapping); |
| 34 | + try { |
| 35 | + client.connect(); |
| 36 | + } catch (Exception e) { |
| 37 | + LOGGER.error("storage client connect error, ", e); |
| 38 | + client.close(); |
| 39 | + System.exit(1); |
| 40 | + } |
| 41 | + scanVertex(client); |
| 42 | + scanEdge(client); |
| 43 | + |
| 44 | + client.close(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Vertex Person's property in Nebula Graph: |
| 49 | + * first_name, last_name, gender, birthday |
| 50 | + * Tom Li 男 2010 |
| 51 | + */ |
| 52 | + public static void scanVertex(StorageClient client) { |
| 53 | + ScanVertexResultIterator iterator = client.scanVertex( |
| 54 | + "test", |
| 55 | + "person", |
| 56 | + Arrays.asList("name", "age")); |
| 57 | + |
| 58 | + while (iterator.hasNext()) { |
| 59 | + ScanVertexResult result = null; |
| 60 | + try { |
| 61 | + result = iterator.next(); |
| 62 | + } catch (Exception e) { |
| 63 | + LOGGER.error("scan error, ", e); |
| 64 | + client.close(); |
| 65 | + System.exit(1); |
| 66 | + } |
| 67 | + if (result.isEmpty()) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + List<VertexRow> vertexRows = result.getVertices(); |
| 72 | + for (VertexRow row : vertexRows) { |
| 73 | + if (result.getVertex(row.getVid()) != null) { |
| 74 | + System.out.println(result.getVertex(row.getVid())); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + System.out.println("\nresult vertex table view:"); |
| 79 | + System.out.println(result.getPropNames()); |
| 80 | + List<VertexTableRow> vertexTableRows = result.getVertexTableRows(); |
| 81 | + for (VertexTableRow vertex : vertexTableRows) { |
| 82 | + System.out.println(vertex.getValues()); |
| 83 | + } |
| 84 | + System.out.println("\n"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Edge Friend's property in Nebula Graph: |
| 90 | + * degree |
| 91 | + * 1.0 |
| 92 | + */ |
| 93 | + public static void scanEdge(StorageClient client) { |
| 94 | + ScanEdgeResultIterator iterator = client.scanEdge( |
| 95 | + "test", |
| 96 | + "like", |
| 97 | + Arrays.asList("likeness")); |
| 98 | + |
| 99 | + while (iterator.hasNext()) { |
| 100 | + ScanEdgeResult result = null; |
| 101 | + try { |
| 102 | + result = iterator.next(); |
| 103 | + } catch (Exception e) { |
| 104 | + LOGGER.error("scan error, ", e); |
| 105 | + client.close(); |
| 106 | + System.exit(1); |
| 107 | + } |
| 108 | + if (result.isEmpty()) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + System.out.println(result.getEdges()); |
| 113 | + |
| 114 | + System.out.println("\nresult edge table view:"); |
| 115 | + System.out.println(result.getPropNames()); |
| 116 | + List<EdgeTableRow> edgeTableRows = result.getEdgeTableRows(); |
| 117 | + for (EdgeTableRow edge : edgeTableRows) { |
| 118 | + System.out.println(edge.getValues()); |
| 119 | + } |
| 120 | + System.out.println("\n"); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments