来使用元数据。
call db.schema()
已弃用,您现在可以使用
call db.schema.visualization()
据我所知,没有直接的方法可以获取NEO4J数据库结构的正确图像图。
中有NEO4J浏览器中的预定义查询,可以找到所有节点类型及其关系。但是,它遍历完整的图,并且如果您需要大量数据,可能会因内存错误而失败。
https://github.com/moxious/neoprofiler
您可以使用此查询来放松标签,以动态(通过脚本语言和使用REST API)动态编写该标签,然后将查询粘贴到Neo4J浏览器中以获取数据集。
CALL db.schema.visualization()
有效,但我找不到一种获得节点或关系属性的方法。
CALL apoc.meta.schema()
APOC
您可以通过执行几个Cypher查询来构建自己的图表。这是PowerShell和GraphViz的解决方案:
Invoke-CypherNeo4j
有点长,但是它支持参数和交易,这些参数和交易未使用。
Get-Neo4jGraphvizSchema
2。安装GraphViz它是一个CLI应用程序,它将使用图形的字符串表示形式,并将绘制PDF,PNG,SVG等。
<#
.SYNOPSIS
Execute a Cypher and get a response.
.DESCRIPTION
Sends a given cypher to DB named 'neo4j' through Neo4j HTTP Connector and returns a response in form of Powershell objects.
When given array of Cyphers, they will be executed in a single transaction (doesn't support params).
At first call in current session will ask for username/password for 'neo4j' DB.
.PARAMETER Cypher
Single string with a cypher or array of strings, each is a cypher.
.PARAMETER Params
Hashmap of parameters which will be available in cypher.
.PARAMETER Raw
Return a database response as-is (json string).
.EXAMPLE
Invoke-CypherNeo4j 'RETURN $x + $y' -Params @{x=5; y=4}
# Output: 9
.EXAMPLE
Invoke-CypherNeo4j 'RETURN 2+1 AS sum' -Raw
# Output: {"results":[{"columns":["sum"],"data":[{"row":[3],"meta":[null]}]}],"errors":[],"lastBookmarks":["FB:kcwQCwnhVTLxT1GEJwIi+AH8pyiQ"]}
.EXAMPLE
Invoke-CypherNeo4j 'MATCH (n) DETACH DELETE n;','RETURN 6/0 AS fail;' | ConvertTo-Json -Compress
# Output: {"code":"Neo.ClientError.Statement.ArithmeticError","message":"/ by zero"}
# (nothing will be deleted)
#>
function Invoke-CypherNeo4j {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]$Cypher,
[HashTable]$Params,
[switch]$Raw = $false
)
if ($null -eq $neo4jCred) {$Global:neo4jCred = Get-Credential}
if ($Cypher -is [array]) {
$stmts = $Cypher | % { @{statement=$_;} }
$bodyJson = @{ statements=@($stmts)} | ConvertTo-Json -d 99
} else {
$bodyJson = @{ statements=@( @{statement=$cypher; parameters=$params}) } | ConvertTo-Json -d 99
}
$respJson = Invoke-WebRequest http://localhost:7474/db/neo4j/tx/commit -Method Post -ContentType 'application/json' -Body $bodyJson -Credential $neo4jCred -AllowUnencryptedAuthentication | % Content
$resp = $null
if ($raw) {return $respJson} else {$resp = $respJson | ConvertFrom-Json -d 99}
if ($resp.errors.Count -ne 0) {return $resp.errors}
else {
$results = [collections.arraylist]@()
foreach ($result in $resp.results) {
$objects = $result.data | % {
$obj = [ordered]@{}
for ($i = 0; $i -lt $_.row.count; $i++) {
$key = ([array]($result.columns)).get($i)
$value = $_.row[$i]
$obj[$key] = $value
}
[pscustomobject]$obj
}
$results.add( @($objects) ) | Out-Null
}
if ($results.count -eq 1) {
# Write-host ($results | toJson -d 9)
return ([array]$results).get(0)
} else {
return $results
}
}
}
<#
.SYNOPSIS
Builds a Graphviz representation of database schema.
.DESCRIPTION
Executes multiple cypher queries by calling Invoke-CypherNeo4j function and builds a Graphviz string.
TODO: bold/italic for constraints and indexes
TODO: switch to display relationships as nodes
TODO: what will hapen if some nodes have multiple labels?
#>
function Get-Neo4jGraphvizSchema {
[CmdletBinding()]
param(
[switch]$IncludeCardinality = $false
)
# Get a count for each Label
$cypherCounts = 'MATCH (n) RETURN count(n) AS count, labels(n) AS labels;'
$nodes = Invoke-CypherNeo4j $cypherCounts
$labelToCount = @{}
$nodes | % { $labelToCount[$_.labels[0]] = $_.count }
# Get a count for each combination Label-rel->Label
$cypher = @'
MATCH (from)-[r]->(to)
RETURN DISTINCT labels(from) as from,type(r) as rel, labels(to) as to,count(r) AS count
'@
$rels = Invoke-CypherNeo4j $cypher
# Get node property info
$props = Invoke-CypherNeo4j 'CALL db.schema.nodeTypeProperties'
$map = $props | % {$_.nodeLabels[0],$_.propertyName,$_.propertyTypes[0] -join "`t"} | ConvertFrom-Csv -delim "`t" -hea label,propName,propType | group label -AsHashTable
$nodeDefs = $nodes | % {
$id = $_.labels -join ''
$count = $_.count
$propsDef = $map[$id] | % {"$($_.propName): $($_.propType)\l"} | Join-String
$nodeDef = "$id [label=""$id ($count)|$propsDef""]"
$nodeDef
}
$i = 0
$edgeDefs = $rels | % {
$from = $_.from[0]
$to = $_.to[0]
$relType = $_.rel
$count = $_.count
$headTailLabel = $null
if ($IncludeCardinality) {
$fromCount = $nodes | ? {$_.labels[0] -eq $from} | % count
$toCount = $nodes | ? {$_.labels[0] -eq $to} | % count
$fromCardin = '{0:#0.##}' -f ($count/$fromCount)
$toCardin = '{0:#0.##}' -f ($count/$toCount)
$headTailLabel = "taillabel=$fromCardin headlabel=$toCardin"
}
"$from -> $to [label="":$relType ($count)"" $headTailLabel]"
}
# save query results
$global:neo4jSchemaData = [pscustomobject]@{nodes = $nodes; rels = $rels; props = $props; cardinality=$Cardinality}
Write-Host -foreg green Cypher results have been saved to `$neo4jSchemaData variable
return @"
digraph G {
rankdir=LR
node [shape=Mrecord]
$($nodedefs -join "`n")
$($edgeDefs -join "`n")
}
"@
}
这是用于内置的西北db:
seneme的弦乐表示 贝洛是电影DB功能的实际输出。如果您不想在本地安装GraphViz,则可以将此字符串复制到任何ONLINE graphviz工具
# build and open pdf
Get-Neo4jGraphvizSchema | dot -Tpdf -o schema.gv.pdf; start schema.gv.pdf
# or
# save graphviz string to $gv variable and build a properly scaled png
Get-Neo4jGraphvizSchema -IncludeCardinality -OutVariable gv | dot -Tpng -Gdpi=256 -o schema.gv.png; start schema.gv.png