Insert SuperColumn in Cassandra using thrift - java

I need some clear examples how I can insert super columns in super column using thrift and java.

#Override
public void insertAllReportsByHost(Map<String, List<IReport>> hostReports) throws DatabaseException {
try {
Cassandra.Client client = getClient();
Map<ByteBuffer, Map<String, List<Mutation>>> mutationsMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
for (Map.Entry<String, List<IReport>> entryReport : hostReports.entrySet()) {
ByteBuffer host = toByteBuffer(entryReport.getKey());
List<IReport> reports = entryReport.getValue();
Map<String, List<Mutation>> keyMutations = new HashMap<String, List<Mutation>>();
List<Mutation> mutations = new ArrayList<Mutation>();
for (IReport report : reports) {
report.getProperties();
Column reportDataColumn = new Column(toByteBuffer("Report Data"));
reportDataColumn.setValue(toByteBuffer(report.toString()));//TODO make another way
reportDataColumn.setTimestamp(System.currentTimeMillis());
Long nano = System.nanoTime();
SuperColumn superColumn = new SuperColumn(toByteBuffer(report.getReportId().toString()), Arrays.asList(reportDataColumn));
ColumnOrSuperColumn col = new ColumnOrSuperColumn();
col.super_column = superColumn;
Mutation m = new Mutation();
m.setColumn_or_supercolumn(col);
mutations.add(m);
}
keyMutations.put(COLUMN_FAMILY, mutations);
mutationsMap.put(host, keyMutations);
}
client.batch_mutate(mutationsMap, ConsistencyLevel.ONE);
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (UnavailableException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (TException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (InvalidRequestException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (TimedOutException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
}
}

Related

Issue while pushing branch into remote (jgit)

Hi i created a branch and added some files on that and checked out that branch after that if I try pushing the branch it is not pushed to remote only inital commit gets pushed each and everytime
Here is the code
private void remotePush(GitDto gitDto, CredentialsProvider credential, File destination)
{
try (Repository repository = repositoryBuilder(destination)) {
try (Git git = Git.open(destination)) {
git.checkout().setName(gitDto.getBranchName()).call(); git.commit().setAll(true).setMessage(gitDto.getCommitMessage()).call();
git.push().setRemote("origin").setCredentialsProvider(credential)
.setRefSpecs(new RefSpec(gitDto.getBranchName() + ":" + gitDto.getBranchName()))
.call();
} catch (RefNotFoundException e)
{
throw new RuntimeException(e);
}
catch (RefAlreadyExistsException e)
{
throw new RuntimeException(e);
}
catch (InvalidRefNameException e)
{
throw new RuntimeException(e);
}
catch (CheckoutConflictException e)
{
throw new RuntimeException(e);
}
catch (GitAPIException e) { throw new RuntimeException(e); }
} catch (IOException e) { throw new RuntimeException(e); }
}
Thanks in advance

How to use map function in my code below instead of collect function?

Kafka producer tries to send bulk message (10k) at a time but after transferring 3k it's start throwing error. I have used collect function in below code that might be a problem so I want to replace that with map.
//Memberjson is JavaRDD<String>
for (String outputMbrJson : memberjson.collect()) {
try {
Producer kafkaProducer = new Producer(configFile);
kafkaProducer.runProducer(Arrays.asList(outputMbrJson).iterator());
}
catch (Throwable e) {
e.printStackTrace();
}
}
---------------------------------------
//runProducer method
public Iterator<String> runProducer(Iterator<String> jsons) throws Exception {
final Producer<Long, String> producer = createProducer();
ArrayList<String> ret = new ArrayList<String>();
try {
while (jsons.hasNext()) {
String jsonobj = jsons.next();
ret.add(EMPTY_STRING);
try {
ProducerRecord<Long, String> record = new ProducerRecord<Long, String>(conf.getProperty("producer.topic"), jsonobj.toString());
producer.send(record);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
producer.flush();
producer.close();
}
return ret.iterator();
}

Reading objects from assets

something is really messed up. I've got a ".ser" document in the assets folder, which stores an ArrayList of Objetcs. In an android application, I want to read this objects. There are a lot of posts related to this issue, however none of them could solve my problem. The strange part is, when I am using similar code in non - android context / "normal" java, it works properly. Here, the last line throws a NullPointerException - What is going wrong?
public void getData() {
ArrayList<MyClass> output= null;
InputStream is = null;
ObjectInputStream ois = null;
try{
is = this.getAssets().open("data.ser");
ois = new ObjectInputStream(is);
output = (ArrayList<MyClass>)ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("TAG", output.get(0).getId());
}
I would create a class and place the array within a single object:
public class ListObjects implements Serializable {
List<MyClass> listMyClass = new ArrayList<>();
public ListObjects(){
}
public List<MyClass> getListMyClass() {
return listMyClass;
}
public void setListMyClass(List<MyClass> listMyClass) {
this.listMyClass = listMyClass;
}
}
I had a similar problem. And it was because the name of the package in the java app was not called the same as the package name in android. And therefore I did not recognize them as equal objects. This is how I do it:
public static Object fromData(byte[] data) {
ObjectInputStream ois = null;
Object object = null;
try {
ois = new ObjectInputStream(
new ByteArrayInputStream(data));
object = ois.readObject();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
ois.close();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
return object;
}

Merge pdf files and loss of information

doing the merge of 2 or + pdf I lose some information that imposed in the upload phase of the files (ALT tags on the images). This is the method:
public static void mergeFiles(ArrayList<String> filesToBeMerged, String mergedFileLocation) {
String[] filesTBM = filesToBeMerged.toArray(new String[filesToBeMerged.size()]);
PDFMergerUtility ut = new PDFMergerUtility();
try {
for (int i = 0; i < filesTBM.length; i++) {
ut.addSource(filesTBM[i]);
}
ut.setDestinationFileName(mergedFileLocation);
ut.mergeDocuments();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
}
If the PDF with the ALT tags were in the list of files to be merged, the result is correct, otherwise not. So far I've tried max with 3 PDFs including 1 with ALT tags.
The questions:
How can I not lose the alt tag after the merge of the files?
Thanks to those who want to help me
Daniele
N.b. I have also tried iText pdf:
public static void mergeFiles(ArrayList<String> filesToBeMerged, String mergedFileLocation) {
String[] filesTBM = filesToBeMerged.toArray(new String[filesToBeMerged.size()]);
Document document = new Document();
PdfCopy copy = null;
try {
copy = new PdfCopy(document, new FileOutputStream(mergedFileLocation));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
document.open();
PdfReader[] reader = new PdfReader[filesTBM.length];
for (int i = 0; i < filesTBM.length; i++) {
try {
reader[i] = new PdfReader(filesTBM[i]);
} catch (IOException e) {
e.printStackTrace();
}
try {
copy.addDocument(reader[i]);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
copy.freeReader(reader[i]);
} catch (IOException e) {
e.printStackTrace();
}
reader[i].close();
}
document.close();
}

How to read multiple same objects from a file

I am trying to read objects from a file(same objects of a class), using Serializable, but whenit reads all objects it gives me error IOException, java.io.ObjectInputStream$BlockDataInputStream.peekByte.
I am reading objects and then saving to list. But as it reached lets say EOF it throws error.
Here is my method:
private static void updateBook(String name) {
// TODO Auto-generated method stub
FileInputStream fis = null;
ObjectInputStream in = null;
Object obj = new Object();
List<Object> libb = new ArrayList<Object>();
File file = new File(name + ".ser");
if (file.exists()) {
try {
fis = new FileInputStream(file);
in = new ObjectInputStream(fis);
try {
while (true) {
obj = in.readObject();
libb.add(obj);
}
} catch (OptionalDataException e) {
if (!e.eof) throw e;
//JOptionPane.showMessageDialog(null, "Done!");
} finally {
in.close();
//fis.close();
}
for(int j = 0; j < libb.size(); ++j) {
Book li = new Book();
li = (Book) libb.get(j);
System.out.println(li.getBookName());
}
//
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} else {
System.out.println("\nThe file does not Exist!");
}
}
Can anyone please tell me how to avoid this error from while(true).
Complete error:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
On your try statement you are missing the catch clause for the EOFException:
try {
while (true) {
obj = in.readObject();
libb.add(obj);
}
} catch (OptionalDataException e) {
if (!e.eof) throw e;
//JOptionPane.showMessageDialog(null, "Done!");
} catch (EOFException eofe) {
// treat it as you like
} finally {
in.close();
//fis.close();
}
you should add:
catch (EOFException e){
// do stuffs
}
as EOFException is not being caught.

Categories