How to create a table in HBASE using Java API - java

I want to create a table in HBASE using Java
I found this example to create a table in HBASE
https://www.tutorialspoint.com/es/hbase/hbase_create_table.htm
but these HTableDescriptor and HColumnDescriptor classes appear to me as deprecated and I don't know what alternative I can use so that the deprecated message does not appear
Connection connexion = ConnectionFactory.createConnection(config);
Admin admin = connexion.getAdmin();
////CREATE TABLE
// Instantiating configuration class
//Configuration con = HBaseConfiguration.create();
// Instantiating HbaseAdmin class
//HBaseAdmin admin = new HBaseAdmin(config);
// Instantiating table descriptor class
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("emp"));
// Adding column families to table descriptor
tableDescriptor.addFamily(new HColumnDescriptor("personal"));
tableDescriptor.addFamily(new HColumnDescriptor("professional"));
// Execute the table through admin
admin.createTable(tableDescriptor);
System.out.println(" Table created ");
////////////
How can I create a table in Hbase with the Java api, please help with some code or documentation
I am working with java 1.8.0.112
and the project dependencies are
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>2.4.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>

In the more recent versions of the Java API, you can use the class TableDescriptorBuilder and related classes.
Try something like this:
Connection conn; // get the HBase connection as you usually do
Admin admin = conn.getAdmin();
TableDescriptorBuilder tBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(TABLE_NAME));
ColumnFamilyDescriptor CFD = ColumnFamilyDescriptorBuilder.newBuilder(COL_FAMILY_NAME).build();
TableDescriptor tDesc = tBuilder.setColumnFamily(CFD).build();
admin.createTable(tDesc);

Connection connexion = ConnectionFactory.createConnection(config);
Admin admin = connexion.getAdmin();
byte[] family = Bytes.toBytes("cf");
TableName tname;
tname = TableName.valueOf("MONITOR:ZEMP");
TableDescriptorBuilder tableDescBuilder = TableDescriptorBuilder.newBuilder(tname);
ColumnFamilyDescriptorBuilder columnDescBuilder = ColumnFamilyDescriptorBuilder
.newBuilder(Bytes.toBytes(ByteBuffer.wrap(family)))
.setCompressionType(Compression.Algorithm.SNAPPY)
.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
tableDescBuilder.setColumnFamily(columnDescBuilder.build());
if (!admin.tableExists(tname)) {
TableDescriptor desc = tableDescBuilder.build();
admin.createTable(desc);
System.out.println(" Table created ");
}
else {
System.out.println(" Table already exists ");
}

Related

Hibernate 5 Second level Cache is not working , Still fetching from Database

Am planning to implement second level cache with Hibernate 5 .Entity class is annotated with #Cacheable and added strategy as CacheConcurrencyStrategy.READ_WRITE . but its still loooking for data in databse instead of cache .Logs shows 2 SQL queries.
Please see my main method
public static void main(String[] args) {
HolidayDAOImpl holidayImpl = new HolidayDAOImpl();
holidayImpl.setSessionFactory(HibernateUtil.INSTANCE.getSessionFactoryInstance());
holidayImpl.load().forEach(System.out :: println);
System.out.println("Loading second time");
holidayImpl.load().forEach(System.out::println);
}
Please see the HibernateUtil enum
public enum HibernateUtil{
INSTANCE;
public SessionFactory getSessionFactoryInstance(){
Properties properties = new Properties();
properties.setProperty(Environment.URL, "jdbc:mysql://dummy");
properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect");
properties.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
properties.setProperty(Environment.USER, "demo");
properties.setProperty(Environment.PASS, "demo");
//second level cache prop
properties.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
properties.setProperty(Environment.USE_QUERY_CACHE, "true");
properties.setProperty(Environment.CACHE_REGION_FACTORY, "org.hibernate.cache.ehcache.EhCacheRegionFactory");
//logging
properties.setProperty("hibernate.show_sql","true");
properties.setProperty("hibernate.format_sql","true");
Configuration cfg = new Configuration();
cfg.setProperties(properties);
cfg.addAnnotatedClass(Holidays.class);
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
Please see the DAOImpl class
public List<Holidays> load() {
try (Session session = sessionFactory.openSession()) {
//List<Holidays> result = session.createQuery("from Holidays", Holidays.class).getResultList();
Criteria criteria = session.createCriteria(Holidays.class);
criteria.add(Restrictions.like("holiday_name", "%QA%"));
return criteria.list();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
pom.xml
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.4.1.Final</version>
</dependency>
</dependencies>
While going through some of the stack overflow answers, could understand that in latest version of Hibernate (after version 4), there are some changes in configurations. Not sure i made any mistake in the configurations.
Could anyone please look into it, and figure out why its still looking for database instead of cache?
To use hibernate query cache, you must add setCacheable (true) in each query.
If you add criteria.setCacheable (true); to the DAOImpl class, the second query result comes from the cache.

Java, Maven, connect SQL , no suitable driver

I am building a project using Netbeans IED, with java. The project is using maven and I am attempting to connect it to sql database which I am having issue's. The code works in java but not with maven.
Here the error:
No suitable driver found for jdbc:derby://localhost:1527/Database
Java Code:
public class DatabaseTest {
public static Connection ConnectionObj = null;
public static Statement SqlStatement = null;
public static ResultSet Sqlresult = null;
public static ResultSetMetaData MetaData = null;
public static String query = "Select * from Wallet";
public static String url = "jdbc:derby://localhost:1527/Database";
public static String user = "ABM";
public static String pass = "password2";
public static void main(String[] args) {
try {
//Allows you to connect the database
ConnectionObj = DriverManager.getConnection(url, user, pass);
SqlStatement = ConnectionObj.createStatement();
Sqlresult = SqlStatement.executeQuery(query);
MetaData = Sqlresult.getMetaData();
System.out.println("Connection worked");
} catch (SQLException e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
Prom depency's:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.1.0</version>
</dependency>
https://gyazo.com/8937aada3bd4a8f5b108b5dc9b386dd7
This part of your POM file is incorrect:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
Your program is attempting to use JDBC to connect to a Derby database, so you should be using a Derby JDBC driver, not a MySQL JDBC driver.
Replace the above with the following:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.14.1.0</version>
</dependency>
(Use the same version as your main Derby version ...)
The code works in Java but not with Maven.
Curious. Perhaps you are setting the runtime classpath correctly in the Java case.

Hibernate datasource connection with JNDI

I can get connection using JDBC
OracleDataSource ds = (OracleDataSource) Class.forName("oracle.jdbc.pool.OracleConnectionPoolDataSource").newInstance();
//...
Connection connection = ds.getConnection("USER", "PASSWORD");
But I can't do it for Hibernate using hibernate.connection.datasource and jni:
I tried different variants, but it does not work.
String var1 = "oracle.jdbc.pool.OracleConnectionPoolDataSource";
String var2 = "java:/oracle.jdbc.pool.OracleConnectionPoolDataSource";
String var3 = "java:/oracle/jdbc/pool/OracleConnectionPoolDataSource";
String var4 = "java:/OracleConnectionPoolDataSource";
configuration.setProperty("hibernate.connection.datasource",var1);
All variants throw exceptions when it tries to build SessionFactory:
private static SessionFactory createSessionFactory(Configuration configuration) {
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = builder.build();
return configuration.buildSessionFactory(serviceRegistry);
}
Exceptions like this:
Exception in thread "main" org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [oracle.jdbc.pool.OracleConnectionPoolDataSource]
maven dependecies for it:
<dependency>
<groupId>local.ora9iDriver</groupId>
<artifactId>ora9iDriver</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:/Users/micah/.m2/repository/local/ora9iDriver/oraDriver/1.0/ora9i.jar</systemPath>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>9.0.2.0.0</version>
</dependency>
Can you check out the UCP with Hibernate blog and get some pointers?

How to connect to Neo4j 3.0 database with neo4j-jdbc?

Hi,
I've created a maven project in eclipse and added the dependency:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc</artifactId>
<version>3.0</version>
</dependency>
I'm just trying to get a connection to my db working so that I can move on to integrate the connection with my main project but I'm having trouble getting things off the ground.
I've used the code example given on the official repo:
import org.neo4j.jdbc.Connection;
import org.neo4j.jdbc.PreparedStatement;
import org.neo4j.jdbc.ResultSet;
public class Neo4jConnectionTest {
public static void main(String[] args) {
// Connect
Connection con = DriverManager.getConnection(
"jdbc:neo4j:bolt://localhost");
// Querying
String query = "MATCH (u:User)-[:FRIEND]-(f:User)
WHERE u.name = {1}
RETURN f.name, f.age";
try {
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1,"John");
ResultSet rs = con.execute();
while (rs.next()) {
System.out.println(
"Friend: "+rs.getString("f.name")+" is "+rs.getInt("f.age"));
}
} catch (Exception e) { e.printStackTrace(); }
con.close();
}
}
I am unable to compile this as:
DriverManager cannot be resolved within the neo4j-jdbc-3.0,
Prepared stmt = con.prepareStatement(query); causes a type mismatch,
and con.execute() is undefined for type org.neo4j.jdbc.Connection
I'd greatly appreciate any advice and expertise on the matter, thanks.
By changing to:
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.0.4</version>
</dependency>
I was able to successfully connect to the Neo4j 3.0 server using an example from Neo4j's site:
public static void main(String[] args) {
Driver driver = GraphDatabase.driver(
"bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
Session session = driver.session();
session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );
StatementResult result = session.run(
"MATCH (a:Person)
WHERE a.name = 'Arthur'
RETURN a.name AS name, a.title AS title");
while ( result.hasNext() ) {
Record record = result.next();
System.out.println( record.get( "title" ).asString() +
" " + record.get("name").asString() );
}
session.close();
driver.close();
}
I thought I'd share as this worked instantly with no hassle.
Neo4j Language Guides
DriverManager, Connection, PreparedStatement and ResultSet are all classes or interfaces from the java.sql package, which is part of the JDK. I guess the documentation assumes you'll use an IDE which will find the correct import for you.
It's the point of using the JDBC driver: you use the JDBC API, not a proprietary one (i.e. in a vendor package).
Update
There was a typo in the neo4j-jdbc readme, it should have read
ResultSet rs = stmt.execute();
instead of
ResultSet rs = con.execute();
otherwise the PreparedStatement has not use (and the code doesn't compile because there's no no-arg overload of Connection.execute()).
Update 2
The documented dependency was also wrong, as neo4j-jdbc does not contain any driver.
You should depend on:
either the all-in-one module, which gives you the opportunity to use the Bolt or HTTP protocols:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.0</version>
</dependency>
or the module for a specific protocol:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-bolt</artifactId>
<version>3.0</version>
</dependency>
<!-- or -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-http</artifactId>
<version>3.0</version>
</dependency>
Update 3
Both problems have now been fixed in the documentation of the project.

Getting error retrieving data from column family using cassandra-jdbc dreiver

when i am retrieving data from column family using Cssandra-JDBC driver. i got error
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cassandra.thrift.Cassandra$Client.execute_cql3_query(Ljava/nio/ByteBuffer;Lorg/apache/cassandra/thrift/Compression;Lorg/apache/cassandra/thrift/ConsistencyLevel;)Lorg/apache/cassandra/thrift/CqlResult;
at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:447)
at org.apache.cassandra.cql.jdbc.CassandraConnection.execute(CassandraConnection.java:472)
at org.apache.cassandra.cql.jdbc.CassandraStatement.doExecute(CassandraStatement.java:161)
at org.apache.cassandra.cql.jdbc.CassandraStatement.executeQuery(CassandraStatement.java:226)
at CassandraJDBCTest.main(CassandraJDBCTest.java:19)
Code is
public static void main (String args[]) throws SQLException{
try {
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
Connection con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/TestExample");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Name,Age FROM Users WHERE keyname='001';");
rs.next();
System.out.println(rs.getString("Name"));
System.out.println(rs.getInt(2));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You have missing dependencies:
Exception in thread "main" java.lang.NoSuchMethodError
I have a feeling that the classpath is just not configured correctly (why the L's?):
Ljava/nio/ByteBuffer;
Lorg/apache/cassandra/thrift/Compression;
Lorg/apache/cassandra/thrift/ConsistencyLevel;
...
If you want to save yourself of dependency hell try using maven, the Datastax java driver has a maven central repo and you just need to include the dependency:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>1.0.2</version>
</dependency>
EDIT
Sorry, didnt realise you are using JDBC. The cassandra jdbc driver has also got a maven repository:
<dependency>
<groupId>org.apache-extras.cassandra-jdbc</groupId>
<artifactId>cassandra-jdbc</artifactId>
<version>1.2.5</version>
</dependency>

Categories