How to authenticate a user using Cassandra database - java

I want to create a way to authenticate users. In mysql I do:
SELECT user_id FROM customers WHERE `username` = 'the_username' AND `password` = 'the_password'
If the query doesn't return an empty resultset then user is authorized. Cassandra expects me to know the row key (user_id) upfront. How can I check all rows in a column family for existence of a username and password where I don't know the row key.
I am using Hector api to connect to Cassandra 1.2.2

Some possible solutions:
use the username as row key
create a secondary index on username
I would use the first one. If username is unique (as I assume), you don't need user_id

Related

Accessing Cassandra Filtering allowed need to be added

I have Cassandra Table as below
create table user(
id UUID PRIMARY KEY,
firstname varchar,
secondname varchar,
emailid varchar,
);
From Java - Spring Boot Im trying to access data
Optional<User> findByEmailid(String emailId);
I get error stating
It asks me to use "FILTERING ALLOWED" part of query. Is there anyway to enable this globally or I should change query/db structure?
That error is telling you that emailid is not a valid column that can be filtered on in your WHERE clause. Enabling ALLOW FILTERING or creating a secondary index on that column is one way to do this. But both of those are pretty terrible solutions (because of how Cassandra is works under-the-hood).
With Cassandra you need to take a query-based modeling approach. This means that sometimes (often) queries and tables share a 1:1 ratio. If you really need to query users by email address, then you will need to create a table to serve that query.
CREATE TABLE user_by_email(
id UUID,
firstname varchar,
secondname varchar,
emailid varchar PRIMARY KEY,
);
Then something like this will work:
Optional<UserByEmail> findByEmailid(String emailId);
And if you don't ever plan on querying the user table by id, then there really isn't a reason to use that column as your sole primary key.

creation of table (of table) using MySql

I am trying to create a server for POP3 protocol. I'm relatively new to mySql and would like to create a simple database for storing users (username and password) and their corresponding emails(some text, not email-ID).
Example: let's say user1 has username1 and password1. I would like to create another table which points to the user1 and contains his emails there.
My main purpose is to run a query using Java and access users (using their username and password) and their emails.
How can I do this?
Take a look at this answer to know how to store passwords in database.
You can have your design something like this:
users
- id int PK
- username varchar unique not null
- passwordhash varchar not null
user_emails
- id int PK
- user_id int not null FK -> users(id)
- email varchar not null
I am not going to write down DDL for you. Check out vendor documentation for create table syntax.

How to compare SQLite Column against a user string?

I am creating a JFrameApplet (In Java) with a log in (SQLite) but I am struggling to understand how to compare a wanted username, against a username that is already taken:
For Example: I want the username JoeBloggs, but it is taken, how do I compare a wanted username against one already made.
I have an SQLite users.db and the field is USERNAME.
Thank you for any assistance.
Try searching up the UNIQUE constraint.
When creating a table useCREATE TABLE Users(Id INTEGER, USERNAME TEXT UNIQUE);
so if JoeBloggs is already an entry in the db trying to add it again with INSERT INTO Users VALUES(2, 'JoeBloggs'); will give you an Error: column USERNAME is not unique.

sql-server jdbc4 drivers doesn't list all columns?

I have a (sql server) column which have a System Type but no Data Type (It is actually more then one). Using the Microsoft jdbc4 drivers and trying to list all the columns using metadata for this table this column with the missing Data type doesn't show up:
ResultSet rs = connection.getMetaData().getColumns(null,"schema","tableName", null);
while (rs.next()) {
String column = rs.getString("COLUMN_NAME");
System.out.println("column:" + column); // This will not print the column
}
If you use the odbc drivers, in for example C#, it does show up (The column with the missing Data Type). And if this column is a constraint it will show up when using the the jdbc api for listing all constraints:
ResultSet rs = connection.getMetaData().getIndexInfo(null,
"schema",
"tableName",
true/*list unique*/,
true);
while(rs.next()){
String column = rs.getString("COLUMN_NAME");
System.out.println("column:" + column); // This will print the column, if it is a unique constraint.
}
This behavior is the same regardless if you use jdbc4 drivers or the jtds drivers.
So my question is, if this is a bug or is it something that I'm missing? And Is it possible to list the meta data in an other way to get all the columns for table?
To create a Table which doesn't display a Data Type. This demands that you create a user which haven't read access to the user defined Data Types. This is how can reproduce it:
I found this code here
CREATE TYPE TEST_TYPE2 FROM [int] NOT NULL
GO
CREATE TABLE Customer
(
CustomerID INT IDENTITY(1,1) NOT NULL,
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
ZIP TEST_TYPE2 NOT NULL
)
GO
-- Create Table without UDDT
CREATE TABLE Customer_Orig
(
CustomerID INT IDENTITY(1,1) NOT NULL,
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
ZIP INT NOT NULL
)
GO
-- Create User with db_datareader to database
USE [master]
GO
CREATE LOGIN testUser WITH PASSWORD=N'THE_PASSWORD',
DEFAULT_DATABASE=[master],
CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE TEST_DATABASE
GO
CREATE USER testUser FOR LOGIN testUser
GO
USE TEST_DATABASE
GO
EXEC sp_addrolemember N'db_datareader', N'THE_PASSWORD'
GO
I understand why it doesn't show up, but I way does it work for the odbc drivers?

H2 sql how to set column to auto_increment

i am trying to set my ID to auto_increment, but it is not doing it. i saved 2 more data into db, all are still getting 0 id. as a result i am not able to set ID as primary key.
how is it possible? can i set the field to auto_increment with JPA annotation or so?
i even tried this command which is the first attempt:
alter table user alter column id int not null auto_increment;
no affect.
can someone help me please
thanks in advance!
If you just want to set ID as a primary key which is auto generated,then sql uniqueidentifier for it.
CREATE TABLE userTable(userId uniqueidentifier primary key, userName nvarchar(50))
--create a table having userId as a primary key
INSERT INTO userTable(userId, userName ) VALUES (NEWID(), 'mohit');
INSERT INTO userTable(userId, userName ) VALUES (NEWID(), 'doniyor');
SELECT * FROM userTable
Result will be:
userId userName
{E8E0A79D-436F-49CB-BCEC-EC9E5D69F1BB} mohit
{21081DFA-7DBB-46AF-A160-550631160C25} doniyor

Categories