I have tried by giving path as" hdfs://localhost:9000/path to file" but still its not working is there any other way to read file?
I want to give the file path in program,but not as an argument on terminal...
If you have WebHDFS enabled (dfs.webhdfs.enabled set to true in hdfs-site.xml) then you can access the file with a simple REST call. Many client libraries can open HTTP URI's directly.
For Java, follow the example in Open stream from uri and construct the URI for your file using the examples in the WebHDFS REST API documentation. For example, I was able to open a file from HDFS on my cluster using the following URI: "http://namenode:50070/webhdfs/v1/sampledata/sample.log?op=OPEN"
If what you want is how read the contents of that file using Java code, then have a look at my answer: Programatically reading contents of text file stored in HDFS using Java.
Related
I am crawling files and directory from my local file system and trying to filter-out files based on their company/publisher/vendor name. Let's assume, I want to filter all files that belongs to Microsoft Corporation. I wonder if I could get these information from the java.nio.file or from java.io.File or from MIME_TYPE using APACHE TIKA but I am not finding any quick way to get the company/publisher/vendor information from the files.
I found the link which exactly does what I want but it slow-down the whole process so I am looking a quick way to extract the company/publisher/vendor details.
I have written an Java code that process some data and stores it in .JSON format.
I also have HTML/CSS/Javascript file, that reads that data, and shows it in much nicer way.
So my question is, is it possible to combine Java and HTML code(in one file .jar) so that, for example on a click of a mouse Java opens HTML file in Default browser?
Thanks
Write out the html file to the filesystem (e.g. a temporary directory How to create a temporary directory/folder in Java?) and then open it using the default program: How to open a file with the default associated program
So I am using S3 to store basically audio files, I stream those files with cloudfront. I need to modify the metadata of those files (not the metadata of the s3 object, but the tags of the music), or convert those files to another format (mp3 to m4a, etc). So the way I see it I need to download these files to my server, modify the files or transcode this files, and reupload the files.
I see some ways to do this but I have some doubts on which is the correct way or best way to do it.
So one way would be to download the file with the following code
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + object.getObjectMetadata());
//displayTextInputStream(object.getObjectContent());
And I could use File to write the file to my server.
My question heres is how do I obtain just the file name from the S3Object, I was looking in the metadata and tried to use getContentDisposition(), but it returns null, looking directly in AWS Console I see the proper name of the file without the path.
The other idea I have is to use cloudfront to download the file, creating a download distribution.
Can I work directly with and inputstream to modify the metadata?
You can save this file under any name you choose, just like any other file you download through HTTP.
However, as you mentioned in your question:
String disposition = object.getObjectMetadata().getContentDisposition()
Should give you the optional Content-Disposition HTTP header, which specifies presentation information for the object such as the recommended file-name for the object to be saved as.
This function returns null if the Content-Disposition header hasn't been set.
This is from Amazon online documentation: For more information on how the Content-Disposition header affects HTTP client behavior, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1.
I have files(pdf,doc,txt,xsl,etc..) stored in my mongo *db*. I want to retrieve and open them. I know to extract. Also I found out that Desktop.getDesktop().open(FileName); will open the file with its respective application(Acrobat reader, office suite etc). But can anyone please tell me if only the file would be enough or we should give the full path for the file.
In the latter case, can anyone plesae tell me how I could path for the file which is being retrieved from mongodb?
You'll have to extract the file from Mongodb if you want to open it using Desktop.getDesktop().open(FileName);
You can create a temp file using File.createTempFile and write in it the content fetched from mongodb, with the correct file extension, and then run Desktop.getDesktop().open() to open it.
I'm facing a problem that, we have a .zip file that contains some text files. Now I'm using java to access that files. If it is not in the .zip file I can read and print on my console easily using FileInputStream.
But how to read a file from .zip file? I use J2SE only..
You should try a ZipInputStream. The interface is a little obtuse, but you can use getNextEntry() to iterate through the items in the .zip file.
As a side note, the Java class-loader does exactly this to load classes from .jar files without extracting them first.
Everything you need is in ZipFile: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html. Google for examples on the web, and if you have specific problems then come back to SO for help.
(The link will eventually break; when it does simply websearch java zipfile.)