Can I use phoenix to achieve the feature like SQL's duplicate key? - apache-phoenix

I want to use phoenix to insert data into Hbase like this:
INSERT INTO mytable (col1, col2) VALUES (?, ?) ON DUPLICATE KEY UPDATE col2 = col2 + ?
Is there any method to achieve this in phoenix?

Just an update, the feature has been released with Phoenix 4.9, the syntax being:
UPSERT INTO mytable (col1, col2) VALUES (?, ?) ON DUPLICATE KEY UPDATE col2 = col2 + ?;
UPSERT INTO mytable (col1, col2) VALUES (?, ?) ON DUPLICATE KEY IGNORE;

No. It's not yet available. See related JIRA for some initial discussion to support above usecase.
https://issues.apache.org/jira/browse/PHOENIX-6

Related

Insert data to DB2 table with autoincrement ID without defining column list

I have a DB2 table named TEST where the ID is autoincremented starting from 1.
Columns are ID, col1, col2, col3.
When I want to insert manually like this:
INSERT INTO TEST VALUES (1, 2, 3).
It throws an error that the number of columns in the table are not in line with the number of values I want to insert.
Then I need to specify the column list in the insert statement for this to work:
INSERT INTO TEST (col1, col2, col3) VALUES (1,2,3)
I there any other way to insert the data without specifying the column list? Similar to the first version.
Try this:
INSERT INTO TEST VALUES (DEFAULT, 1, 2, 3);

How to update multiple rows with on conflict clause?

I'm trying to do a bulk upsert (more than 50k rows per query) on a table with ON CONFLICT UPDATE clause.
Ex: INSERT INTO foo (col1, col2, col3) values (v1,v2,v3), (v4,v5,v6) ON CONFLICT (col1, col2) DO UPDATE SET col3 = EXCLUDED.col3;
Above's works just fine when (col1,col2) tuple is unique identifier to a row.
Happens that I need to the UPDATE affects multiple rows.
Ex: INSERT INTO foo (col1, col2, col3) values (v1,v2,v3), (v4,v5,v6) ON CONFLICT (col1) DO UPDATE SET col3 = EXCLUDED.col3;
I want the ON CONFLICT clause updates all rows where col1 = EXCLUDED.col1
But when I place a non-unique tuple on "ON CONFLICT" postgres result in this error: "There is no unique or exclusion constraint matching the ON CONFLICT specification"
There is a way to achieve the desired behavior?

Entity Framework: View exclusion without primary key

I am using SQL Server where I have designed a view to sum the results of two tables and I want the output to be a single table with the results. My query simplified is something like:
SELECT SUM(col1), col2, col3
FROM Table1
GROUP BY col2, col3
This gives me the data I want, but when updating my EDM the view is excluded because "a primary key cannot be inferred".
With a little research I modified the query to spoof an id column to as follows:
SELECT ROW_NUMBER() OVER (ORDER BY col2) AS 'ID', SUM(col1), col2, col3
FROM Table1
GROUP BY col2, col3
This kind of query gives me a nice increasing set of ids. However, when I attempt to update my model it still excludes my view because it cannot infer a primary key. How can we use views that aggregate records and connect them with Linq-to-Entities?
As already discussed in the comments you can try adding MAX(id) as id to the view. Based on your feedback this would become:
SELECT ISNULL(MAX(id), 0) as ID,
SUM(col1),
col2,
col3
FROM Table1
GROUP BY col2, col3
Another option is to try creating an index on the view:
CREATE UNIQUE CLUSTERED INDEX idx_view1 ON dbo.View1(id)
I use this code alter view
ISNULL(ROW_NUMBER() OVER(ORDER BY ActionDate DESC), -1) AS RowID
I use this clause in multi relations view / table query
ROW_NUMBER never give null value because it never seen -1
This is all I needed to add in order to import my view into EF6.
select ISNULL(1, 1) keyField

PostgreSQL - INSERT INTO statement

What I'm trying to do is select various rows from a certain table and insert them right back into the same table. My problem is that I keep running into the whole "duplicate PK" error - is there a way to skip the PK field when executing an INSERT INTO statement in PostgreSQL?
For example:
INSERT INTO reviews SELECT * FROM reviews WHERE rev_id=14;
the rev_id in the preceding SQL is the PK key, which I somehow need to skip. (To clarify: I am using * in the SELECT statement because the number of table columns can increase dynamically).
So finally, is there any way to skip the PK field?
Thanks in advance.
You can insert only the values you want so your PK will get auto-incremented
insert into reviews (col1, col2, col3) select col1, col2, col3 from reviews where rev_id=14
Please do not retrieve/insert the id-column
insert into reviews (col0, col1, ...) select col0, col1, ... from reviews where rev_id=14;

select where not exists excluding identity column

I am inserting only new records that do not exist in a live table from a "dump" table. My issue is there is an identity column that I don't want to insert into the live, I want the live tables identity column to take care of incrementing the value but I am getting an insert error "Insert Error: Column name or number of supplied values does not match table definition." Is there a way around this or is the only fix to remove the identity column all together?
Thanks,
Sam
You need to list of all the needed columns in your query, excluding the identity column.
One more reason why you should never use SELECT *.
INSERT liveTable
(col1, col2, col3)
SELECT col1, col2, col3
FROM dumpTable dt
WHERE NOT EXISTS
(
SELECT 1
FROM liveTable lt
WHERE lt.Id == dt.Id
)
Pro tip: You can also achieve the above by using an OUTER JOIN between the dump and live tables and using WHERE liveTable.col1 = NULL (you will probably need to qualify the column names selected with the dump table alias).
I figured out the issue.... my live table didn't have the ID field set as an identity, somehow when I created it that field wasn't set up correctly.
you can leave that column in your insert statment like this
insert into destination (col2, col3, col4)
select col2, col3 col4 from source
Don't do just
insert into destination
select * from source