main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/neo4j/neo4j-go-driver/v4/neo4j"
  6. )
  7. func main() {
  8. // 连接到ClickHouse数据库
  9. clickhouseDSN := "tcp://your_clickhouse_host:your_clickhouse_port?database=your_clickhouse_database"
  10. clickhouseConn, err := sql.Open("clickhouse", clickhouseDSN)
  11. if err != nil {
  12. panic(err)
  13. }
  14. defer clickhouseConn.Close()
  15. // 查询Map数据
  16. query := "SELECT your_map_column FROM your_table_name"
  17. rows, err := clickhouseConn.Query(query)
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer rows.Close()
  22. // 连接到Neo4j数据库
  23. neo4jDSN := "bolt://your_neo4j_host:your_neo4j_port"
  24. neo4jConn, err := neo4j.NewDriver(neo4jDSN, neo4j.BasicAuth("neo4j_username", "neo4j_password", ""))
  25. if err != nil {
  26. panic(err)
  27. }
  28. defer neo4jConn.Close()
  29. neo4jSession, err := neo4jConn.Session(neo4j.AccessModeWrite)
  30. if err != nil {
  31. panic(err)
  32. }
  33. defer neo4jSession.Close()
  34. // 解析并导入数据到Neo4j
  35. for rows.Next() {
  36. var mapData map[string]interface{}
  37. err := rows.Scan(&mapData)
  38. if err != nil {
  39. panic(err)
  40. }
  41. // 处理解析后的Map数据,并转换为Neo4j节点和关系
  42. nodeProps := make(map[string]interface{})
  43. nodeProps["property_name"] = mapData["property_name"]
  44. node, err := neo4jSession.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
  45. result, err := tx.Run(
  46. "CREATE (n:YourLabel) SET n = $props RETURN n",
  47. map[string]interface{}{"props": nodeProps},
  48. )
  49. if err != nil {
  50. return nil, err
  51. }
  52. record, err := result.Single()
  53. if err != nil {
  54. return nil, err
  55. }
  56. return record.GetByIndex(0), nil
  57. })
  58. if err != nil {
  59. panic(err)
  60. }
  61. fmt.Println(node)
  62. }
  63. if err := rows.Err(); err != nil {
  64. panic(err)
  65. }
  66. }