Showing posts with label rows. Show all posts
Showing posts with label rows. Show all posts

Sunday, 12 April 2020

TableSaw: Add rows from other table

‘table.addRow’ method adds a row from source table to destination table.

Example
‘destinationTable.addRow(rowNumberToAdd, sourceTable)’

Below snippet add all the rows from destinationTable to sourceTable.
for(int i = 0; i < destinationTable.rowCount(); i++) {
         sourceTable.addRow(i, destinationTable);
}

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

 public static void main(String args[]) {
  int[] empIds = { 1, 2, 3, 4, 5 };
  String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Hareesh" };
  String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Baji" };

  Table sourceTable = Table.create().addColumns(IntColumn.create("Employee Id", empIds))
    .addColumns(StringColumn.create("FirstName", firstNames))
    .addColumns(StringColumn.create("LastName", lastNames));

  int[] empIds1 = { 5, 6 };
  String[] firstNames1 = { "Sarala", "Vimala" };
  String[] lastNames1 = { "Maj", "Krishna" };

  Table destinationTable = Table.create().addColumns(IntColumn.create("Employee Id", empIds1))
    .addColumns(StringColumn.create("FirstName", firstNames1))
    .addColumns(StringColumn.create("LastName", lastNames1));

  System.out.println("Source Table : " + sourceTable.print() + "\n");
  System.out.println("Destination Table : \n" + destinationTable.print());
  
  System.out.println("\nAdding all the rows from destination table to source table\n");
  
  for(int i = 0; i < destinationTable.rowCount(); i++) {
   sourceTable.addRow(i, destinationTable);
  }
  
  System.out.println("Source Table : \n" + sourceTable.print() + "\n");
 }
}

Output

Source Table :  Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           1  |       Hari  |   Krishna  |
           2  |        Ram  |    Gurram  |
           3  |     Sowmya  |       Maj  |
           4  |      Chamu  |       Dev  |
           5  |    Hareesh  |      Baji  |

Destination Table : 
 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           5  |     Sarala  |       Maj  |
           6  |     Vimala  |   Krishna  |

Adding all the rows from destination table to source table

Source Table : 
 Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           1  |       Hari  |   Krishna  |
           2  |        Ram  |    Gurram  |
           3  |     Sowmya  |       Maj  |
           4  |      Chamu  |       Dev  |
           5  |    Hareesh  |      Baji  |
           5  |     Sarala  |       Maj  |
           6  |     Vimala  |   Krishna  |





Previous                                                    Next                                                    Home

Saturday, 11 April 2020

TableSaw: Select n random rows from a table

'table.sampleN(noOfRows)' return n number of rows from given table.

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

 public static void main(String args[]) {
  int[] empIds = { 1, 2, 3, 4, 5 };
  String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Hareesh" };
  String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Baji" };

  Table table = Table.create().addColumns(IntColumn.create("Employee Id", empIds))
    .addColumns(StringColumn.create("FirstName", firstNames))
    .addColumns(StringColumn.create("LastName", lastNames));

  Table randomData = table.sampleN(3);
  System.out.println(randomData.print());
 }
}

Output
Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           1  |       Hari  |   Krishna  |
           2  |        Ram  |    Gurram  |
           4  |      Chamu  |       Dev  |


Previous                                                    Next                                                    Home

Sunday, 5 April 2020

TableSaw: Print all the rows in console

'table.printAll()' method returns a string representation of table.

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

 public static void main(String args[]) {
  int[] empIds = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  String[] firstNames = { "Hari", "Ram", "Chamu", "Sowmya", "Harini", "Lahari", "Rama", "Lakshman", "Sandya" };

  Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
    .addColumns(StringColumn.create("FirstName", firstNames));

  System.out.println(table.printAll());
 }
}

Output

Employee Ids  |  FirstName  |
------------------------------
            1  |       Hari  |
            2  |        Ram  |
            3  |      Chamu  |
            4  |     Sowmya  |
            5  |     Harini  |
            6  |     Lahari  |
            7  |       Rama  |
            8  |   Lakshman  |
            9  |     Sandya  |





Previous                                                    Next                                                    Home

Thursday, 2 April 2020

TableSaw: Get number of rows and columns in a table

‘table.rowCount()’ returns number of rows in a table.
‘table.columnCount()’ returns number of columns in a table.

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

 public static void main(String args[]) {
  int[] empIds = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  String[] firstNames = { "Hari", "Ram", "Chamu", "Sowmya", "Harini", "Lahari", "Rama", "Lakshman", "Sandya" };

  Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
    .addColumns(StringColumn.create("FirstName", firstNames));

  table.setName("Employee Information");

  System.out.println("Row Count : " + table.rowCount());
  System.out.println("Column Count : " + table.columnCount());
 }
}

Output
Row Count : 9
Column Count : 2

Previous                                                    Next                                                    Home

Sunday, 29 March 2020

TableSaw: Selection.with : Select specific rows

‘Selection.with’ method is used to select the row at given index.

Example
IntColumn filteredElements = intColumn.where(Selection.with(0, 2, 4, 6));

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;
import tech.tablesaw.selection.Selection;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 2, 4, 103, 24, 53, 23, 12, 11, 56, 6 };

  IntColumn intColumn = IntColumn.create("My Numbers", numbers);

  IntColumn filteredElements = intColumn.where(Selection.with(0, 2, 4, 6));

  System.out.println(intColumn.print() + "\n");

  filteredElements.setName("Filtered Elements");
  System.out.println(filteredElements.print());
 }
}

Output
Column: My Numbers
2
4
103
24
53
23
12
11
56
6


Column: Filtered Elements
2
103
53
12


Previous                                                    Next                                                    Home

Friday, 7 February 2020

Cassandra: Delete all the rows from a table

Using ‘TRUNCATE’ command, you can delete all the rows from a table.

Syntax:
<truncate-stmt> ::= TRUNCATE ( TABLE | COLUMNFAMILY )? <tablename>

Example:
TRUNCATE keyspaceName.tableName;

If you are already in the keyspace, then you can use the tableName directly.

TRUNCATE tableName;

cqlsh> CREATE KEYSPACE cassandratutorial WITH REPLICATION = 
   ... { 
   ...   'class' : 'SimpleStrategy', 
   ...   'replication_factor' : 1 
   ... };
cqlsh> 
cqlsh> CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
   ...   id INT PRIMARY KEY, 
   ...   firstName VARCHAR,
   ...   lastName VARCHAR,
   ...   age int
   ... );
cqlsh> 
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 1, "firstName" : "Krishna", "lastName" : "Gurram", "age" : 30}';
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 2, "firstName" : "Ram", "lastName" : "Gurram", "age" : 31}' ;
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 3, "firstName" : "Vijay", "lastName" : "Ponnam", "age" : 45}';
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 4, "firstName" : "Chitra", "lastName" : "Rajan", "age" : 45}';
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.employee;

 id | age | firstname | lastname
----+-----+-----------+----------
  1 |  30 |   Krishna |   Gurram
  2 |  31 |       Ram |   Gurram
  4 |  45 |    Chitra |    Rajan
  3 |  45 |     Vijay |   Ponnam

(4 rows)
cqlsh> 
cqlsh> TRUNCATE cassandratutorial.employee;
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.employee;

 id | age | firstname | lastname
----+-----+-----------+----------

(0 rows)



Previous                                                    Next                                                    Home