To use embedded database I have created hsqldb database connected with java by this tutorial.
In general it is about creating simple table with 3 records through hsqldb manager and connect to this database with java.
Database was created and after exit the manager and connect again I gained my tables. They are recorded in test.script file.
If I try to connect with java by
connection = DriverManager.getConnection("jdbc:hsqldb:file:/db/test;ifexists=true", "SA", "");
then I got connection and can read all tables from it by
resultSet = statement.executeQuery("SELECT TABLE_NAME, COLUMN_NAME, TYPE_NAME, COLUMN_SIZE, DECIMAL_DIGITS, IS_NULLABLE FROM INFORMATION_SCHEMA.SYSTEM_COLUMNS WHERE TABLE_NAME NOT LIKE 'SYSTEM_%'");
But I can't get previous created table in manager even if script file contains that table.
This snippet of create table is placed in test.script file:
CREATE MEMORY TABLE PUBLIC.SALARYDETAILS(EMPID VARCHAR(6) PRIMARY KEY,SALARY INTEGER NOT NULL,BONUS INTEGER NOT NULL,INCREMENT INTEGER NOT NULL)
I was confused by MEMORY command, but maybe it is not what I should to fix. After remove it manager add it there again.
----- update 1 -----
SHUTDOWN command didn't help.
I don't know how HSQLDB store data, but thought that when they are in script file, it is done.
Exception what is see in the command line when running Java is
user lacks privilege or object not found
----- update 2 -----
I have created a table in Java and record data into it and I was available to get these data, but can't see it in the manager. It seems to me like different database, but location is same.
Script file doesn't contain new table and data. I don't know where data are stored.
It seems the database file was not saved. Before you exit the manager, execute this SQL command:
SHUTDOWN
Related
I've created the following table in Hive:
CREATE TABLE mytable (..columns...) PARTITIONED BY (load_date string) STORED AS ...
And I'm trying to insert data to my table with spark as follow:
Dataset<Row> dfSelect = df.withColumn("load_date","15_07_2018");
dfSelect.write().mode("append").partitionBy("load_date").save(path);
And also make the following configuration:
sqlContext().setConf("hive.exec.dynamic.partition","true");
sqlContext().setConf("hive.exec.dynamic.partition.mode","nonstrict");
And after I make the write command I see on HDFS the directory /myDbPath/load_date=15_07_2018, which contains the file that I've written but when I make query like:
show partitions mytable
or
select * from mytable where load_date="15_07_2018"
I get 0 records.
What happened and how can I fix this?
EDIT
If I run the following command in Hue:
msck repair table mytable
I solve the problem, how can I do it in my code?
Hive stores a list of partitions for each table in its metastore. If, however, new partitions are directly added to HDFS (say by using hadoop fs -put command (or) .save..etc), the metastore (and hence Hive) will not be aware of these partitions unless the user runs either of the below commands
Meta store check command (msck repair table)
msck repair table <db.name>.<table_name>;
(or)
ALTER TABLE table_name ADD PARTITION commands on each of the newly added partitions.
We can also add partition by using alter table statement by using this way we need to add each and every newly created partition to the table
alter table <db.name>.<table_name> add partition(load_date="15_07_2018") location <hdfs-location>;
Run either of the above statements and then check the data again for load_date="15_07_2018"
For more details refer these links add partitions and msck repair table
I want to write script for create database with tables in PostgreSQL.
I created deploy_db.bat:
#echo off
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -h localhost -p 5433 -U postgres -d postgres -f run_main.sql
pause
my run_main.sql:
BEGIN;
\i create_db.sql
\i tableA.sql
COMMIT;
create_db.sql:
CREATE DATABASE test;
DROP SCHEMA IF EXISTS test CASCADE;
CREATE SCHEMA test
AUTHORIZATION postgres;
tableA.sql:
CREATE TABLE test.tableA(
id serial PRIMARY KEY,
name text,
age INTEGER
);
So, I run deploy_db.bat and see:
BEGIN
psql:create_db.sql:1: ERROR: CREATE DATABASE cannot run inside a transaction block
psql:create_db.sql:3: ERROR: current transaction is aborted, commands ignored until end of transaction block
ROLLBACK
But, Why? How can resolved it?
You issued BEGIN which started a transaction, then CREATE DATABASE which produced the error message because you ran it inside the transaction.
You could just move the CREATE DATABASE statement before the BEGIN, that would get rid of the error message.
But reading your SQL script I suspect that you want to create the new schema and table in the newly created database, which is not what will happen with your script. Rather, the schema and the table will be created in the database postgres.
To change that, your script should look like this:
CREATE DATABASE test;
-- connect to that database
\connect test
-- now create your schema and your table
CREATE SCHEMA ...
CREATE TABLE ...
I am having a problem. I have a query that checks one database table and updates another database table. I am using MySQL 5.1
UPDATE dldd.temp,test.temp
SET test.temp.name = dldd.temp.word
WHERE dldd.temp.id = test.temp.id
this is my SQL statement that is working fine. Now I want to execute this statement using Java PreparedStatement . The problem is I don't know how to write the Connection String to select two database i.e
"jdbc:mysql://localhost:3306/"+dbname+"?characterEncoding=UTF-8"
What should come in place of dbname. Can I select multiple db there.
Have a look at http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html.
If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.
I'm trying to create a function in my java application, where the user could select a prior made backup but only import table-rows that aren't in the current database instance. With a MySql database I could dump my tables, rename them inside the .sql to create temporary tables when imported again, and then simply cross query all rows not in the DB. Any idea how I could acomplish something similar in hsqldb from within my java application?
You can do this:
open the backup database
create a text table that is a copy of the main table, e.g. CREATE TEXT TABLE yourtable_copy AS (SELECT * FROM yourtable)
set a file for the table SET TABLE yourtable_copy SOURCE 'filepath'
copy the data to the new table
set the source off with SET TABLE yourtable_copy SOURCE OFF
shutdown the backup database
open the main database
now do the same text table creation and source setting with the main database but do not copy the data, as the backup data is already there and will be opend
do your updates then turn the text source off in the main database
reference http://www.hsqldb.org/doc/2.0/guide/texttables-chapt.html
I'm currently using Java to access a .sql file (called patient.sql). Running queries and updating the table works well while the program is running, but the changes aren't made on disk.
So, for example, I have a 30 node database with some fields including caseID (primary key) and Hospital. I want to change the Hospital of the node with caseID = Case29. To do this, I use the following code:
// Prepare a statement to update a record
String sql = "UPDATE patient SET Hospital='CX' WHERE caseID = 'Case29'";
// Execute the insert statement
stmt.executeUpdate(sql);
I have checked this and seen that it works (using a quick System.out.println()). However, when I finish the program and open the patient.sql, my change has not been registered. How can I save this change made?
Cheers
EDIT: I'm using HSQLDB
If you are using HSQLDB changes are stored in a .log file until SHUTDOWN is called.
After a SHUTDOWN, all changes are moved to a .script file.
One description of HSQLDB files here:
http://hsqldb.org/doc/guide/ch01.html
In your case I suspect no SHUTDOWN has been called.