Skip to content

Commit 1e354c6

Browse files
authored
Merge pull request #112619 from manishmsfte/cassandra-changefeed
added Java Sample
2 parents 6ea1259 + c49d8fd commit 1e354c6

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

articles/cosmos-db/cassandra-change-feed.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ ms.author: thvankra
1313

1414
[Change feed](change-feed.md) support in the Azure Cosmos DB API for Cassandra is available through the query predicates in the Cassandra Query Language (CQL). Using these predicate conditions, you can query the change feed API. Applications can get the changes made to a table using the primary key (also known as the partition key) as is required in CQL. You can then take further actions based on the results. Changes to the rows in the table are captured in the order of their modification time and the sort order is guaranteed per partition key.
1515

16-
The following example shows how to get a change feed on all the rows in a Cassandra API Keyspace table using .NET. The predicate COSMOS_CHANGEFEED_START_TIME() is used directly within CQL to query items in the change feed from a specified start time (in this case current datetime). You can download the full sample [here](https://docs.microsoft.com/samples/azure-samples/azure-cosmos-db-cassandra-change-feed/cassandra-change-feed/).
16+
The following example shows how to get a change feed on all the rows in a Cassandra API Keyspace table using .NET. The predicate COSMOS_CHANGEFEED_START_TIME() is used directly within CQL to query items in the change feed from a specified start time (in this case current datetime). You can download the full sample, for C# [here](https://docs.microsoft.com/samples/azure-samples/azure-cosmos-db-cassandra-change-feed/cassandra-change-feed/) and for Java [here](https://github.com/Azure-Samples/cosmos-changefeed-cassandra-java).
1717

18-
In each iteration, the query resumes at the last point changes were read, using paging state. We can see a continuous stream of new changes to the table in the Keyspace. We will see changes to rows that are inserted, or updated. Watching for delete operations using change feed in Cassandra API is currently not supported.
18+
In each iteration, the query resumes at the last point changes were read, using paging state. We can see a continuous stream of new changes to the table in the Keyspace. We will see changes to rows that are inserted, or updated. Watching for delete operations using change feed in Cassandra API is currently not supported.
1919

2020
```C#
2121
//set initial start time for pulling the change feed
@@ -65,7 +65,40 @@ In each iteration, the query resumes at the last point changes were read, using
6565
}
6666

6767
```
68+
```java
69+
Session cassandraSession = utils.getSession();
70+
71+
try {
72+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
73+
LocalDateTime now = LocalDateTime.now().minusHours(6).minusMinutes(30);
74+
String query="SELECT * FROM uprofile.user where COSMOS_CHANGEFEED_START_TIME()='"
75+
+ dtf.format(now)+ "'";
76+
77+
byte[] token=null;
78+
System.out.println(query);
79+
while(true)
80+
{
81+
SimpleStatement st=new SimpleStatement(query);
82+
st.setFetchSize(100);
83+
if(token!=null)
84+
st.setPagingStateUnsafe(token);
85+
86+
ResultSet result=cassandraSession.execute(st) ;
87+
token=result.getExecutionInfo().getPagingState().toBytes();
88+
89+
for(Row row:result)
90+
{
91+
System.out.println(row.getString("user_name"));
92+
}
93+
}
94+
95+
96+
} finally {
97+
utils.close();
98+
LOGGER.info("Please delete your table after verifying the presence of the data in portal or from CQL");
99+
}
68100

101+
```
69102
In order to get the changes to a single row by primary key, you can add the primary key in the query. The following example shows how to track changes for the row where "user_id = 1"
70103

71104
```C#
@@ -74,7 +107,11 @@ In order to get the changes to a single row by primary key, you can add the prim
74107
$"SELECT * FROM uprofile.user where user_id = 1 AND COSMOS_CHANGEFEED_START_TIME() = '{timeBegin.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture)}'");
75108

76109
```
77-
110+
```java
111+
String query="SELECT * FROM uprofile.user where user_id=1 and COSMOS_CHANGEFEED_START_TIME()='"
112+
+ dtf.format(now)+ "'";
113+
SimpleStatement st=new SimpleStatement(query);
114+
```
78115
## Current limitations
79116

80117
The following limitations are applicable when using change feed with Cassandra API:

0 commit comments

Comments
 (0)