How to Insert and retrieve a decimal value in hive table - java

I want to insert and retrieve a decimal value in hive table.
Here is my snippet which always returns NULL:
CREATE TABLE complex_types (c1 decimal);
INSERT INTO TABLE complex_types SELECT cast('100' as decimal);
SELECT * FROM complex_types;

You can read and write values in such a table using either the LazySimpleSerDe or the LazyBinarySerDe.
For example:
alter table decimal_1 set serde 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe';
or
alter table decimal_1 set serde 'org.apache.hadoop.hive.serde2.lazy.LazyBinarySerDe';
source information is here

Related

Insert into SQLite database if not exist in Java [duplicate]

I have an SQLite database. I am trying to insert values (users_id, lessoninfo_id) in table bookmarks, only if both do not exist before in a row.
INSERT INTO bookmarks(users_id,lessoninfo_id)
VALUES(
(SELECT _id FROM Users WHERE User='"+$('#user_lesson').html()+"'),
(SELECT _id FROM lessoninfo
WHERE Lesson="+lesson_no+" AND cast(starttime AS int)="+Math.floor(result_set.rows.item(markerCount-1).starttime)+")
WHERE NOT EXISTS (
SELECT users_id,lessoninfo_id from bookmarks
WHERE users_id=(SELECT _id FROM Users
WHERE User='"+$('#user_lesson').html()+"') AND lessoninfo_id=(
SELECT _id FROM lessoninfo
WHERE Lesson="+lesson_no+")))
This gives an error saying:
db error near where syntax.
If you never want to have duplicates, you should declare this as a table constraint:
CREATE TABLE bookmarks(
users_id INTEGER,
lessoninfo_id INTEGER,
UNIQUE(users_id, lessoninfo_id)
);
(A primary key over both columns would have the same effect.)
It is then possible to tell the database that you want to silently ignore records that would violate such a constraint:
INSERT OR IGNORE INTO bookmarks(users_id, lessoninfo_id) VALUES(123, 456)
If you have a table called memos that has two columns id and text you should be able to do like this:
INSERT INTO memos(id,text)
SELECT 5, 'text to insert'
WHERE NOT EXISTS(SELECT 1 FROM memos WHERE id = 5 AND text = 'text to insert');
If a record already contains a row where text is equal to 'text to insert' and id is equal to 5, then the insert operation will be ignored.
I don't know if this will work for your particular query, but perhaps it give you a hint on how to proceed.
I would advice that you instead design your table so that no duplicates are allowed as explained in #CLs answer below.
For a unique column, use this:
INSERT OR REPLACE INTO tableName (...) values(...);
For more information, see: sqlite.org/lang_insert
insert into bookmarks (users_id, lessoninfo_id)
select 1, 167
EXCEPT
select user_id, lessoninfo_id
from bookmarks
where user_id=1
and lessoninfo_id=167;
This is the fastest way.
For some other SQL engines, you can use a Dummy table containing 1 record.
e.g:
select 1, 167 from ONE_RECORD_DUMMY_TABLE

MySQL inserting data while only knowing 1 column name

I am wanting to insert some data into a MySQL table, these are the columns:
uuid | id_1 | id_41
the "id_1" and "id_41" could be anything, all I know is the primary key (uuid) and I am wanting to be able to insert into the table while only knowing the uuid column value as I am doing this so far:
PreparedStatement newPlayer = "INSERT INTO `test` values(?);";
newPlayer.setString(1, event.getPlayer().getUniqueId().toString());
But when I test it, it doesn't add to the table and does not produce any errors. I also know that all of the other values have a default value of 0
If you want to add a row without all columns included, you need to specify the column's name
INSERT INTO `test` (`uuid`) values(?);
Simple tell to insert the column you want insert eg for uuid
INSERT INTO `test` ( `uuid`) values(?);

Cassandra time stamp returns 0 rows

I created table column datatype as timestamp while storing it stores the default timestamp format yyyy-mm-dd HH:mm:ssZbut when try to select based on timestamp it doesn't return record.
CREATE TABLE TEST
(
TS timestamp,
VALUE text,
EMAILID text,
PRIMARY KEY (TS,VALUE)
);
INSERT INTO TEST(TS,VALUE,EMAILID) VALUES('1418026180922','sometext','email#');
SELECT * FROM TEST WHERE TS='2014-12-08 00:38:10-0800' ALLOW FILTERING;
THIS Query returns 0 rows? Why it returns like that am i doing something wrong?
It works for below query:
SELECT * FROM TEST WHERE TS='1418026180922' ALLOW FILTERING;
Your query
SELECT * FROM TEST WHERE TS='2014-12-08 00:38:10-0800' ALLOW FILTERING;
discards (or rather ignores) the millisecond part of the timestamp you use to insert the row
1418026180922
// ^^^^
And so the dates aren't equal.
If you had instead inserted
INSERT INTO TEST(TS,VALUE,EMAILID) VALUES('14180261800000','sometext','email#');
you could retrieve it with
select * from test where ts='2014-12-08 00:09:40-0800'; // note that I've fixed it from your '2014-12-08 00:38:10-0800'

Apache Derby: Achieving 'SELECT INTO' behaviour

It is possible in MS SQL Server to store the results of query into a table, and most importantly, have the query create the table:
SELECT an_existing_column
INTO table_xyz
FROM an_existing_table
This is also possible in MySQL using:
CREATE TABLE table_xyz
SELECT an_existing_column
FROM an_existing_table
I have searched the Apache Derby Reference Guide and cannot see a method for achieving similar behaviour.
Does anyone know if this possible in Apache Derby?
Store the results of a query into a table:
INSERT INTO table_xyz (an_existing_column) SELECT an_existing_column FROM an_existing_table;
Create a table from another table:
All Columns:
CREATE TABLE table_xyz AS SELECT * FROM an_existing_table WITH NO DATA;
Specific Column:
CREATE TABLE table_xyz AS SELECT an_existing_column FROM an_existing_table WITH NO DATA;
It does not work in JAVA DB, the correct way to do it is:
For all columns:
Step 1: Create a new table with a different name. for example, my_new_table:
CREATE TABLE my_new_table AS SELECT * FROM original_table WITH NO DATA;
This statement creates a new table from original table in the same format and no data copied. It is required to specify WITH NO DATA for it creates a new table with the same columns.
Step 2: Copy data from orig_table to my_new_table using INSERT INTO.
INSERT INTO my_new_table SELECT * FROM orig_table.
Then you will have all the data copied.

Get value of index using PostgreSQL

I have one database that has two tables.
One table (Table A) has 3 columns:
idpms serial NOT NULL,
iduser integer NOT NULL,
iduser2 integer NOT NULL,
And the other (Table B) 3 columns
idmessage serial NOT NULL,
idpms integer NOT NULL,
text character varying(255),
I'm using Java to manage my database.
First, table A is updated with the values of user one and user two.
With the idpms values generated when I insert this data, Table B is updated with idpms and text
I have two questions:
How can I retrieve the value of idpms after inserting data in Table A?
In Java, can I somehow extract the idpms and cast it to an int?
Q1 :Yes.
Q2 :Yes.
Serial is just autoincrementing integer
There is no special command to get the value after insert you can use sql queries for it.
If you want to get value before insert then This article discusses about it I hope you get what you want
insert into t (iduser, iduser2) values (1,2)
returning idpms
I think you are looking for a KeyHolder object.
Assuming you are using Spring to perform database operations:
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.KeyHolder;
// stuff...
KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
// Execute the update
namedJdbcTemplate.update(sqlQuery, params, generatedKeyHolder);
// Retrieve the map of ALL the autoincremented fields in the table
// Just get the desired one by field name
Integer idpms = (Integer) generatedKeyHolder.getKeys().get("idpms");
// use idbpms as parameter for your next query...
hope that helps

Categories

Resources