Eclipse 中的 Kundera + Hbase 配置
在 linux 或其他设备上启动 Hbase 配置
创建动态网络项目
将以下jar添加到项目的库中
1)asm-4.0.jar
2)cglib-2.1.jar
3)commons-lang-2.5.jar
4)commons-logging-1.1.1.jar
5)hadoop-core-1.0.0.jar
6)hbase-0.94.4.jar
7)jts-1.11.jar
8)kundera-core-2.5.1.jar
9)kundera-hbase-2.5.jar
10)log4j-1.2.16.jar
11)lucene-core-3.5.0.jar
12)xstream-1.3.1.jar
13)zookeeper-3.3.2.jar
添加persistence.xml文件如下
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="hbase_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>com.fvrl.MyObject</class>
<properties>
<property name="kundera.nodes" value="your host ip"/>
<property name="kundera.port" value="port"/>
<property name="kundera.keyspace" value="KunderaExamples"/>
<property name="kundera.dialect" value="hbase"/>
<property name="kundera.client.lookup.class" value="com.impetus.client.hbase.HBaseClientFactory" />
<property name="kundera.client.property" value="yourxmlfilepath" />
<property name="kundera.ddl.auto.prepare" value="update" />
</properties>
</persistence-unit>
</persistence>
以上 xml 文件路径必须放在适当的位置。
使您的实体类如下
@Entity
@Table(name = "MyObject", schema = "KunderaExamples@hbase_pu")
@NamedQueries({
@NamedQuery(name="findAll", query="select c from MyObject c")
})
public class MyObject
{
@Id
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getSecondname() {
return secondname;
}
public void setSecondname(String secondname) {
this.secondname = secondname;
}
private String firstname;
private String secondname;
}
通过 main 方法运行您的项目
public static void main(String[] args) {
MyObject myObject = new MyObject();
myObject.setId("0006");
myObject.setFirstname("Nirav");
myObject.setSecondname("shah");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hbase_pu");
EntityManager em = emf.createEntityManager();
//Save
HBaseJPAImpl hBaseJPAImpl =new HBaseJPAImpl(em);
hBaseJPAImpl.save(myObject);
//retrive
List<MyObject> list= hBaseJPAImpl.findAllDetails();
for(MyObject myObject1 : list){
System.out.println("Row Id : "+myObject1.getId());
System.out.println("First Name : "+myObject1.getFirstname());
System.out.println("Last Name : "+myObject1.getSecondname());
}
}
HBaseJPAImpl 类如下
public class HBaseJPAImpl implements IHBaseJPA
{
public HBaseJPAImpl(EntityManager em) {
// TODO Auto-generated constructor stub
this.em = em;
}
@Inject protected EntityManager em;
@Transactional
public void save(MyObject myObject)
{
// em.persist(myObject));
EntityTransaction entityTransaction = this.em.getTransaction();
entityTransaction.begin();
em.persist(myObject));
entityTransaction.commit();
}
@SuppressWarnings("unchecked")
@Override
@Transactional
public List<MyObject> findAllDetails()
{
Query query = em.createNamedQuery("findAll");
List<MyObject> results = (List<MyObject>) query.getResultList();
return results;
}
}
interface IHBaseJPA is below
public interface IHBaseJPA
{
void save(MyObject contact);`enter code here`
List<MyObject> findAllDetails();
}
如果对上述内容有任何疑问,请与我联系