{"id":20297,"date":"2013-05-10T12:49:02","date_gmt":"2013-05-10T07:19:02","guid":{"rendered":"http:\/\/vskills.in\/certification\/tutorial\/?p=20297"},"modified":"2024-04-12T14:16:40","modified_gmt":"2024-04-12T08:46:40","slug":"single-range-and-multiget-slice","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/","title":{"rendered":"Single, range and multiget slice"},"content":{"rendered":"<p><a class=\"vsc\" href=\"http:\/\/www.vskills.in\/certification\/Certified-Cassandra-Professional\"><span class=\"vsc-cn\" style=\"text-align: center;\"><span style=\"color: red;\">Certify and Increase Opportunity.<\/span><br \/>\n<span style=\"color: green;\">Be <\/span><br \/>\nGovt. Certified Apache Cassandra Professional<br \/>\n<\/span><\/a><\/p>\n<p>Single, range and multiget slice<\/p>\n<p><strong>SLICE<\/strong><\/p>\n<p>A slice is a result of a query on the database. When querying a database i want to be able to ask something like.<\/p>\n<p>Give me the password of the user with the login name of loginname.<\/p>\n<p>When querying Cassandra a simple question like that is less simple from what you might expect.<br \/>\nThe way forward for me is using and setting a secondary index on a column.<\/p>\n<p>A secondary index is telling Cassandra to be prepared to answer simple questions like above.<br \/>\nNote that this feature is build in later so i expect that this is not the normal way of querying the database but for now i can create ColumnFamilies with indexed Columns.<\/p>\n<p><span style=\"text-decoration: underline;\">Create an indexed Column<\/span><\/p>\n<p>BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition();<br \/>\ncolumnFamilyDefinition.setKeyspaceName(_keyspaceName);<br \/>\ncolumnFamilyDefinition.setName(_columnFamilyName);<br \/>\ncolumnFamilyDefinition.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());<\/p>\n<p>\/\/ Create a column that can be queried by a secondary slice<br \/>\nBasicColumnDefinition bcd = new BasicColumnDefinition();<br \/>\nbcd.setName(StringSerializer.get().toByteBuffer(FIRSTNAME_COLUMN_NAME));<br \/>\nbcd.setIndexName(FIRSTNAME_COLUMN_NAME);<br \/>\nbcd.setValidationClass(&#8220;org.apache.cassandra.db.marshal.UTF8Type&#8221;);<br \/>\nbcd.setIndexType(ColumnIndexType.KEYS);<br \/>\ncolumnFamilyDefinition.addColumnDefinition(bcd);<br \/>\n\/\/ Persist or execute update on cassandra<br \/>\n_cluster.addColumnFamily(columnFamilyDefinition);<\/p>\n<p><span style=\"text-decoration: underline;\">Slice an indexed Column<\/span><\/p>\n<p>\/\/ Create the query object<br \/>\nIndexedSlicesQuery&lt;String, String, byte[]&gt; indexedSlicesQuery = HFactory.createIndexedSlicesQuery(_keyspace,<br \/>\nstringSerializer,stringSerializer, byteArraySerializer);<\/p>\n<p>\/\/ Query column family with name<br \/>\nindexedSlicesQuery.setColumnFamily(_columnFamilyName);<br \/>\n\/\/ Set the columns you want back<br \/>\nindexedSlicesQuery.setColumnNames(FIRSTNAME_COLUMN_NAME, MIDDLENAME_COLUMN_NAME,LASTNAME_COLUMN_NAME);<br \/>\n\/\/ A mandatory equals method. Find the firstname10 in column with name firstname<br \/>\nindexedSlicesQuery.addEqualsExpression(FIRSTNAME_COLUMN_NAME,stringSerializer.toBytes(&#8220;firstname10&#8221;));<br \/>\n\/\/ Do the query<br \/>\nQueryResult&lt;OrderedRows&lt;String, String, byte[]&gt;&gt; result = indexedSlicesQuery.execute();<\/p>\n<p>boolean found = false;<br \/>\n\/\/ result is &#8230;.<br \/>\nIterator&lt;Row&lt;String, String, byte[]&gt;&gt; queryResultIterator = result.get().iterator();<\/p>\n<p>while (queryResultIterator.hasNext()){<br \/>\n\/\/ fetch the result and translate the content of the column<br \/>\nRow&lt;String, String, byte[]&gt; queryRow= queryResultIterator.next();<br \/>\nString firstName = stringSerializer.fromBytes(<br \/>\nqueryRow.getColumnSlice().getColumnByName(&#8220;firstName&#8221;).getValue());<br \/>\nAssert.assertEquals(&#8220;Search string should match query string&#8221;, firstName , &#8220;firstname10&#8221;);<br \/>\nfound&nbsp; = true;<br \/>\n}<\/p>\n<p><strong>Range Slice<\/strong><\/p>\n<p>In Cassandra, rows are hash partitioned&nbsp; by default. If you want to data sorted by some attribute, column name sorting feature of Cassandra is usually exploited. If you look at the Cassandra slice range API, you will find that you can specify only the range start, range end and an upper limit on the number of columns fetched.<\/p>\n<p>However in many applications the need is to paginate through the data i.e each call should fetch a predetermined number of items.<\/p>\n<p>The only option is to select a range such that that the number of items expected to return is far greater than the max limit. In this post I will discuss an adaptive range reader that I have implemented recently in agiato hosted on github. It borrows ideas from feedback control system to adaptively change the column range so that a predetermined number of items are returned.<\/p>\n<p>Listed below id the slice range constructor as defined. It gets used in the Cassandra API get_slice(). As you can tell, there is no way to set the appropriate range that will ensure the return of expected number of items.<\/p>\n<p>SliceRange(byte[] start, byte[] finish, boolean reversed, int count)<\/p>\n<p>The only thing you can do is to set start low enough and finish high enough, and hope&nbsp; that the number of columns in that range is&nbsp; greater than count, resulting in count number of items returned from the query. But, there is no guarantee and nobody likes hope based logic.<\/p>\n<p><strong>Multiget Slice<\/strong><\/p>\n<p>With get_slice, user can fetch a set of column names for a single specified row key. But, multiget_slice can fetch a subset of columns for a set of row keys based on a column parent and a predicate. Thus by providing more than one row key, fetch the value of the named columns for each key. So a multiget slice is more than one named column for more than one row.<\/p>\n<div class=\"apply\">\n<h3>Apply for Apache Cassandra Certification Now!!<\/h3>\n<p><a href=\"http:\/\/www.vskills.in\/certification\/Certified-Cassandra-Professional\">http:\/\/www.vskills.in\/certification\/Certified-Apache-Cassandra-Professional<\/a><\/p>\n<h5>Go To Tutorial-&nbsp;<a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/apache-cassandra-professional\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Certified Apache Cassandra Professional Tutorial<\/strong><\/a><\/h5>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Certify and Increase Opportunity. Be Govt. Certified Apache Cassandra Professional Single, range and multiget slice SLICE A slice is a result of a query on the database. When querying a database i want to be able to ask something like. Give me the password of the user with the login name of loginname. When querying&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[63],"tags":[],"class_list":["post-20297","page","type-page","status-publish","hentry","category-apache-cassandra"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Single, range and multiget slice - Tutorial<\/title>\n<meta name=\"description\" content=\"Single, range and multiget slice. Vskills Government Certification in Apache Cassandra is very popular in India amongst developers working in the IT Bid data industry.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Single, range and multiget slice - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Single, range and multiget slice. Vskills Government Certification in Apache Cassandra is very popular in India amongst developers working in the IT Bid data industry.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:46:40+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/\",\"name\":\"Single, range and multiget slice - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2013-05-10T07:19:02+00:00\",\"dateModified\":\"2024-04-12T08:46:40+00:00\",\"description\":\"Single, range and multiget slice. Vskills Government Certification in Apache Cassandra is very popular in India amongst developers working in the IT Bid data industry.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Single, range and multiget slice\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Single, range and multiget slice - Tutorial","description":"Single, range and multiget slice. Vskills Government Certification in Apache Cassandra is very popular in India amongst developers working in the IT Bid data industry.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/","og_locale":"en_US","og_type":"article","og_title":"Single, range and multiget slice - Tutorial","og_description":"Single, range and multiget slice. Vskills Government Certification in Apache Cassandra is very popular in India amongst developers working in the IT Bid data industry.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:46:40+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/","name":"Single, range and multiget slice - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2013-05-10T07:19:02+00:00","dateModified":"2024-04-12T08:46:40+00:00","description":"Single, range and multiget slice. Vskills Government Certification in Apache Cassandra is very popular in India amongst developers working in the IT Bid data industry.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/single-range-and-multiget-slice\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Single, range and multiget slice"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/20297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=20297"}],"version-history":[{"count":12,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/20297\/revisions"}],"predecessor-version":[{"id":122134,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/20297\/revisions\/122134"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=20297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=20297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=20297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}