87326

Upsert data into Cassandra database using Astyanax client

Question:

<strong>Problem Statement:-</strong>

I am trying to insert data into Cassandra database. I am using Netflix/Astyanax client for this.

Below is the code which will upsert data into Cassandra database using Astyanax client.

In my below method, it accepts two parameter. One is the userId which I will be using as the RowKey and attributes map which will contain, column name as the key and it's column value as the value in the map.

Now how can I use Astyanax client to upsert data into Cassandra database using the below method which accepts those two parameters?

I am mainly concern about attributes Map. How should I use that map to upsert data into Cassandra database?

/** * Performs an upsert of the specified attributes for the specified id. */ public void upsertAttributes(final String userId, final Map<String, String> attributes) { MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch(); m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), userId) .putColumn(attributeName from attributesMap, attributeValue from attributesMap, null) ; try { m.execute(); } catch (ConnectionException e) { StringBuilder message = new StringBuilder(); message.append("Failed to read from C* = '").append(e).append("'. "); LOG.error("Failed to write data to C*", e); throw new RuntimeException("failed to write data to C*", e); } }

Answer1:

You have almost given the solution, but i think its not clear to you

MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch(); ColumnListMutation<String> clm = m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), userId); for(String key: attributeMap.keySet()){ clm.putColumn(key, attributeMap.get(key), null); } try { m.execute(); } catch (ConnectionException e) { e.printStackTrace(); }

Basically <strong>putColumn</strong> has different overloaded variations, so it will support all the datatypes supported by cassandra.

Recommend

  • How to remove special characters in file names?
  • cURL for Windows can't make a secure connection to the Stack API
  • Why is new Number(8) not exactly equal to 8?
  • Angular page doesn't refresh after data is added or removed
  • How to call a procedure using NHibernate that returns result from multiple tables?
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Running jasmine tests for a component with NgZone dependency
  • What does “t” refer to in this SQL?
  • Private IP address in reserved subnet range
  • Bigquery event streaming and table creation
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • Yii2: Config params vs. const/define
  • How to do unit test for HttpContext.Current.Server.MapPath
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why value captured by reference in lambda is broken? [duplicate]
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • script to move all files from one location to another location
  • sending/ receiving email in Java
  • JSON with duplicate key names losing information when parsed
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • C# - Getting references of reference
  • JTable with a ScrollPane misbehaving
  • Why joiner is not used after Sequence generator or Update statergy
  • How to stop GridView from loading again when I press back button?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Android Heatmap on canvas or ImageView
  • How can I use threading to 'tick' a timer to be accessed by other threads?