Is there any javadoc (or any documentation at all) for the flexmonster CSVCompressor API. There are only a few examples in your documentation and I cannot find any in the forums. I am interested in using the compressor, but need some addition information to try and figure out how to integrate it with our Java application that currently extracting the data from MongoDB, transforming the data to csv format and streaming it out one record at a time to the Response object's PrintWiter (text-stream)
Hi Dennis,
Unfortunately, we don't have a detailed description of CSVCompressor API, but it doesn't have a complicated structure.
Please find all supported method below:
Compression for CSV files
InputStream compressFile(String fileName, char delimiter)
InputStream compressFile(String fileName)
Compression for strings in CSV format
InputStream compressString(String string, char delimiter)
InputStream compressString(String string)
Compression for streams
InputStream compressStream(InputStream inputStream, char delimiter)
InputStream compressStream(InputStream inputStream)
Compression for Databases
InputStream compressDb(ResultSet resultSet)
Example
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.flexmonster.compressor.Compressor;
public class CompressorServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/foodmart", "root", "password");
String query = "SELECT * FROM customer";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
InputStream inputStream = Compressor.compressDb(resultSet);
//InputStream inputStream = Compressor.compressFile(this.getServletContext().getRealPath("/WEB-INF/data.csv"));
response.setContentType("text/plain");
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[10240];
int length = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
outputStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am using MongDB, am performing a query and when the resultset is returned, I am processing each record, converting it to csv format and writing it to the HttpServletResponse outstream via 'out.println(csvData);' So I would assume I would use the Compressors for strings
InputStream compressString(String string) to compress the csvData and then use the standard out.write(); out.flush() loop as you have documented. However, wouldn't I need to change the response.setContentType to "application/octet-stream"
Hi Dennis,
I think in your case it would be enough to set content-type
as "text/plain"
.
Please ask me if you need more help.
Regards,
Tanya
when I call...
InputStream in = Compresor.compressString(columnNames);
where columnNames is a string of comma separated CSV column names (e.g. "ID,Name,Date,Time,Source,Severity,Category,Favorite"), I get an UnsupportedEncodingException:uft8
Note the "UFT8". I think you have a typo in your code. You probably meant "UTF8". Assuming this is issue and since you have not provided the source, I am unable to to use the Compressor until this code is corrected.
By the way, it would be nice to document an example of using the "compressString()" method. The only examples I can find are compressDB and compressFile.
Hello,
Please find the answer to the question in the following thread: http://www.flexmonster.com/question/error-calling-compressor-compressstringstring/.
Regards,
Dmytro.
Hello ~
I try to use mybatis Access database use this java compressor API and the parameters of this method【Compressor.compressDb(resultSet)】 is ResultSet type.
But the type returned by mybatis is List , so failed.
After , I use the 【Compressor.compressString(input) 】 method, foreach the list and append all string, it's work!
but this is too slow,Is there any better way?
thanks.
Hello Simba,
Thank you for writing. Yes, you are right, compressing string data is no a very efficient solution. However, we recommend using the ResultSet since it should provide much better performance. As we can see it, the MyBatis is a framework which works over the JDBC level, therefore the ResultSet should be available there. The idea is to path non-mapped data to the compressor. Seems it can be done the following way - https://stackoverflow.com/questions/36025073/get-resultset-from-mybatis.
Please let us know if the suggested approach solves the case.
Regards,
Dmytro
Hello,Dmytro
Thank you for your reply.
【The idea is to path non-mapped data to the compressor】,what non-mapped data mean?
Mybatis put executive sql and returntype in xml,like this:
and I try set this resultType=Resultset ,it is doesn't work .
Looking forward to your reply .
Thanks.
Hello Simba,
Getting non-mapped data means that you should get the object which contains metadata and query result. The possible solution is to write the necessary query using JDBC
connector. You can find the tutorial in our documentation - http://www.flexmonster.com/doc/connecting-to-relational-database-with-java/. The other approach is to get ResultSet
when using MyBatis. Please find the possible solution here - https://coderanch.com/t/415314/databases/retrieve-metadata-ibatis. You need to implement TypeHandlerCallback
interface and then call from ResultGetter
object getResultSet()
method - https://ibatis.apache.org/docs/java/user/com/ibatis/sqlmap/client/extensions/ResultGetter.html#getResultSet(). After that you can pass ResultSet
object to the compressor.
Regards,
Dmytro.