I am new in Hsqldb database. I want to know how to take backup and restore of Hsqldb database through java code.
Use the BACKUP DATABASE TO command.
Here is a link to the documentation:
HSQLDB System Management Documentation
I haven't tested this, but I imagine it's something along the lines of:
String backup = "BACKUP DATABASE TO " + "'" + filePath + "' BLOCKING";
PreparedStatement preparedStatement = connection.prepareStatement(backup);
preparedStatement.execute();
You'll want to wrap it in a try-catch block of course.
As far as restoring the db goes, I think you have to perform that while the database is offline using the DbBackupMain application. So you would issue this command at the command line:
java -cp hsqldb.jar org.hsqldb.lib.tar.DbBackupMain --extract tardir/backup.tar dbdir
Each HyperSQL database is called a catalog. There are three types of catalog depending on how the data is stored.
Types of catalog data :
mem: stored entirely in RAM - without any persistence beyond the JVM process's life
file: stored in filesystem files
res: stored in a Java resource, such as a Jar and always read-only
To back up a running catalog, obtain a JDBC connection and issue a BACKUP DATABASE command in SQL. In its most simple form, the command format below will backup the database as a single .tar.gz file to the given directory.
BACKUP DATABASE TO <directory name> BLOCKING [ AS FILES ]
The directory name must end with a slash to distinguish it as a directory, and the whole string must be in single quotes like so: 'subdir/nesteddir/'.
To back up an offline catalog, the catalog must be in shut down state. You will run a Java command like
java -cp hsqldb.jar org.hsqldb.lib.tar.DbBackupMain --save tardir/backup.tar dbdir/dbname
. In this example, the database is named dbname and is in the dbdir directory. The backup is saved to a file named backup.tar in the tardir directory.
where tardir/backup.tar is a file path to the *.tar or *.tar.gz file to be created in your file system, and dbdir/dbname is the file path to the catalog file base name.
You use DbBackup on your operating system command line to restore a catalog from a backup.
java -cp hsqldb.jar org.hsqldb.lib.tar.DbBackupMain --extract tardir/backup.tar dbdir
where tardir/backup.tar is a file path to the *.tar or *.tar.gz file to be read, and dbdir is the target directory to extract the catalog files into. Note that dbdir specifies a directory path, without the catalog file base name. The files will be created with the names stored in the tar file.
For more details refer
So in java + SPring + JdbcTemplate
Backup (On-line):
#Autowired
public JdbcTemplate jdbcTemplate;
public void mainBackupAndRestore() throws IOException {
...
jdbcTemplate.execute("BACKUP DATABASE TO '" + sourceFile.getAbsolutePath() + "' BLOCKING");
}
This will save .properties, .scripts and .lobs file to a tar in sourceFile.getAbsolutePath().
Restore:
DbBackupMain.main(new String[] { "--extract", baseDir.getAbsolutePath(),
System.getProperty("user.home") + "/restoreFolder" });
This will get files from baseDir.getAbsolutePath() and will put them in userHome/restoreFolder where you can check if all restore is OK.
lobs contains lob/blob data, scripts contains executed queries.
Related
i am trying to add jar on the hive shell. I am aware of the global option on the server but my requirement is to add them per session on the hive shell.
I have used this class for the hdfs dfs commands to add the jars to the hdfs file system
This is what i have tried:
Created a folder on the hdfs /tmp
Add the file to hdfs filesystem using FileSystem.copyFromLocalFile method
(equivalent to the hdfs dfs -put myjar.jar /tmp
Set permissions on the file on fhe fs file system
Check that the jar was loaded to hdfs using the getFileSystem method
List files on the fs FileSystem using listFiles to confirm the jars are there.
This works and I have the jars loaded to hdfs but i cannot add jars to the hive session
When i am trying to add it in the hive shell, i am doing the following:
statement = setStmt(createStatement(getConnection()));
query = "add jar " + path;
statement.execute(query);
I am getting this error [For example path of /tmp/myjar.jar]:
Error while processing statement: /tmp/myjar.jar does not exist
Other permutations on the path such as
query = "add jar hdfs://<host>:<port>" + path;
query = "add jar <host>:<port>" + path;
results with an error.
command to list jars works (with no results)
query = "list jars";
ResultSet rs = statement.executeQuery(query);
I managed to solve this issue
The process failed because of the configuration of the FileSystem.
This object is where we upload the jars to, before adding them on the session.
This is how you init the FileSystem
FileSystem fs = FileSystem.newInstance(conf);
The object conf should have the properties of the hive server.
In order for the process to work, I needed to set the following parameter on the Configuration property
conf.set("fs.defaultFS", hdfsDstStr);
How do I insert a file that resides in windows in c:\temp\sample.txt
I have tried
insert into lob_file VALUES (5, pg_read_file('c://temp//sample.txt')::BYTEA);
and got
ERROR: could not start file c://temp//sample.txt. No such file or
directory.
Is data base running locally or on a Windows server and you have moved the file to the server. See Docs
The functions shown in Table 9.94 provide native access to files on
the machine hosting the server. Only files within the database cluster
directory and the log_directory can be accessed unless the user is
granted the role pg_read_server_files. Use a relative path for files
in the cluster directory, and a path matching the log_directory
configuration setting for log files.
I'm using an SQLite database and I want to open a .db file from within a Jimfs virtual file system. Using the following code I can import a file into the virtual file system:
String databaseFilePath = "...";
Configuration configuration = Configuration.unix();
FileSystem fileSystem = Jimfs.newFileSystem(configuration);
Path targetDirectory = fileSystem.getPath("/");
Files.copy(Paths.get(databaseFilePath), targetDirectory);
Next, when I try to open the database file, I'm running into problems:
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFileName);
I cannot use Strings since the virtual file can only be referenced using the Path object. How do I open a database connection using Paths?
SQLite works on 'real' files.
To be able to store data elsewhere, you have to implement your own SQLite VFS. (This is not supported by every JDBC driver.)
When using the DistributedCache in Hadoop, I manage to push the files from hdfs in the driver class like this:
FileSystem fileSystem = FileSystem.get(getConf());
DistributedCache.createSymlink(conf);
DistributedCache.addCacheFile(fileSystem.getUri().resolve("/dumps" + "#" + "file.txt"), job.getConfiguration());
Then, to read the file, in the setup() of Mapper I do:
Path localPaths[] = context.getLocalCacheFiles();
The file is located in the cache, under a path /tmp/solr-map-reduce/yarn-local-dirs/usercache/user/appcache/application_1398146231614_0045/container_1398146231614_0045_01_000004/file.txt. But when I read it, I get IOException: file is a directory.
How can one go about solving this?
I have a SQL script that contains the following
LOAD DATA LOCAL INFILE '.\\datafiles\\customers.txt' INTO TABLE Customers`
I open and run it in MySQL workbench and I get following error:
Error Code: 2. File '.\datafiles\customers.txt' not found
(Errcode: 2 - No such file or directory)
Where should I put file customers.txt according '.\datafiles\customers.txt'?
I use windows 7 and I tried the following placse:
C:\datafiles
C:\temp\datafiles
C:\tem\datafiles
C:\Users\michael\datafiles
I think \\ is incorrect. I Always use full path.
So if the file is in C:\datafiles\ use
LOAD DATA LOCAL INFILE 'C:\datafiles\customers.txt'
INTO TABLE Customers LINES TERMINATED BY '\r\n';
Use absolute path with slash as a separator like this:
'C/your_folder/.../datafiles/customers.txt'