Selection of a database
We want to implement an intelligent question and answer system, so the keyword is uncertain, so the query is more focused on the data connectivity. The normal tabular database does not provide the proper performance to traverse large amounts of data, whether it is a traversal or a retrieval, which is more difficult. As a graph database, neo4j can provide storage for more connection data. It stores each profile data internally as a node, the nodes it connects to adjacent nodes, and they are interconnected through relationships, so that retrieval or traversal is very easy and faster.
So we use the NEO4J database, compared to the following he has a bit:
- It is easy to represent connected data
- Retrieving/traversing/navigating more connection data is very easy and fast
- It is very easy to represent semi-structured data
- neo4j cql Query Language command is a human readable format, very easy to learn
- It uses a simple and powerful data model
- It does not require a complex connection to retrieve the connected/related data because it is easy to retrieve its neighboring nodes or the relationship details are not connected or indexed
Two Building neo4j Database
1.MYSQL database structure
Because what we're going to do is a smart quiz system for the movie, so enough data is the basis to ensure the success of the question and answer, we can't import the data one by one, so we found a MySQL database on the internet about the movie. The structure of the database includes the following:
(1). genre table (movie type table)
(2). Movie Table (Film information table)
(3). Person Table (actor information table)
(4). Movie_to_genre table (Movie and category correspondence)
(5): Person_to_movie (the right relationship between the movie and the actor)
The above is the MySQL data type and some of the data, we have to export the MySQL data in CSV format can be used in the NEO4J database.
2. Export the CSV file
Because the default directory for MySQL export data is: Installation path \uploads\, so when we export the CSV, we must specify the export file name in this directory, or you will be prompted insufficient permissions.
The scripting language is as follows:
1 Use movie;2 3 #CMD命令 view MySQL import and export directory "No Permissions for other directories"4# using Mysql-u root-P Connect MySQL5# Show variables like'%secure%'6#+--------------------------+------------------------------------------------+7#| variable_name | Value |8#+--------------------------+------------------------------------------------+9#| Require_secure_transport | OFF |Ten#| Secure_auth | On | One#| Secure_file_priv | C:\ProgramData\MySQL\MySQL Server5.7\uploads\ |genregenre A#+--------------------------+------------------------------------------------+ -#3Rowsinch Set,1Warning (0.00sec) - the #MySql导出csv数据 with a table head - - - #导出电影的类型 +SELECT * into OUTFILE'c:/programdata/mysql/mysql Server 5.7/uploads/genre.csv' -Fields TERMINATED by',' +From (Select 'GID','Gname'UnionSelect* fromgenre) Genre_; A at - -#导出电影的信息 = =if too many can only export the first 500, plus limit -SELECT * into OUTFILE'c:/programdata/mysql/mysql Server 5.7/uploads/movie.csv' -Fields TERMINATED by',' -Optionally enclosed by'"' inLINES TERMINATED by'\ r'#电影描述中出现 \ r newline character, -From (Select 'Mid','title','Introduction','rating','ReleaseDate'UnionSelect* frommovie) Movie_; to + - the#导出演员person的信息 = =If you have Chinese name , if you do not take English name *SELECT * into OUTFILE'c:/programdata/mysql/mysql Server 5.7/uploads/person.csv' $Fields TERMINATED by',' Panax NotoginsengOptionally enclosed by'"' -From (Select 'PID','Birth','Death','name','biography','Birthplace'Union the SelectPerson_id,person_birth_day,person_death_day, CaseWhen Person_name is NULLThen Person_english_nameElsePerson_name End + asName,person_biography,person_birth_place fromPerson ) Person_; A the #导出电影ID和电影类别之间的对应 "1 to 1" +SELECT * into OUTFILE'c:/programdata/mysql/mysql Server 5.7/uploads/movie_to_genre.csv' -Fields TERMINATED by',' $Optionally enclosed by'"' $From (Select 'Mid','GID'UnionSelect* frommovie_to_genre) movie_to_genre_; - - the #导出演员ID和电影ID之间的对应 "1-to-many" -SELECT * into OUTFILE'c:/programdata/mysql/mysql Server 5.7/uploads/person_to_movie.csv'WuyiFields TERMINATED by',' theOptionally enclosed by'"' -From (Select 'PID','Mid'UnionSelect* fromPerson_to_movie) Person_to_movie_; Wu - About#解决导出csv中文乱码问题: Open the CSV with TXT, save As, select UTF8 encoding to save the overlay
After exporting, the following export files are available in the upload directory:
Note: If garbled in the CSV file, open it as Notepad and change the encoding to "UTF-8"when saving, overwriting the source file.
Three Import neo4j Database
1. Neo4j Import Path
In the NEO4J installation directory, there is an import folder (you do not have to create a new folder), the CSV file you just exported to this folder.
2. NEO4J Import Data
After opening the neo4j service, go to the service page http://localhost:7474/browser/
Import the database by entering the following command in the input command line.
1Locate the neo4j installation path, and in the D:\neo4j-community-3.4.0\ Directory to create the import directory2The full path is as follows D:\neo4j-community-3.4.0\import3 because neo4j supports importing CSV files, the default directory entry is ... \import4 5 6 //import node Movie type = = Note type conversion7LOAD CSV with HEADERS from"File:///genre.csv" as Line8 MERGE (P:genre{gid:tointeger (Line.gid), name:line.gname})9 Ten One //Import node actor information ALOAD CSV with HEADERS from'File:///person.csv' as Line - MERGE (P:person {pid:tointeger (line.pid), Birth:line.birth, - Death:line.death,name:line.name, the Biography:line.biography, - Birthplace:line.birthplace}) - - + //Import node Movie information -LOAD CSV with HEADERS from"File:///movie.csv" as Line + MERGE (P:movie{mid:tointeger (Line.mid), Title:line.title,introduction:line.introduction, A rating:tofloat (line.rating), releasedate:line.releasedate}) at - - //Import Relationship Actedin movie who was in the 1-to-many -LOAD CSV with HEADERS from"File:///person_to_movie.csv" as Line -Match ( from:P Erson{pid:tointeger (Line.pid)}), (To:movie{mid:tointeger (Line.mid)}) -Merge ( from)-[r:actedin{pid:tointeger (Line.pid), Mid:tointeger (Line.mid)}]-> (to) in - //import relationship What kind of movie is the type = = 1 to many toLOAD CSV with HEADERS from"File:///movie_to_genre.csv" as Line +Match ( from: Movie{mid:tointeger (Line.mid)}), (To:genre{gid:tointeger (Line.gid)}) -Merge ( from)-[r: is{Mid:tointeger (Line.mid), Gid:tointeger (Line.gid)}] -> (to)
Detection query:
1 // Q: What movies did Zhang Ziyi play? 2where n.name=' Zhang Ziyi 'return3/ / Delete all nodes and relationships 4 MATCH (n)-[r]-(b)5 Delete n,r,b
Since CSV import neo4j data is a string data type, for some fields that have special requirements, we need to do the type conversion at the time of import. The following, etc.:
After importing, the NEO4J structure is as follows:
The associated records can be queried as follows:
1 $ match (n)-[r:isreturn
Above, we completed the creation of neo4j database in the motion picture question and answer system.
! Part of the article refers to "80332911"
Movie Smart Quiz--neo4j Database creation