In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use Java client to load data into Grakn knowledge map". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Migrate data to Grakn
We will outline how migration occurs.
First, we need to talk to our Graknkey space. To do this, we will use Grakn's Java client.
We will iterate through each data file, extract each data item, and parse it into a JSON object.
We pass each data item (in the form of a JSON object) to its corresponding template. The template returns the Graql query used to insert the item into the Grakn.
We will execute each query to load the data into the target key space-phone_calls.
Before continuing, make sure that Java 1.8 is installed and that the Grakn server is running on your computer.
Getting started creating a new Maven project
The project uses SDK 1.8 and is named phone_calls. I will use IntelliJ as the IDE.
Set Grakn to dependency
Modify pom.xml to include the latest version of Grakn (1.4.2) as a dependency.
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0
< groupId >Ai.grakn.examples
< artifactId >Migrate-csv-to-grakn
< version >1.0-SNAPSHOT
< 存储库> < repository > < id >Publish
< url >Https://oss.sonatype.org/content/repositories/releases
< properties > < grakn.version >1.4.2
< maven.compiler.source >1.7
< maven.compiler.target >1.7
< dependencies > < 依赖> < groupId >Ai.grakn
< artifactId >Client-java
< version >${grakn.version} configure logging
We want to be able to configure the content of the Grakn logout. To do this, modify pom.xml to exclude the addition of grakn with slf4j and logback as a dependency.
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0
< dependencies > < 依赖> < groupId >Ai.grakn
< artifactId >Client-java
< version >${grakn.version}
< exclusions > < 排除> < groupId >Org.slf4j
< artifactId >Slf4j-simple
< 依赖> < groupId >Ch.qos.logback
< artifactId >Logback-classic
< version >1.2.3
Next, add a new file called by logback.xml with the following and put it in the following src/main/resources.
< configuration debug = "false" > < root level = "INFO" />Create a migration class
Create a new file called Migration.java in src/main. This is where we write all the code.
Include data files
Select one of the following data formats and download the file. After downloading each of the four files, place them in the src/main/resources/data directory. We will use them to load data into our phone_calls knowledge graph.
All subsequent code will be written to Migration.java.
Specify the details of each data file
Before that, we need a structure to contain the details needed to read the data file and build the Graql query. These details include:
The path to the data file, and
Receives the JSON object and generates a template function for the Graql insert query.
To do this, we created a new subclass called Input.
Import mjson. Jason.
Public classes migrate {abstract static class Input {string path; public Input (String path) {this. Path = path;} String getDataPath () {return path;} abstract String template (Json data);}}
Later in this article, we'll see how Input creates an instance of a class, but before we get started, let's add the mjson dependency to the dependencies tag in the file, pom.xml.
< 依赖> < groupId >Org.sharegov
< artifactId >Mjson
< version >1.4.0
It is time to initialize the inputs.
The following code calls initialiseInputs () to return the method inputs of the collection. We will then load each data file into Grakn using input for each element in this collection.
/ / other imports are imported into java. Util . ArrayList; import java. Util . Collection
Public class migration {abstract static class input {.}
Public static void main (String [] args) {Collection
< Input >Inputs = initialiseInputs ();}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList (); / / next is the return input;}} the input instance of the company
/ / Import
Public class migration {abstract static class input {.} public static void main (String [] args) {.}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList ()
Enter. Add (new Input ("data / companies") {@ overrides public String template (Json company) {returns "insert $company isa company has name" + company. At ("name") + ";";}})
Return input;}}
Input.getDataPath () will be back, data/companies.
Given that company is
{name: "Telecom"}
Input.template (company) will return
Insert the name "Telecom" owned by the $company ISA; one person's input instance / / import
Public class migration {abstract static class input {.} public static void main (String [] args) {.}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList ()
Enter. Add (new Input ("data / companies") {...})
Enter. Add (new Input ("data / people") {@ overrides public String template (Json person) {/ / inserter String graqlInsertQuery = "insert $person isa person has phone-number" + person. At ("phone_number")
If (! People. ("FIRST_NAME") {/ / person is not a customer graqlInsertQuery + = "has is-customer false";} else {/ / person is a customer graqlInsertQuery + = "has is-customer true"; graqlInsertQuery + = "has a name" + person. At ("first_name"); graqlInsertQuery + = "have a last name" + person. At ("last_name"); graqlInsertQuery + = "cities" + people. In ("city"); graqlInsertQuery + = "age" + people. In (age). AsInteger ();}
GraqlInsertQuery + = ";"; return graqlInsertQuery;}})
Return input;}}
Input.getDataPath () will be back, data/people.
Given that person is
{phone_number: "+ 44091 xxx"}
Input.template (person) will return
Insert $person owns phone-number "+ 44091 XXX"
And give person to be
{Abies-name: Jackie Chan, last name: Joe, city: Ink, age: 77 Phoebe number: "+ 0091 XXX"}
Input.template (person) will return
Insert $person owns phone-number "+ 44091 XXX" with first name "Jackie Chan" has passed-name "Joe" has city "Jimo" has age 77; input example of contract / / import
Public class migration {abstract static class input {.} public static void main (String [] args) {.}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList ()
Enter. Add (new Input ("data / companies") {...})
Enter. Add (new Input ("data / people") {...})
Enter. Add (new Input ("data / contracts") {@ overrides public String template (Json contract) {/ / matching company String graqlInsertQuery = "match $company isa company has name" + contract. At ("company_name") + ";"; / / matching person graqlInsertQuery + = "$customer isa person has phone-number" + contract. At ("person_id") + ";"; / / insert contract graqlInsertQuery + = "insert (provider:$ company,customer:$ customer) isa contract;"; return graqlInsertQuery;}})
Return input;}}
Input.getDataPath () will be back, data/contracts.
Given that contract is
{company_name: "Telecom", person_id: "+ 00091 xxx"}
Input.template (contract) will return
Competition $Company ISA owns the name "Telecom"; $customer ISA owns the phone-number "+ 00091 XXX"; insert (provider:$ company,customer:$ customer) isa contract; input instance of the call / / import
Public class migration {abstract static class input {.} public static void main (String [] args) {.}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList ()
Enter. Add (new Input ("data / companies") {...})
Enter. Add (new Input ("data / people") {...})
Enter. Add (new Input ("data / contracts") {...})
Enter. Add (new Input ("data / calls") {@ overrides public String template (Json call) {/ / matches caller String graqlInsertQuery = "match $caller isa person has phone-number" + call. At ("caller_id") + ";"; / / matches the called graqlInsertQuery + = "$callee isa person has phone-number" + call. At ("callee_id") + ";"; / / insert phone graqlInsertQuery + = "insert $call (caller:$ caller,callee:$ callee) isa call;" + "$call has started" + incoming call. At ("started_at"). AsString () + ";" + "$call has duration" + call. In (duration). AsInteger () + ";"; return graqlInsertQuery;}})
Return input;}}
Input.getDataPath () will be back, data/calls.
Given that call is
{caller_id: "44091 XXX", callee_id: "00091 XXX", started_at:2018-08-10 T07V 5751, duration: 148}
Input.template (call) will return
Competition $Caller ISA person owns phone-number "+ 44091 XXX"; $called ISA person owns phone-number "+ 00091 XXX"; insert $call (caller:$ caller,callee:$ callee) isa call; $phone has started-at 2018-08-10 T07 57 XXX 51; $phone has duration 148; connection and migration
Now that we have defined the data path and template for each data file, we can continue to connect to our phone_calls knowledge graph and load the data into it.
/ / other imported ai. Grakn . GraknTxType; imported ai. Grakn . Keyspace; imported ai. Grakn . Customer. Grakn; imported ai. Grakn . Util . SimpleURI; import java. Io . UnsupportedEncodingException
Public class migration {abstract static class input {.}
Public static void main (String [] args) {Collection
< Input >Inputs = initialiseInputs (); connectAndMigrate (input);}
Static void connectAndMigrate (Collection
< Input >Inputs) {SimpleURI localGrakn = new SimpleURI ("localhost", 48555); Keyspace keyspace = Keyspace. Of ("phone_calls"); Grakn grakn = new Grakn (localGrakn); Grakn. Session session = grakn. Session (keyspace)
Enter. ForEach (enter-> {system. Out. The println ("[by loading" + input. GetDataPath () + "] becomes Grakn..."); try {loadDataIntoGrakn (input, session);} catch (UnsupportedEncodingException e) {e. PrintStackTrace () ()
Meeting. Close ();}
Static Collection
< Input >InitialiseInputs () {...}
Static void loadDataIntoGrakn (input input, Grakn. Session session) throws UnsupportedEncodingException {...}}
ConnectAndMigrate (Collection inputs) is the only way to initiate data migration to the phone_calls knowledge graph.
The following occurs in this method:
Grakn creates a Grakn instance and connects to our locally running server, localhost:48555.
Session creates A, which connects to the key space phone_calls.
For each input object inputs in the collection, we call it loadDataIntoGrakn (input, session). This will be responsible for loading the data specified in the input object into our key space.
Finally, session was shut down.
Load data into phone_calls
Now that we have session connected to the phone_ calls key space, we can continue to actually load the data into our knowledge graph.
/ / Import
Public class migration {abstract static class input {.}
Public static void main (String [] args) {Collection
< Input >Inputs = initialiseInputs (); connectAndMigrate (input);}
Static Collection
< Input >InitialiseInputs () {...}
Static void connectAndMigrate (Collection
< Input >Inputs) {.}
Static void loadDataIntoGrakn (input input, Grakn. Session) throws UnsupportedEncodingException {ArrayList
< Json >Items = parseDataToJson (input); item. ForEach (item-> {Grakn. Transaction tx = session. Transaction (GraknTxType. WRITE); String graqlInsertQuery = input. Template (project); system. Out. Println ("execute Graql query:" + graqlInsertQuery); tx. Graql (). Parsing (graqlInsertQuery). Execute (); tx. Commit (); tx. Close ();}); system. Out. Println ("\ nInserted" + project) of. Size () + "entered by project [" +. GetDataPath () + "]. To Grakn\ n");}
Static ArrayList
< Json >ParseDataToJson (input) throws UnsupportedEncodingException {...}}
To load the data from each file into Grakn, we need to:
Retrieve an ArrayListJSON object, each representing a data item. To this end, we call on parseDataToJson (input), and
For each JSON object items:a) create transaction tx,b) construct graqlInsertQuery use the corresponding template,c) run query,d) commit transaction and e) close transaction.
Considerations for creating and committing transactions: to avoid running out of memory, it is recommended that you create and commit each query in a single transaction. However, in order to migrate large datasets faster, each query occurs once, which is the maximum number of queries guaranteed to run on a single transaction.
Now that we have done all of the above, we are ready to read each file and parse each data item into a JSON object. These JSON objects are passed to the methods on each Input object of the template.
We are going to write and implement parseDataToJson (input).
DataFormat specific implementation
The implementation of parseDataToJson (input) varies depending on the format of the data file.
But no matter what the data format is, we need the correct settings to read the file line by line. To do this, we will use InputStreamReader.
/ / other imports are imported into java. Io . InputStreamReader; import java. Io . Readers
Public class migration {abstract static class input {.}
Public static void main (String [] args) {...}
Static void connectAndMigrate (Collection
< Input >Inputs) {.}
Static Collection
< Input >InitialiseInputs () {...}
Static void loadDataIntoGrakn (input input, Grakn. Session session) throw UnsupportedEncodingException {.}
Public static Reader getReader (String relativePath) throws UnsupportedEncodingException {returns a new InputStreamReader (migration. Class. GetClassLoader (). GetResourceAsStream (relativePath), "UTF-8");}} parse CSV
We will use Univocity CSV Parser to parse our .csv file. Let's add a dependency to it. We need to add the following content pom.xml to the dependencies tag.
< 依赖> < groupId >Com.univocity
< artifactId >Univocity-parsers
< version >2.7.6
When you are done, we will write parseDataToJson (input) to parse the implementation of the .csv file.
/ / other imports
Import com. Unfair. Parser. Csv . CsvParser; imported com. Unfair. Parser. Csv . CsvParserSettings
Public class migration {abstract static class input {.}
Public static void main (String [] args) {...}
Static void connectAndMigrate (Collection
< Input >Inputs) {.}
Static Collection
< Input >InitialiseInputs () {...}
Static void loadDataIntoGrakn (input input, Grakn. Session session) throw UnsupportedEncodingException {.}
Static ArrayList
< Json >ParseDataToJson (input) throws UnsupportedEncodingException {ArrayList
< Json >Items = new ArrayList ()
CsvParserSettings settings = new CsvParserSettings (); setting. SetLineSeparatorDetectionEnabled (true); CsvParser parser = new CsvParser (settings); parser. BeginParsing (getReader (input. GetDataPath () + ".csv"))
String [] columns = parser. ParseNext (); String [] row; and ((line = parser). ParseNext ()! = empty) {Json item = Json. Object (); for (interpreting me = 0; I
< 行。长度 ; 我++){ 项目。set(columns [ i ],row [ i ]); } 物品。add(item); } 返回 的项目 ; } public static Reader getReader(String relativePath)throws UnsupportedEncodingException { 返回 新 的InputStreamReader(迁移。类。getClassLoader()。的getResourceAsStream(relativePath),"UTF-8"); }} 除了这个实现,我们还需要进行一次更改。 鉴于CSV文件的性质,生成的JSON对象将把.csv文件的所有列作为其键,即使该值不存在,它也将被视为一个null。 出于这个原因,我们需要在persontemplate的input实例方法中更改一行。 if (! person.has("first_name")) {...} 变 if (person.at("first_name").isNull()) {...}。 阅读JSON 我们将使用Gson的JsonReader来读取我们的.json文件。让我们为它添加依赖项。我们需要在dependencies标签中添加以下内容pom.xml。 < 依赖> < groupId >Com.google.code.gson
< artifactId >Gson
< version >2.7
When you are done, we will write an implementation of parseDataToJson (input) for reading .json files.
/ / other imported com. Google. Gson . Stream. JsonReader
Public class migration {abstract static class input {.}
Public static void main (String [] args) {...}
Static void connectAndMigrate (Collection
< Input >Inputs) {.}
Static Collection
< Input >InitialiseInputs () {...}
Static void loadDataIntoGrakn (input input, Grakn. Session session) throw UnsupportedEncodingException {.}
Static ArrayList
< Json >ParseDataToJson (input) throws IOException {ArrayList
< Json >Items = new ArrayList ()
JsonReader jsonReader = new JsonReader (getReader (input. GetDataPath () + "upload .json"))
JsonReader . BeginArray (); and (jsonReader. HasNext () {jsonReader. BeginObject (); Json item = Json. Object (); and (jsonReader. HasNext () {String key = jsonReader. NextName (); switch (jsonReader. PEEK () {case STRING: project. Set (key, jsonReader. NextString (); break; case number: project. Set (key, jsonReader. NextInt (); break;}} jsonReader. EndObject (); item. Add (item);} jsonReader. EndArray (); returned project;}
Public static Reader getReader (String relativePath) throws UnsupportedEncodingException {returns a new InputStreamReader (migration. Class. GetClassLoader (). GetResourceAsStream (relativePath), "UTF-8");}} parse XML
We will use Java's built-in StAX to parse our .xml file.
To parse the XML data, we need to know the name of the target tag. This needs to be declared in the Input class and specified when constructing each input object.
/ / Import
Public class XmlMigration {abstract static class Input {string path; string selection; public Input (String path,String selector) {this. Path = path; this. Selector = selector;} String getDataPath () {return path;} String getSelector () {return selector;} abstract String template (Json data);}
Public static void main (String [] args) {...}
Static void connectAndMigrate (Collection
< Input >Inputs) {.}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList ()
Enter. Add (new Input ("data / companies", "company") {...}); enter. Add (new input ("data / person", "person") {.}); input. Add (new Input ("data / contracts", "contract") {...}); enter. Add (new Input ("data / calls", "call") {...})
Return input;}
Static void loadDataIntoGrakn (input input, Grakn. Session session) throw UnsupportedEncodingException,XMLStreamException {.}
Public static Reader getReader (String relativePath) throws UnsupportedEncodingException {...}}
It is now used for the implementation of parsing .xml files for parseDataToJson (input).
/ / other imports are imported into javax. Xml . Stream. XMLInputFactory; import javax. Xml . Stream. XMLStreamConstants; import javax. Xml . Stream. XMLStreamException; import javax. Xml . Stream. XMLStreamReader
Public class XmlMigration {abstract static class Input {string path; string selection; public Input (String path,String selector) {this. Path = path; this. Selector = selector;} String getDataPath () {return path;} String getSelector () {return selector;} abstract String template (Json data);}
Public static void main (String [] args) {...}
Static void connectAndMigrate (Collection
< Input >Inputs) {.}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList ()
Enter. Add (new Input ("data / companies", "company") {...}); enter. Add (new input ("data / person", "person") {.}); input. Add (new Input ("data / contracts", "contract") {...}); enter. Add (new Input ("data / calls", "call") {...})
Return input;}
Static void loadDataIntoGrakn (input input, Grakn. Session session) throw UnsupportedEncodingException,XMLStreamException {.}
Static ArrayList
< Json >ParseDataToJson (input) throws UnsupportedEncodingException,XMLStreamException {ArrayList
< Json >Items = new ArrayList ()
XMLStreamReader r = XMLInputFactory. NewInstance (). CreateXMLStreamReader (getReader (input. GetDataPath () + ".xml"); string key; String value = null; Boolean inSelector = false; Json item = null; and ([R. HasNext () {int event = r. Next ()
Switch (event) {case XMLStreamConstants. START_ELEMENT: if. Number getLocalName (). Equal to (input. GetSelector ()) {inSelector = true; item = Json. Object ();} break
Case XMLStreamConstants . Character: value = r. GetText (); break
Case XMLStreamConstants . END_ELEMENT: key = r. GetLocalName (); if (inSelector & &! The key. Equality (input. GetSelector ()) {Project. Set (key,value);} if (key. Equal to (input. GetSelector ()) {inSelector = false; item. Add (item);}
Break;} returned items;}
Public static Reader getReader (String relativePath) throws UnsupportedEncodingException {...}} put it together
Here's what our Migrate.java looks like when we load CSV data into Grakn and find the JSON and XML files here.
Package ai. Grakn . Exampl
Import ai. Grakn . GraknTxType; imported ai. Grakn . Keyspace; imported ai. Grakn . Customer. Grakn; imported ai. Grakn . Util . SimpleURI
/ * * A collection of fast and reliable Java-based parsers for CSV,TSV and fixed-width files * @ see univocity * / imported com. Unfair. Parser. Csv . CsvParser; imported com. Unfair. Parser. Csv . CsvParserSettings
/ * for Java's compact JSON library, * @ see mjson * / Import mjson. Jason.
Import java. Io . InputStreamReader; import java. Io . Readers; import java. Io . UnsupportedEncodingException; import java. Util . ArrayList; import java. Util . Collection
The common class CsvMigration {/ * represents the Input object that links the input file to its template function, and * is used to map the Json object to the Graql query string * / abstract static class Input {string path
Public Input (String path) {this. Path = path;}
String getDataPath () {return path;}
Abstract String template (Json data);}
/ * 1. Create a Grakn instance * 2. Create a session to the target key space * 3. Initialize the input list, each containing the details needed to parse the data * 4. Load csv data into the Grakn * 5 of each file. End meeting * / public static void main (String [] args) {Collection
< Input >Inputs = initialiseInputs (); connectAndMigrate (input);}
Static void connectAndMigrate (Collection
< Input >Inputs) {SimpleURI localGrakn = new SimpleURI ("localhost", 48555); Grakn grakn = new Grakn (localGrakn); Keyspace keyspace = Keyspace. Of ("phone_calls"); Grakn. Session session = grakn. Session (keyspace)
Enter. ForEach (enter-> {system. Out. The println ("[by loading" + input. GetDataPath () + "] becomes Grakn..."); try {loadDataIntoGrakn (input, session);} catch (UnsupportedEncodingException e) {e. PrintStackTrace () ()
Meeting. Close ();}
Static Collection
< Input >InitialiseInputs () {Collection
< Input >Inputs = new ArrayList ()
/ / defines the template input used to build the company Graql insert query. Add (new Input ("data / companies") {@ overrides public String template (Json company) {returns "insert $company isa company has name" + company. At ("name") + ";";}); / / defines the template input used to construct a human Graql insert query. Add (new Input ("data / people") {@ overrides public String template (Json person) {/ / inserter String graqlInsertQuery = "insert $person isa person has phone-number" + person. At ("phone_number")
If (individual. In ("FIRST_NAME"). Refer to isNull () {/ / person is not a customer graqlInsertQuery + = "has is-customer false";} else {/ / person is a customer graqlInsertQuery + = "has is-customer true"; graqlInsertQuery + = "has a name" + person. At ("first_name"); graqlInsertQuery + = "have a last name" + person. At ("last_name"); graqlInsertQuery + = "cities" + people. In ("city"); graqlInsertQuery + = "age" + people. In (age). AsInteger ();}
GraqlInsertQuery + = ";"; return graqlInsertQuery;}); / / defines the template Graql insert query input used to construct the contract. Add (new Input ("data / contracts") {@ overrides public String template (Json contract) {/ / matching company String graqlInsertQuery = "match $company isa company has name" + contract. At ("company_name") + ";"; / / matching person graqlInsertQuery + = "$customer isa person has phone-number" + contract. At ("person_id") + ";"; / insert contract graqlInsertQuery + = "insert (provider:$ company,customer:$ customer) isa contract;"; return graqlInsertQuery;}}); / / defines the template input used to invoke the Graql insert query. Add (new Input ("data / calls") {@ overrides public String template (Json call) {/ / matches caller String graqlInsertQuery = "match $caller isa person has phone-number" + call. At ("caller_id") + ";"; / / matches the called graqlInsertQuery + = "$callee isa person has phone-number" + call. At ("callee_id") + ";"; / / insert phone graqlInsertQuery + = "insert $call (caller:$ caller,callee:$ callee) isa call;" + "$call has started" + incoming call. At ("started_at"). AsString () + ";" + "$call has duration" + call. In (duration). AsInteger () + ";"; return graqlInsertQuery;}}); return input;}
/ * load csv data into our Grakn phone_ calls key space: * 1. Get the data item as a list of json objects * 2. For each json object * one. Create a Grakn transaction * b. Construct the corresponding Graql insert query * C. Run the query * d. Submit deal * e. Closing the transaction * * @ param entering the details necessary to parse the data * @ param session will create a transaction * @ throws UnsupportedEncodingException * / static void loadDataIntoGrakn (input, Grakn. Session) throws UnsupportedEncodingException {ArrayList
< Json >Items = parseDataToJson (input); / / 1 item. ForEach (item-> {Grakn. Transaction tx = session. Transaction (GraknTxType. WRITE); / / 2a String graqlInsertQuery = input. Template (project); / / 2b system. Out. Println ("execute Graql query:" + graqlInsertQuery); tx. Graql (). Parsing (graqlInsertQuery). Execute () / / 2c tx. Commit () / / 2d tx. Close (); / / 2e
}); system. Out. Println ("\ nInserted" + project) of. Size () + "entered by project [" +. GetDataPath () + "]. To Grakn\ n");}
/ * 1. Read csv file * 2 through stream. Parse each line into a json object * 3. Add json objects to the itemlist * * @ param enter the path used to get the data file, minus the format * @ return json object list * @ throws UnsupportedEncodingException * / static ArrayList
< Json >ParseDataToJson (input) throws UnsupportedEncodingException {ArrayList
< Json >Items = new ArrayList ()
CsvParserSettings settings = new CsvParserSettings (); setting. SetLineSeparatorDetectionEnabled (true); CsvParser parser = new CsvParser (settings); parser. BeginParsing (getReader (input. GetDataPath () + ".csv"); / / 1
String [] columns = parser. ParseNext (); String [] row; and ((line = parser). ParseNext ()! = empty) {Json item = Json. Object (); for (interpret me = 0; I < line). Length; I + +) {project. Set (columns [I], row [I]); / / 2} items. Add (item); / / 3} returned project;}
Public static Reader getReader (String relativePath) throws UnsupportedEncodingException {returns the new InputStreamReader (CsvMigration). Class. GetClassLoader (). GetResourceAsStream (relativePath), "UTF-8");}} load time
Run the main method, sit down, relax and watch the logs, while data starts pouring into Grakn.
Look back.
Let's start by setting up the project and locating the data file.
Next, we continue to set up the migration mechanism, which is independent of the data format.
Then we learned how to parse files with different data formats into JSON objects.
Finally, we run the method inputs that triggers the connectAndMigrate method with the given main method. This loads the data into our Grakn knowledge graph.
This is the end of the content of "how to load data into the Grakn knowledge map with Java client". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.