getting latest inserted object from riak timeseries - riak-ts

Procedure to get latest record from Riak TS based on timestamp.
I created following table:
CREATE TABLE devicedetail (memberId SINT64 NOT NULL, type VARCHAR NOT NULL,model VARCHAR NOT NULL,imei VARCHAR NOT NULL,deviceId VARCHAR NOT NULL,manufacturer VARCHAR NOT NULL,os VARCHAR NOT NULL,version VARCHAR NOT NULL,time TIMESTAMP NOT NULL,PRIMARY KEY ((memberId),memberId));
Insert data using SQL:
INSERT INTO devicedetail VALUES (110,'health Kit','andorid-4.0','76565657675222','sdgsd1212','sony','windows','5.0',1420113600000);
and now I want to select deviceId based on latest inserted record:
select deviceId from devicedetail where memberId=110 and type='health Kit' order by time desc limit 1;
I am getting Exception like:
SQL Parser error {<<"order">>,riak_ql_parser,
["syntax error before: ","identifier"]}
but while querying:
select deviceId from devicedetail where memberId=110 and type='health Kit';
Getting Output as:
+---------+
|deviceId |
+---------+
|sdgsd1212|
+---------+
Anyone knows how we get this in a simple way? Please tell me.

Answer as of December 21, 2016:
Riak TS 1.5 was released this week. ORDER BY and LIMIT are now supported.
Old Answer:
ORDER BY is not supported in the current version of Riak TS (1.4) which is why you are getting the error. LIMIT is also not yet supported. ORDER BY and LIMIT will be included in the near future.

Related

Data move from table to table , column type error

I'm using Postgresql with timescaledb extension, pg13 - tsdb 2.2.1
table 1
create table user
(
...
join_dt timestamptz NULL,
...
);
Table 2
create table a_user
(
...
join_dt timestamptz NULL,
...
);
I used SQL insert to a_user table so there's data inside the table, and want to move the data to user table by querying this :
insert into user
select * from a_user;
I get this error:
[42804] ERROR: column "join_dt" is of type timestamp with time zone but expression is of type character varying
The original data comes from a table which is like this
create table ori_user
(
...
join_dt timestamp(6) with time zone,
...
);
I export data as SQL insert form from ori_user, and then inserted into a_user, and now I want to move from a_user to user.
This is what I have tried:
insert into user select
...
join_dt::timestamptz,
...
from a_user;
does not work.
with aa as (select pg_typeof(join_dt)::varchar as type,* from a_user)
select * from aa where aa.type not like 'timestamp with time zone';
shows no row.
any other solutions? please help.
Thanks in advance.
I thought table columns are in same order, but it wasn't.
fix the order, problem solved.

Embedding Current Date in Hive Insert

I am trying to get the current date into a Hive database (version 0.13 running on an HDInsight cluster) with the following script
SET curdt = from_unixtime(unix_timestamp());
DROP TABLE IF EXISTS curtime_test;
CREATE TABLE curtime_test (
dateEntered STRING
);
INSERT INTO TABLE curtime_test
SELECT '${hivevar:curdt}' FROM hivesampletable limit 3;
SELECT * FROM curtime_test;
Note that I want to have the same insert date for all the inserted records, this is a toy example, but the real one I want to use it on has millions of records to insert. This version I tried above just inserts the string '${hivevar:curdt}' into the database, which is not what I want:
${hivevar:curdt}
${hivevar:curdt}
${hivevar:curdt}
Omitting the quotes causes the insert to error out because of the spaces in the string. How can I do this right?
Update:
Using the line
SELECT ${hiveconf:curdt} FROM hivesampletable limit 3;
as per the comment from Charlie Haley (I mixed up ${hivevar} and ${hiveconf}), gives me the results that I want. If he writes it up as an answer I will mark it as right.
The following code sample works for me. Does this solve your problem?
DROP TABLE IF EXISTS curtime_test;
CREATE TABLE curtime_test (
dateEntered STRING
);
INSERT INTO TABLE curtime_test
SELECT unix_timestamp() FROM hivesampletable limit 1;
SELECT * FROM curtime_test;

Get random records invariant on same day

I have the folowing SQL table:
Create table Post (
Id int primary key not null,
Title nvarchar (80) not null
)
I need to get 4 random rows from the table but they would always be the same for the same Day.
How can this be done?
In MS SQL Server, you can use RAND function with current day of the year as the seed
For a specified seed value, the result returned is always the same.
RAND(DATEPART(dayofyear, GETDATE()))

Sql timestamp for created/updated dates

In a few logging tables that are frequently written to, I'd like to be able to store a relative order so that I can union between these tables, and get the order that things actually occurred in.
DateTime2's resolution is lacking. Several rows will get the exact same date, so there is no way to tell which happened first.
Because sorting should work across several tables, sorting by Id is out.
Then I started looking at timestamp. This works for updated dates, but it does not work for created dates, because you can only have one timestamp column per table, and it automatically updates.
This is for Microsoft Sql Server 2008.
Any suggestions?
You can simulate it with another column typed as binary(8) (same as rowversion) and defaulting to ##DBTS:
create table TX (
ID int not null,
Updated rowversion not null,
Created binary(8) not null constraint DF_TX_Created DEFAULT (##DBTS)
)
go
insert into TX (ID)
values (1),(2)
go
update TX set ID = 3 where ID = 1
go
insert into TX (ID)
values (4)
go
select * from TX
Result:
ID Updated Created
----------- ------------------ ------------------
3 0x00000000000007D3 0x00000000000007D0
2 0x00000000000007D2 0x00000000000007D0
4 0x00000000000007D4 0x00000000000007D3
Notes:
The Created values will always be equal to the last rowversion value assigned, so they will "lag", in some sense, compared to Updated values.
Also, multiple inserts from a single statement will receive the same Created values, whereas Updated values will always be distinct.

Getting the identity value from a table with single identity column in FitNesse

Does FitNesse/dbFit support inserting into a table with a single identity column and then returning that value into a variable?
For example:
create table TestTable ( ID int identity(1,1) )
!|Insert|TestTable|
|ID? |
|>>TestID |
results in the following exception:
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ')'.
Is there another way to accomplish this?
Your problem is not with Fitnesse, but with the SQL engine. If you try the same query in SQL Server Management Studio you will receive the same error. Thus, given the restriction that you cannot use set identity_insert on the question you really need to ask is how to insert a record with no insertable fields in SQL Server independent of Fitnesse? This StackOverflow post provides a simple answer:
INSERT INTO #TempTable DEFAULT VALUES
Having that in hand, now the task is to map this to Fitnesse. Here is one solution:
!|execute|CREATE TABLE #TestTable ( ID int identity(1,1) )|
!|execute|INSERT INTO #TestTable DEFAULT VALUES|
!|query|SELECT TOP 1 ##IDENTITY [ID] FROM #TestTable|
|ID? |
|>>TestID |
Fitnesse's INSERT command does not support the default construct, so you must switch to the more general EXECUTE command to do the insert. That, however, does not return results so you cannot glean the auto-inserted ID value directly. The final query gives you one way to grab the just-inserted identity value.