How can I store a file data into a kind in google datastore using golang [on hold]
How can I store a file data into a kind in google datastore using golang [on hold]
I have came through one challenge that how can the entities from excel sheet be imported into Google Datastore and store into kind as entities in default namespace.
Is there any way to do like this?
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1 Answer
1
You can export the excel file as a csv, like in this example:
firstname,lastname,city, age
Juan,Rulfo,Mexico,60
Pedro,Perez,Barcelona,25
Manuel,Lopez,Lima,43
Anna,Moncada,Cartagena,35
Read the first line in order to have the property names, and then for every line after the first you add a property to an entity:
package com.victor;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
public class CreateFromCSV {
private String inputFile;
private String kindName;
private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public CreateFromCSV(String inputFile, String kindName) {
this.inputFile = inputFile;
this.kindName = kindName;
}
public void loadFile() {
String line = "";
int i = 0;
// Use try-with-resources to autoclose BufferedReader
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
// This will hold the properties names
String props = null;
while ((line = br.readLine()) != null) {
if (i == 0) {
props = line.split(",s*");
} else {
// And this, the properties values
String values = line.split(",s*");
// At this point you have the property names
// And a row of values, so create and save the entity
createSaveEntities(props, values);
}
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void createSaveEntities(String props, String values) {
Entity e = new Entity(kindName);
for (int i = 0; i < values.length; i++) {
e.setProperty(props[i], values[i]);
}
datastore.put(e);
try {
System.out.println("Entity: " + datastore.get(e.getKey()).toString());
} catch (EntityNotFoundException e1) {
e1.printStackTrace();
}
}
}
and call this class from somewhere else as:
class MyTestClass{
private static final String LOCATION = "/path/to/your/file.csv";
private static final String KINDNAME = "Customer";
public static void main(String args){
public MyTestClass(){
}
CreateFromCSV create = new CreateFromCSV(LOCATION, KINDNAME);
create.loadFile();
}
}
As I told you you should try this yourself. I wish I could help more but Golang is a foreign language to me.
I am using Golang to do these.can you suggest me in GO.
– sriNiha
Jul 5 at 8:39
Sorry but I cannot. You better start looking how to parse csv files with golang and build something based on the examples from the docs
– Victor Herasme Perez
Jul 5 at 11:50
Yes it can be done, but you should show us what you have done so far. Follow this instructions to write good questions: stackoverflow.com/help/how-to-ask
– Victor Herasme Perez
Jul 4 at 14:23