Neo4j - Syntax for Loading CSV with Headers - neo4j

I'm just getting started with Neo4j, and have been trying to create my first project in Neo4j Community with a small sample data from a CSV. I keep getting an invalid input/syntax error (see image below).
The problem could be several places:
I may not have set up my project correctly
I may not have the file in the right place
I may not be using the syntax correctly
Here is the Cypher I've been using to try to load the file:
LOAD CSV WITH HEADERS FROM 'C:\Users\Diana\Documents\Nattosphere\Natto_Sample.csv' AS line
CREATE (n: Natto_Variety{Product_UID: line.Product_UID, Product_Manufacturer: line.Product_Manufacturer, Product_Weight_g: line.Product_Weight_g, Product_Flavoring: line.Product_Flavoring})
I've tried several approaches, and created a simplified file, but am getting the same error each time:
Invalid input 's': expected org$neo4j$cypher$internal$compiler$v2_2$parser$Strings$$HexDigit (line 1, column 33 (offset: 32))
"LOAD CSV WITH HEADERS FROM 'C:\Users\Diana\Documents\Nattosphere\Natto_Sample.csv' AS line"
At the bottom of the GUI, another error reads:
"Neo.ClientError.Statement.InvalidSyntax"
Any idea what might be happening here?
-D

I think you have to do:
LOAD CSV WITH HEADERS FROM 'file:C:/Users/Diana/Documents/Nattosphere/Natto_Sample.csv'
If that doesn't work try:
LOAD CSV WITH HEADERS FROM 'file://C:\Users\Diana\Documents\Nattosphere\Natto_Sample.csv'
See: http://neo4j.com/developer/guide-import-csv/

Frist, you have to set true in setting dbms.security.allow_csv_import_from_file_urls
Second, you must to set where neo4J will search the csv in dbms.directories.import
When you setting those items, you have to copy you csv file into the folder where we set the dbms.directories.import
Later in Cypher:
LOAD CSV FROM 'file:///Natto_Sample.csv'
*if you hace folder into folder... use this character / to route your URL

The Syntaxe to load the csv with headers as described In the CSV Import Guide is :
LOAD CSV WITH HEADERS FROM "file-url" AS line
where the file-url , for local files, is :
file:///data.csv
Put the csv file in the import directory,and that should works

You don't need to write the C: reference:
LOAD CSV WITH HEADERS FROM 'file:/Users/Diana/Documents/Nattosphere/Natto_Sample.csv'

Related

Neo4j LOAD CSV Error: unknown protocol: c

LOAD CSV FROM "file:/C:/Users/abcd/Desktop/Neo4J/fileName.csv" AS row
WITH row
RETURN row
This is my code for importing this csv to my database
but it is giving me error as
Neo.ClientError.Statement.ExternalResourceFailed: Invalid URL
'C:/Users/abcd/Desktop/Neo4J/fileName.csv': unknown protocol: c
can anyone help me solve this
Local CSV files are accessible using file:/// URL.
file:/// URLs identify files on the filesystem of the database server
You need to add file as protocol before the local files address, as follows:
LOAD CSV FROM "file:///C:/Users/abcd/Desktop/Neo4J/fileName.csv" AS row
WITH row
RETURN row
NOTE:
You need to change neo4j.conf file for allowing CSV import from
file URLs.
Uncomment this line(remove #):
#dbms.security.allow_csv_import_from_file_urls=true
Comment this line(Add # in the start):
dbms.directories.import=import
Don't forget to restart Neo4j after these changes.
try below line, use some extra slashes
LOAD CSV FROM "file:///C:/Users/abcd/Desktop/Neo4J/fileName.csv" AS row
WITH row
RETURN row

Neo4j LOAD CSV not working when I use link from github

I have this piece of code which loads a csv from another site.
LOAD CSV FROM 'https://www.quackit.com/neo4j/tutorial/genres.csv' AS line
CREATE (:Genre { GenreId: line[0], Name: line[1]})
But when I upload the same csv to my github account and try it, it gives me an error.
LOAD CSV FROM 'https://www.quackit.com/neo4j/tutorial/genres.csv' AS line
CREATE (:Genre { GenreId: line[0], Name: line[1]})[![enter image description here][2]][2]
I only changed the link and nothing else. How do I resolve this?
You need to use the RAW version of the file.
On your github repo, click on your genres.csv file and then click on RAW
Then copy the url and use it in your LOAD CSV command :
https://raw.githubusercontent.com/JP-Reddy/Recommendation-Engine/master/genres.csv

PHPEXCEL weird characters on form inputs

I need some help with PHPEXCEL library, everything works great, I'm successfully extracting my SQL query to excel5 file, I need to give this file to transport company in order to auto collect informations about packages, unfotunately the generated excel file has some ascii characters between each letter of the cell text, and when the excel file is imported you need to manually delete these charaters.
If I open the excel file, everything is fine I see: COMPANY NAME, If I open the excel file with notepad++, I see the cell values this way: C(NUL)O(NUL)M(NUL)P(NUL)A(NUL)N(NUL)Y N(NUL)A(NUL)M(NUL)E
If I open again the file with excel and save, then reopen with notepad++ I see COMPANY NAME.
So I do not understan why every time I create an excel file using PHPEXCEL my every letter of all words are filled with (nul) every letter.
So how do I prevent the generated excel file to include (nul) between every word????
Also if you open the original excel file generated from PHPExcel samples are also filled with (nul) and if you open and save it, the (nul) is gone.
Any help would be appreciated, thanks.
what is the (nul) ??? 0x00??? char(0)???
ok, here is the example:
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('Disponibile solo su browser');
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Solidus")
->setLastModifiedBy("Solidus")
->setTitle("Import web")
->setSubject("Import File")
->setDescription("n.a")
->setKeywords("n.a")
->setCategory("n.a");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueExplicit("A1", "COMPANY")
->setCellValue('A2', 'SAMSUNG');
$objPHPExcel->getActiveSheet()->setTitle('DDT');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="TEST.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Cache-Control: private',false);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');
As you can see from this little example, this scripts creates a file excel5 with 2 cells, A1 = COMPANY, A2 = SAMSUNG
when I send this file to the transport company, they import the file into their system, but as you can see from the picture, there is an weird character between each letter.
so I noticed every time I open the generated Excel5 with notepad++ file I get:
S(nul)A(nul)M(nul)S(nul)U(nul)N(nul)G
If I save the save with excel and then open it again with notepad++ I get:
SAMSUNG
and this file is ok for the transport company
so my question is, how should I avoid the file generated to contain thi '(nul) charachter between each letter????
some help?
weird characters
SAMSUNG
I found the soluion by myself, I explain just in case anyone has also this problem:
there is not way to change the way the excelfile is encoded by PHPEXCEL
so I figured out the problem was reading the file, I did some simulations and reproduce the problem, every time a read the file and put the result into inputs a get weird characters:
C�O�M�P�A�N�Y�
If I set the output enconding enconding as follows:
$excel->setOutputEncoding('UTF-8');
the file loads fine, so the problem was not creating the excel file, but reading the excel file.
If I print the variable with ECHO I get: "COMPANY",
if I put the variable on input as value I get: "C�O�M�P�A�N�Y�"
setting the output solves the problem, but I would like to know why the difference when I put the variable on input as value, thanks

Neo4j Importing local CSV File

I'm trying to import a local csv file but I have got InvalidSyntax Error.
LOAD CSV WITH HEADERS FROM file:C:/csv/user.csv
Invalid input '/' (line 1, column 35 (offset: 34))
"LOAD CSV WITH HEADERS FROM file:C:/csv/user.csv"
You need to put the filename in quotes, and add a few more slashes:
LOAD CSV WITH HEADERS FROM "file:///C:/csv/user.csv"
Full documentation here.
The command below will return the first 5 lines of your CSV file:
LOAD CSV WITH HEADERS FROM "file:///<PATH_TO_YOUR_CSV_FILE>" AS line WITH line RETURN line LIMIT 5;
But you'll have to follow some steps to align with Neo4J security restrictions.
1) Find the conf folder in the neo4j server folder.
Open the neo4j.conf with a text editor.
2) Uncomment the line containing:
#dbms.security.allow_csv_import_from_file_urls=true
To uncomment it, just remove #. It should be like this:
dbms.security.allow_csv_import_from_file_urls=true
3) Comment this line below:
dbms.directories.import=import
To comment it, add #. It should be like this:
#dbms.directories.import=import
Further on importing from CSV in neo4j documentation here: https://neo4j.com/blog/importing-data-neo4j-via-csv/
LOAD CSV WITH HEADERS FROM "file:C:/path/location/filename.csv" AS row
Found that these query asks Neo4j to look in a specific location
C:\Users\*******\.Neo4jDesktop\neo4jDatabases\database-2b9d81ff-1976-427e-ba98-4f3191c3ef62\installation-3.4.9\import
placing your csv here and using the query
LOAD CSV WITH HEADERS FROM "file:///testData2.csv" AS line
solved the issue for me
or you can change the settings by making changes here
dbms.directories.import=import
NB: I am using windows 10 , neo4j-desktop-offline-1.1.12
I had the same problem (in Windows 10) and I realized that I was just trying to load the CSV file without saying to it to return something.
For me it worked pretty well like this:
LOAD CSV WITH HEADERS FROM "file:///C:all_data.csv" AS line
RETURN line
Note: Do not forget to place the CSV file that you want to import on the neo4j import file!

Neo4J Load CSV -> URI is not hierarchical

I try to import CSV in a Neo4j Database and I have a problem.
On my desktop computer (windows 7, java 1.8.0_40-b25), the LOAD CSV works great.
But on the server (windows 2012 R2, java 1.8.0_65-b17), i have this error message "URI is not hierarchical".
I try to put the data on C:, F: ... no change.
Here's the code :
USING PERIODIC COMMIT 100
LOAD CSV WITH HEADERS FROM
"file:F:/Neo4JData/Destination.csv"
AS line
MERGE (d:Destination {`Code`: line.`Code`});
Thanks for your help.
Are you using 2.3.0 Community Edition?
try:
USING PERIODIC COMMIT 10000 LOAD CSV FROM
'file:///F:\\Neo4JData\\Destination.csv
Create an import folder in the default path of the DB and place the file there that helped me.
For example: C:\Users\XXXXY\Documents\Neo4j\default.graphdb\import and put the csv there. In the query use
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///customers.csv" AS row
CREATE (:Customer {companyName: row.CompanyName, customerID: row.CustomerID, fax: row.Fax, phone: row.Phone});
I had the same problem. I solved it by putting /// instead of F:/ or F:///.
So if your source is
F:/FolderOne/FolderTwo/file.csv
It becomes
///FolderOne/FolderTwo/file.csv
Remember that in order to add the file you must put file: in front of the source.
So finally
file:///FolderOne/FolderTwo/file.csv
As specified above once you try with
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///E:/AdventureWorks/adventureworks-neo4j/data/products.csv" as row
CREATE (:Product {productName: row.ProductName, productNumber: row.ProductNumber, productId: row.ProductID, modelName: row.ProductModelName, standardCost: row.StandardCost, listPrice: row.ListPrice});
The "URI is not hierarchical" error disappears. Then most probably you will get an error saying that it couldnt load the resource like
TransientError.Statement.ExternalResourceFailure
In order to solve the same you should find the neo4j.conf file.
Since i'm using a windows 10 machine and community edition of neo4j i could find the same in the below path.
C:\Users\{username}\AppData\Roaming\Neo4j Community Edition
Edit the conf file and comment out the line
dbms.directories.import=import
Doing the above steps enabled me to load the csv file.
The file path seems wrong, can you try with :
"file:F:///Neo4JData/Destination.csv"
Try with:
file:///F:/Neo4JData/Destination.csv
Putting the .csv into the $NEO4JHOME/default.graphdb/import directory worked. You may have to create the folder, or maybe uncommenting the dbms.directories.import=import line in conf might do it. I dunno, did it the hard way :)
However, I found that I still had to include the drive specifier, i.e., file:///c:\csv2import.csv even if it wasn't in the root but in the import directorry
Use the path 'file:///F:/Neo4JData/Destination.csv' and add the Destination.csv file to the neo4jDB\import directory.If the import directory is not there create a new directory named as import and add the file.
Create a folder import under default.graphdb and place your csv files there. Later, you can use file:///fileName.csv in your LOAD CSV query
I was getting this problem as well so what I did was to copy the folder in which the .csv file was present to the following location:
C:\Users\Username\AppData\Roaming\Neo4j Desktop\Application\neo4jDatabases\database-27badd10-8989-482d-871b-cad746091f07\installation-3.3.3\import\
it seems when neo4j is installed and we try to import data from files by giving "file:///c://........" then neo4j starts looking for that file in the location C:\Users\Username\AppData\Roaming\Neo4j Desktop\Application\neo4jDatabases\database-27badd10-8989-482d-871b-cad746091f07\installation-3.3.3\import\
i.e the import folder. So we have to copy all the files needed to be imported by this this type of statement in that import folder.
This one worked at my end in windows
LOAD CSV FROM 'File:///order-details.csv' AS row
RETURN count(row);

Resources