Signature
ascii(string str)
Convert the String first character to numeric ASCII value.
hive> SELECT ascii('ABC');
OK
_c0
65
Time taken: 0.039 seconds, Fetched: 1 row(s)
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Signature
ascii(string str)
Convert the String first character to numeric ASCII value.
hive> SELECT ascii('ABC');
OK
_c0
65
Time taken: 0.039 seconds, Fetched: 1 row(s)
By annotating a byte array with @Lob annotation, we can map a byte array to BLOB.
Example
@Lob
private byte[] arr1;
Find the below working application.
Step 1: Create new maven project ‘hibernate-byte-array-to-blob-demo’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>hibernate-byte-array-to-blob-demo</artifactId>
<version>1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>15</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.2.Final</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
Step 3: Define Demo class.
Demo.java
package com.sample.app.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
@Entity
@Table(name = "demo")
public class Demo {
@Id
private int id;
@Lob
private byte[] arr1;
public Demo(int id, byte[] arr1) {
this.id = id;
this.arr1 = arr1;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getArr1() {
return arr1;
}
public void setArr1(byte[] arr1) {
this.arr1 = arr1;
}
}
Step 4: Create hibernate.cfg.xml file under src/main/resources folder.
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test</property>
<property name="connection.username">admin</property>
<property name="connection.password">admin</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>
<!-- dbcp connection pool configuration -->
<property name="hibernate.dbcp.initialSize">2</property>
<property name="hibernate.dbcp.maxTotal">10</property>
<property name="hibernate.dbcp.maxIdle">5</property>
<property name="hibernate.dbcp.minIdle">3</property>
<property name="hibernate.dbcp.maxWaitMillis">-1</property>
<!-- mappings for annotated classes -->
<mapping class="com.sample.app.entity.Demo" />
</session-factory>
</hibernate-configuration>
Step 5: Define main application class.
App.java
package com.sample.app;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import com.sample.app.entity.Demo;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
public class App {
private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
final StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build();
final Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();
return metaData.getSessionFactoryBuilder().build();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
private static <T> List<T> loadAllData(Class<T> clazz, Session session) {
final CriteriaBuilder builder = session.getCriteriaBuilder();
final CriteriaQuery<T> criteria = builder.createQuery(clazz);
criteria.from(clazz);
return session.createQuery(criteria).getResultList();
}
public static void main(final String args[]) throws IOException, SQLException {
String msg = "Hello World";
byte[] data = msg.getBytes();
Demo e1 = new Demo(1, data);
try (final Session session = SESSION_FACTORY.openSession()) {
session.beginTransaction();
session.persist(e1);
session.flush();
session.getTransaction().commit();
List<Demo> records = loadAllData(Demo.class, session);
for (Demo record : records) {
int id = record.getId();
System.out.println("id : " + id);
byte[] arr1 = record.getArr1();
System.out.println("arr1 : " + new String(arr1));
}
}
}
}
Total project structure looks like below.
Run App.java, you will see below messages in the console.
Aug 17, 2022 3:22:48 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.1.2.Final
Aug 17, 2022 3:22:48 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
Aug 17, 2022 3:22:48 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: org.h2.Driver
Aug 17, 2022 3:22:48 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:h2:mem:test]
Aug 17, 2022 3:22:48 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=admin}
Aug 17, 2022 3:22:48 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Aug 17, 2022 3:22:48 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 1 (min=1)
Aug 17, 2022 3:22:49 PM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Aug 17, 2022 3:22:49 PM org.hibernate.metamodel.internal.EntityInstantiatorPojoStandard resolveConstructor
INFO: HHH000182: No default (no-argument) constructor for class: com.sample.app.entity.Demo (class must be instantiated by Interceptor)
Hibernate:
drop table if exists demo cascade
Aug 17, 2022 3:22:49 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@2ab26378] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate:
create table demo (
id integer not null,
arr1 blob,
primary key (id)
)
Aug 17, 2022 3:22:49 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@5115f590] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate:
insert
into
demo
(arr1, id)
values
(?, ?)
Hibernate:
select
d1_0.id,
d1_0.arr1
from
demo d1_0
id : 1
arr1 : Hello World
You can download complete working application from this link.
Below table summarizes different string functions supported by Hive.
|
Function |
Description |
Example |
|
Convert the String first character to numeric ASCII value. |
hive> SELECT ascii('ABC'); OK _c0 65 Time taken: 0.039 seconds, Fetched: 1 row(s) |
|
|
Concatenate strings |
hive> SELECT concat('Hello',' ', 'World'); OK _c0 Hello World Time taken: 0.045 seconds, Fetched: 1 row(s) |
|
|
Concatenate strings using given delimiter. |
hive> SELECT concat_ws('-','Hello','World','Program'); OK _c0 Hello-World-Program Time taken: 0.041 seconds, Fetched: 1 row(s) |
|
|
find_in_set(string search_string, string source_string_list) |
Search for the occurrence of a string in given string list. This method return 0 if no occurrence found |
hive> SELECT find_in_set('World','Hello,World,Program'); OK _c0 2 hive> ; hive> ; hive> SELECT find_in_set('World1','Hello,World,Program'); OK _c0 0 Time taken: 0.044 seconds, Fetched: 1 row(s) |
|
Return the length of a string. |
hive> SELECT length('Hello World'); OK _c0 11 Time taken: 0.047 seconds, Fetched: 1 row(s) |
|
|
Return the string in lowercase. |
hive> SELECT lcase('HEllo'); OK _c0 hello Time taken: 0.043 seconds, Fetched: 1 row(s) |
|
|
Return the string in lowercase. |
hive> SELECT lower('HEllo'); OK _c0 hello Time taken: 0.052 seconds, Fetched: 1 row(s) |
|
|
Pad the characters to the string at left side until the string reaches to given length |
hive> SELECT lpad('He', 5, 'X'); OK _c0 XXXHe Time taken: 0.043 seconds, Fetched: 1 row(s) |
|
|
Remove all the left side trailing spaces in the string. |
hive> SELECT concat(ltrim(' Hello '), 'world'); OK _c0 Hello world Time taken: 0.039 seconds, Fetched: 1 row(s) |
|
|
Repeat a string n times. |
hive> SELECT repeat('Hi', 5); OK _c0 HiHiHiHiHi Time taken: 0.049 seconds, Fetched: 1 row(s) |
|
|
Reverse the string |
hive> SELECT reverse('Hello'); OK _c0 olleH Time taken: 0.038 seconds, Fetched: 1 row(s) |
|
|
Pad the characters to the string at right side until the string reaches to given length |
hive> SELECT rpad('He', 5, 'X'); OK _c0 HeXXX Time taken: 0.04 seconds, Fetched: 1 row(s) |
|
|
Remove all the right side trailing spaces in the string. |
hive> SELECT concat(rtrim(' Hello '), 'world'); OK _c0 Helloworld Time taken: 0.049 seconds, Fetched: 1 row(s) |
|
|
Return ‘n’ number of spaces. |
hive> SELECT concat('Hello', space(10), 'world'); OK _c0 Hello world Time taken: 0.039 seconds, Fetched: 1 row(s) |
|
|
Split the string by a separator and return array of strings as output. |
hive> SELECT split('ram,23,india', ','); OK _c0 ["ram","23","india"] Time taken: 0.039 seconds, Fetched: 1 row(s) |
|
|
Return substring from start position to length number of characters. If the length is not specified, then it considers size of string as length. |
hive> SELECT substr('Hello World', 4, 5); OK _c0 lo Wo Time taken: 0.051 seconds, Fetched: 1 row(s) hive> ; hive> ; hive> SELECT substr('Hello World', 4); OK _c0 lo World Time taken: 0.046 seconds, Fetched: 1 row(s) |
|
|
substring (string sourceStr, int startPosition [,int length]) |
Same as substr |
hive> SELECT substring('Hello World', 4, 5); OK _c0 lo Wo Time taken: 0.039 seconds, Fetched: 1 row(s) hive> ; hive> SELECT substring('Hello World', 4); OK _c0 lo World Time taken: 0.038 seconds, Fetched: 1 row(s) |
|
Trim all trailing and leading spaces |
hive> SELECT concat(trim(' Hello '), 'World'); OK _c0 HelloWorld |
|
|
Return the string in upper case. |
hive> SELECT ucase('Hello'); OK _c0 HELLO Time taken: 0.04 seconds, Fetched: 1 row(s) |
|
|
Same as ucase. |
hive> SELECT upper('Hello'); OK _c0 HELLO Time taken: 0.048 seconds, Fetched: 1 row(s) |
Previous Next Home
Signature
weekofyear(string date)
hive> SELECT weekofyear('2021-01-05');
OK
_c0
1
Time taken: 0.04 seconds, Fetched: 1 row(s)
hive> SELECT weekofyear('2021-01-12');
OK
_c0
2
Time taken: 0.045 seconds, Fetched: 1 row(s)
Previous Next Home