How do I display database data in table format? - ruby

I want to display the table in the below format. How can I achieve this?
---A----+-----B-----+----C-----
12335 | abcd | qwerty
45335 | efgh | poiuy
78956 | hjukukuk | mkloijhkll
12346 | sfsfsf | vbhkhadad
EDIT 1:
The contents of the table can be of any length. The width of the particular cell has to be decided by the content itself.
I got the column width from
col_width = a.transpose.map{|col| col.map{|cell| cell.to_s.length}.max}
and displayed the table contents with:
a.each{|row| puts '['+
row.zip(col_width).map{|cell, w| cell.to_s.ljust(w)}.join(' | ')+']'}
where 'a' contains the data from the database.
I only cannot get to print the column headers.
How i can achieve those so that it can align with the table cell contents.
I need to display the output in the console. I am using OCI for accessing the database.

The Sequel ORM can do this using the pretty_table extension. Otherwise HIRB is capable of doing it, either with ActiveRecord, or from arrays/hashes.
I use Sequel often, and have occasional need to display a table summary on the console, so pretty_table works nicely for me.
HIRB is used in the irbtools plugin for IRB, and provides table output for all sorts of things.
In either case, the displayed table width is dynamically determined using the lengths of the strings being displayed to find the widths of the columns. I've never tried pushing really long strings through either that would require wrapping inside a column, but they should automatically handle that since it's a common requirement.

I wrote a gem that will help you with this: http://github.com/arches/table_print

Related

How to control formatting of pipe format tables in bookdown, such as striping?

Many thanks to Rstudio for the visual Rmarkdown editor. I am using it to generate and preview bookdown content. When I insert a table, it generates a pipe format table. However I want to remove the alternate row shading which kable does by default. There is plenty of documentation on using function kable_styling but I cannot use that - there is no object for the table, it is autogenerated in the visual editor. I don't really want to load it into a dataframe - that seems the wrong route.
How can I switch off row shading for pipe format tables globally or more generally modify the options for tables generated in this way via the visual Rmarkdown editor or otherwise?
A minimal section of my table looks like this. When I knit it in Rstudio as a Rmarkdown document it appears unstriped. I would like it stripe-free in bookdown, and ideally to control other styling of it.
+------------------+------------+
| $A=\pi.r^2$ | 1 |
+------------------+------------+
| $\sqrt{b^2-4ac}$ | 2 |
+------------------+------------+
| $\sum{1/i}$ | 3 |
+------------------+------------+
Many thanks.

How Hbase handles duplicate records?

I want to understand how Hbase internally handles duplicates records from a file.
In order to experiment this, I have created an EXTERNAL table in hive with HBase specific configuration properties like table properties, SERDE, column family.
I have to create the table in HBase with column family as well, which I did.
I have performed an insert overwrite into this HIVE table from a source table which has duplicate records.
By duplicate records I mean like this,
ID | Name | Surname
1 | Ritesh | Rai
1 | RiteshKumar | Rai
Now after performing insert overwrite, I queried my HIVE table with id 1, I got the output as (the second one)
1 RiteshKumar Rai
I wanted to under how HBase decides which one is updated? Is it just that it just writes the data in a sequential manner. The last record will be overwritten in and considered as latest? Or how it is?
Thanks in advance.
Regards,
Govind
You are on the right track!
HBase datamodel can be seen as a 'multidimensional map' and each cell value is associated with a timestamp (insertion_time by default):
row:column_family:column_qualifier:timestamp:value
NOTE: The timestamp is associated with each single value and not the entire row (This enables several nice features)!
At read time you will get the latest versions by default unless you specify otherwise. By default 3 versions should be stored. Hbase does a 'merge read' and it will return the latest cell value for each row.
Please try this from your hbase-shell (not really tested before posting):
put ‘table_name’, ‘1’, ‘f:name’, ‘Ritesh’
put ‘table_name’, ‘1’, ‘f:surname’, ‘Rai’
put ‘table_name’, ‘1’, ‘f:name’, ‘RiteshKumar’
put ‘table_name’, ‘1’, ‘f:surname’, ‘Rai’
put ‘table_name’, ‘1’, ‘f:other’, ‘Some other stuff’
// Data on 'disk' (that might just be the memstore for now) will look like this:
// 1:f:name:1234567890:‘Ritesh’
// 1:f:surname:1234567891:‘Rai’
// 1:f:name:1234567892:‘RiteshKumar’
// 1:f:surname:1234567893:‘Rai’
// 1:f:other:1234567894:‘Some other stuff’
// Now try... And you will get ‘RiteshKumar’, ‘Rai’, ‘Some other stuff’
get ‘table_name’, ‘1’
// To get the previous versions of the data use the following:
get ‘table_name’, ‘1’, {COLUMN => ‘f’, VERSIONS => 2}
Don't forget to take a look at the best practices of schema design

Wicket custom (data)table layout

Is there a way to customize the wicket DataTable layout?
I'd like to have a DataTable with a horizontal flow of data and multiple columns per row.
e.g.:
------------------------------
|prename| John| surname| Doe |
------------------------------
|city | NY | country| USA |
------------------------------
Unlike the normal use of the DataTable class this would be used for displaying just one object (respectively one dataset)
Don't use a DataTable for this.
For a simple case, just make a panel containing a Label for each data element and put the table layout in the corresponding html fragment.
To make it more dynamic, with parametrized size as noted in your comment, you might build your own structure using nested RepeatingView components, one for the rows and one for the columns, or perhaps use a DataGridView, which would do some of the layout work for you.
But DataTable is really meant for a table with meaningful columnar structure and includes alot of code for handling the column structure which won't make sense for your data and will get in your way.

How to force a vertical table or limit columns in Hirb?

I want to display few active records in rails console, I have Hirb enabled. The table is narrow enough to be displayed (so Hirb uses standard, horizontal table) but columns are so narrow that content is completely unreadable. Do you have any idea what I could do about it?
Displaying only few columns would be great (I have records in a pure Array, not AR collection, so I cannot just pass :select to finder method). Forcing Hirb to display records in vertical table would be perfect as well.
Thanks in advance.
If you look at the 'Views: Anytime, Anywhere' section of hirb's readme, you'll see that hirb provides you with a table command that let's you select columns/fields:
>> extend Hirb::Console
=> main
>> table My_AR_Array, :fields=>[:field1, :another_field, :and_another_one]
# ... Displays table with only these three columns
If you'd like to enable a vertical view, read the docs. In particular, learn about a table's options (:vertical is what you want) and learn about hirb's config file format.
In the future, please ask these questions on github.

How do I return multiple columns of data using ImportXML in Google Spreadsheets?

I'm using ImportXML in a Google Spreadsheet to access the user_timeline method in the Twitter API. I'd like to extract the created_at and text fields from the response and create a two-column display of the results.
Currently I'm doing this by calling the API twice, with
=ImportXML("http://twitter.com/status/user_timeline/matthewsim.xml?count=200","/statuses/status/created_at")
in the cell at the top of one column, and
=ImportXML("http://twitter.com/status/user_timeline/matthewsim.xml?count=200","/statuses/status/text")
in another.
Is there a way for me to create this display with a single call?
ImportXML supports using the xpath | separator to include as many queries as you like.
=ImportXML("http://url"; "//#author | //#catalogid| //#publisherid")
However it does not expand the results into multiple columns. You get a single column of repeating triplets (or however many attributes you've selected) as shown below in column A.
The following is deprecated
2015.06.16: continue is not available in "the new Google Sheets" (see: The Google Documentation for continue).
However you don't need to use the automatically inserted CONTINUE() function to place your results.
=CONTINUE($A$2, (ROW()-ROW($A$2)+1)*$A$1-B$1, 1)
Placed in B2 that should cleanly fill down and right to give you sane column data.
ImportXML is in A2.
A3 and below are how the CONTINUE() functions are automatically filled in.
A1 is the number of attributes.
B1:D1 are the attribute index for their columns.
Another way to convert the rows of =CONTINUE() into columns is to use transpose():
=transpose(importxml("http://url","//a | //b | //c"))
Just concatenate your queries with "|"
=ImportXML("http://twitter.com/status/user_timeline/matthewsim.xml?count=200","/statuses/status/created_at | /statuses/status/text")
I posed this question to the Google Support Forum and this is was a solution that worked for me:
=ArrayFormula(QUERY(QUERY(IFERROR(IF({1,1,0},IF({1,0,0},INT((ROW(A:A)-1)/2),MOD(ROW(A:A)-1,2)),IMPORTXML("http://example.com","//td/a | //td/a/#href"))),"select min(Col3) where Col3 <> '' group by Col1 pivot Col2",0),"offset 1",0))
Replace the contents of IMPORTXML with your data and query and see if that works for you. I
Apparently, this attempts to invoke the IMPORTXML function only once. It's a solution for now, at least.
Here's the full thread.
This is the best solution (NOT MINE) posted in the comments below. To be honest, I'm not sure how it works. Perhaps #Pandora, the original poster, could provide an explanation.
=ArrayFormula(iferror(hlookup(1,{1;ARRAY},(row(A:A)+1)*2-transpose(sort(row(A1:A2)+0,1,0)))))
This is a very ugly solution and doesn't even explain how it works. At least I couldn't get it to work due to multiple errors, like i.e. to much parameters for IF (because an array is used). A shorter solution can be found here =ArrayFormula(iferror(hlookup(1,{1;ARRAY},(row(A:A)+1)*2-transpose(sort(row(A1:A2)+0,1,0))))) "ARRAY" can be replaced with IMPORTXML-Function. This function can be used for as much XPATHS one wants. – Pandora Mar 7 '19 at 15:51
In particular, it would be good to know how to modify the formula to accommodate more columns.

Resources