I have a file in the local system and I want to create a metadata of that file which includes:
{Filename, create date, update date, file format etc etc}
as a key value pair in JSON format. Next, I want to upload it in CKAN,(CKAN API call) so that I can finally see the file using the CKAN url. I already have CKAN host url with CKAN key.
Please can someone help me to write this script in Java? I am new to CKAN and its the first time try to do this but I am not able to. It will be helpful if someone can share sample piece of code to refer. I think it should just be an API call.
1.Below is python script for same work. But I want it to be done in java, please help..
metadata ={}
azure_urls=[]
for blob in urls:
print(blob)
for url in urls[blob]:
print(url)
r = requests.get(url,stream=True)
file_name = url.split(“/”)[-1]
with open(file_name, ‘wb’) as data:
for chunk in r.iter_content(chunk_size = 1024*1024):
if chunk:
data.write(chunk)
block_blob_service.create_blob_from_path(path.join(container,blob),
data.name,
file_name ,
content_settings=ContentSettings(content_type=mimetypes.guess_type(‘./%s’ %url.split(“/”)[-1])[0]))
print(‘uploading file to ‘+’‘+blob+' ‘+’in a ‘+ container)
os.remove(data.name)
download_url = block_blob_service.make_blob_url(path.join(container, blob),data.name)
azure_urls.append(download_url)
metadata[blob]= azure_urls
metadata[‘Title’] = ‘Dbpedia’+‘-’+blob
metadata[‘Publisher’] = ‘Name’
metadata[‘Created’] = datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)
metadata[‘version’] = “2016-10"
metadata[ “Container”] = container
metadata[‘SourceType’] = [url.split(“.”)[-1] for url in azure_urls]
print(metadata)
And below is ckanAPI call statement:
ckan = ckanapi.RemoteCKAN(‘http://hostname’,‘key’ )
3.getting output in url as below:
{‘ontology’: [‘link1', ‘link2’], ‘Title’: ‘Dbpedia-ontology’, ‘Publisher’: ‘SiddarthaP’, ‘Created’: ‘2017-08-03 00:55:22’, ‘version’: ‘2016-10’, ‘Container’: ‘dbpedia’, ‘SourceType’: [‘owl’, ‘nt’]}
答案 0 :(得分:0)
我假设您要做的是以给定格式处理某些信息并将其上传到CKAN。我不确定您在private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void processUrls(String[] blobs, Map<String, String[]> urls) {
CkanClient cc = new CheckedCkanClient("HOSTNAME", "key");
// Process each blob
for (String dsName : blobs) {
System.out.println("DATASET: " + dsName);
// Process all the available URLS of the same blob
List<String> urls = new ArrayList<>();
String publisherName;
String container;
for (String url : urls.get(blob)) {
System.out.println("URL: " + url);
String[] urlFields = url.split("/");
String fileName = urlFields[urlFields.length - 1];
System.out.println(" - Resource " + fileName + " with URL " + url);
urls.add(url);
}
// Construct the JSON metadata
JSONObject metaData = createMetaData(dsName, urls, publisherName, container);
System.out.println("METADATA: ");
System.out.println(metaData.toJSONString());
// Upload to CKAN
ObjectMapper objectMapper = new ObjectMapper();
CkanDataset cd = objectMapper.readValue(metaData, CkanDataset.class);
CkanDataset createdDataset = cc.createDataset(cd);
}
}
private JSONObject createMetaData(String dsName, List<String> urls, String publisherName, String container) {
JSONArray metaDataUrls = new JSONArray();
JSONArray urlTypes = new JSONArray();
for (String url : urls) {
metaDataUrls.put(url);
String[] urlFields = url.split(".");
urlTypes.put(urlFields[urlFields.length - 1]);
}
JSONObject metaData = new JSONObject();
metaData.put(dsName, metaDataUrls);
metaData.put("Title", "Dbpedia-" + dsName);
metaData.put("Publisher", publisherName);
metaData.put("Created", FORMAT.format(new Date()));
metaData.put("version", "2016-10");
metaData.put("Container", container);
metaData.put("SourceType", urlTypes);
return metaData;
}
中提供的代码是否符合您的预期(即,所有blob之间共享的URL,而不是每个blob都累积),并且它也不会上传任何内容。
这就是说,使用JAKAN API v0.4,您可以编写类似的代码:
代码:
x