【问题标题】:Trying to insert Json into Neo4j试图将 Json 插入 Neo4j
【发布时间】:2020-03-10 17:48:59
【问题描述】:

我是 neo4j 的新手,我正在尝试将 Json 输入 Neo4j,但我得到的是 Match 语句而不是 create。早些时候我自己尝试了一些东西,当我只插入 Json 消息时 {“名称”:“约翰”,“部门”:“科学”} 它没有出现故障,但每次我想尝试添加数字数据时都会出错。

2020-03-10 13:21:59.793  INFO 94817 --- [ntainer#0-0-C-1] o.n.o.drivers.http.request.HttpRequest : Thread: 
29, url: http://localhost:7474/db/data/transaction/92, request: {"statements":[{"statement":"UNWIND {rows} 
as row **MATCH** (n) WHERE ID(n)=row.nodeId SET n:`UsersInfo` SET n += row.props RETURN row.nodeId as ref,
ID(n) as id, {type} as type","parameters":{"type":"node","rows":[{"nodeId":23,"props":{"name":"raj",
"dept":"science","age":11}}]},"resultDataContents":["row"],"includeStats":false}]}

这些是我的课程 Kafka配置

@EnableKafka
@Configuration
public class KafkaConfiguration {

    @Bean
    public ConsumerFactory<String, Users> userConsumerFactory(){
        Map<String, Object> config = new HashMap<>();
         config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
         config.put(ConsumerConfig.GROUP_ID_CONFIG, "group_json");
         config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
         config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
         return new DefaultKafkaConsumerFactory<>(config, new StringDeserializer(),
                    new JsonDeserializer<>(Users.class));
        }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Users> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Users> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(userConsumerFactory());
        return factory;
    }
}

Kafka消费类

Service
public class KafkaConsumer {

    @Autowired
    public Neo4jservice neo4jService;

    @KafkaListener(topics = "UsersJson", groupId = "group_id", containerFactory = "kafkaListenerContainerFactory")
    public void consume(Users users) {
        System.out.println("Consumed message: " + users);
        UsersInfo usern = new UsersInfo();
        usern.setAge(users.getAge());
        usern.setDept(users.getDept());
        usern.setId(users.getId());
        usern.setName(users.getName());
        neo4jService.saveIntoStudentsTable(usern);
    }
}

Neo4j服务

@Service
public class Neo4jservice {

    @Autowired
    private UsersRepo userRepo;

    public UsersInfo saveIntoStudentsTable(UsersInfo users) {
        UsersInfo usern = userRepo.save(users);
        return (usern);
    }   

}

用户回购

@Repository
public interface UsersRepo extends Neo4jRepository<UsersInfo, Long>{

}

用户类

public class Users {

    private Long id;
    private String name;
    private String dept;
    private Integer age;

    **getters,setters and toString method here**
}

同样的 UsersInfo 类

@NodeEntity
public class Users {

    @Id
    private Long id;
    private String name;
    private String dept;
    private Integer age;

    **getters,setters and toString method here**
}

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 输入Json是什么意思?通常,您使用 cypher create 或 merge 语句或 LOAD CSV 或 Json 加载数据。

标签: spring spring-boot neo4j spring-kafka spring-data-neo4j


【解决方案1】:

您还设置了User 类的id 值。 这将使 Spring Data Neo4j 和用于持久性的 Neo4j Object Graph Mapper 认为实体已经存在。 在这种情况下,它将在现有的 id(n)MATCH 并更新您在日志中看到的属性,而不是 CREATE 一个新节点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多