12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package main
- import (
- "database/sql"
- "fmt"
- "github.com/neo4j/neo4j-go-driver/v4/neo4j"
- )
- func main() {
- // 连接到ClickHouse数据库
- clickhouseDSN := "tcp://your_clickhouse_host:your_clickhouse_port?database=your_clickhouse_database"
- clickhouseConn, err := sql.Open("clickhouse", clickhouseDSN)
- if err != nil {
- panic(err)
- }
- defer clickhouseConn.Close()
- // 查询Map数据
- query := "SELECT your_map_column FROM your_table_name"
- rows, err := clickhouseConn.Query(query)
- if err != nil {
- panic(err)
- }
- defer rows.Close()
- // 连接到Neo4j数据库
- neo4jDSN := "bolt://your_neo4j_host:your_neo4j_port"
- neo4jConn, err := neo4j.NewDriver(neo4jDSN, neo4j.BasicAuth("neo4j_username", "neo4j_password", ""))
- if err != nil {
- panic(err)
- }
- defer neo4jConn.Close()
- neo4jSession, err := neo4jConn.Session(neo4j.AccessModeWrite)
- if err != nil {
- panic(err)
- }
- defer neo4jSession.Close()
- // 解析并导入数据到Neo4j
- for rows.Next() {
- var mapData map[string]interface{}
- err := rows.Scan(&mapData)
- if err != nil {
- panic(err)
- }
- // 处理解析后的Map数据,并转换为Neo4j节点和关系
- nodeProps := make(map[string]interface{})
- nodeProps["property_name"] = mapData["property_name"]
- node, err := neo4jSession.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
- result, err := tx.Run(
- "CREATE (n:YourLabel) SET n = $props RETURN n",
- map[string]interface{}{"props": nodeProps},
- )
- if err != nil {
- return nil, err
- }
- record, err := result.Single()
- if err != nil {
- return nil, err
- }
- return record.GetByIndex(0), nil
- })
- if err != nil {
- panic(err)
- }
- fmt.Println(node)
- }
- if err := rows.Err(); err != nil {
- panic(err)
- }
- }
|