概述

Gora是apache的一个开源项目。

The Apache Gora open source framework provides an in-memory data model and persistence for big data. Gora supports persisting to column stores, key value stores, document stores and RDBMSs,
and analyzing the data with extensive Apache Hadoop MapReduce support. - See more at: http://gora.apache.org/current/tutorial.html#sthash.i7gfQUe7.dpuf
The Apache Gora open source framework provides an in-memory data model and persistence for big data. Gora supports persisting to column stores, key value stores, document stores and RDBMSs,
and analyzing the data with extensive Apache Hadoop MapReduce support. - See more at: http://gora.apache.org/current/tutorial.html#sthash.i7gfQUe7.dpuf
The Apache Gora open source framework provides an in-memory data model and persistence for big data. Gora supports persisting to column stores, key value stores, document stores and RDBMSs,
and analyzing the data with extensive Apache Hadoop MapReduce support. - See more at: http://gora.apache.org/current/tutorial.html#sthash.i7gfQUe7.dpuf

The Apache Gora open source framework provides an in-memory data model and persistence for big data. Gora supports persisting to column stores, key value stores, document stores and RDBMSs, and analyzing the data with extensive
Apache Hadoop MapReduce support.

Gora与Hibernate类似,提供了java类到数据库的映射及持久化,前者虽也支持RDMS,但更侧重于列式、KV等类型的数据库。

The Apache Gora open source framework provides an in-memory data model and persistence for big data. Gora supports persisting to column stores, key value stores, document stores and RDBMSs,
and analyzing the data with extensive Apache Hadoop MapReduce support. - See more at: http://gora.apache.org/current/tutorial.html#sthash.i7gfQUe7.dpuf

使用Gora写入数据的关键步骤

1、根据要处理的数据,创建用于描述数据结构的json文件,并由此生成java类。

2、创建gora-hbase-mapping.xml,用于注明描述了数据库表的结构,以及java类中的属性与数据库中字段的对应关系。

3、创建主类,用于创建对象,并写入数据库。

即前2步建立了用于描述数据的java类及数据库表,以及它们之间的映射关系。第三步首先将内容读入java程序中,然后通过gora写入数据库。

快速入门范例

更详细范例可参考

http://blog.csdn.net/jediael_lu/article/details/43272521

http://gora.apache.org/current/tutorial.html

1、创建一个java project,并准备好待分析的内容。

本项目用于读取/etc/passwd中的内容,并将其写入hbase数据库中。

2、创建conf/gora.properties,此文件定义了gora所使用的一些属性。

##gora.datastore.default is the default detastore implementation to use
##if it is not passed to the DataStoreFactory#createDataStore() method.
gora.datastore.default=org.apache.gora.hbase.store.HBaseStore ##whether to create schema automatically if not exists.
gora.datastore.autocreateschema=true

第二个属性指定了若表不存在,则自动创建一个。

因此在下面的java代码中没有显式调用

DataStoreFactory#createDataStore()

3、根据/etc/passwd的内容创建avro/passwd.json

{
"type": "record",
"name": "Passwd", "default":null,
"namespace": "org.ljh.gora.demo.generated",
"fields" : [
{"name": "loginname", "type": ["null","string"], "default":null},
{"name": "passwd", "type": ["null","string"], "default":null},
{"name": "uid", "type": "int", "default":0},
{"name": "gid", "type": "int", "default":0},
{"name": "username", "type": ["null","string"], "default":null},
{"name": "home", "type": ["null","string"], "default":null},
{"name": "shell", "type": ["null","string"], "default":null}
]
}

4、利用avro/passwd.json生成类

$ gora goracompiler avro/passwd.json src

Compiling: /Users/liaoliuqing/99_Project/1_myCodes/GoraDemo/avro/passwd.json

Compiled into: /Users/liaoliuqing/99_Project/1_myCodes/GoraDemo/src

Compiler executed SUCCESSFULL

5、创建conf/gora-hbase-mapping.xml,用于注明描述了数据库表的结构,以及java类中的属性与数据库中字段的对应关系。

<?xml version="1.0" encoding="UTF-8"?>

<gora-otd>
<table name="Passwd">
<family name="common"/>
<family name="env"/>
</table> <class name="org.ljh.gora.demo.generated.Passwd" keyClass="java.lang.Long" table="Passwd">
<field name="loginname" family="common" qualifier="loginname"/>
<field name="passwd" family="common" qualifier="passwd"/>
<field name="uid" family="common" qualifier="uid" />
<field name="gid" family="common" qualifier="gid"/>
<field name="username" family="common" qualifier="username"/>
<field name="home" family="env" qualifier="home"/>
<field name="shell" family="env" qualifier="shell"/>
</class> </gora-otd>

6、编写类文件

package org.ljh.gora.demo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException; import org.apache.gora.store.DataStore;
import org.apache.gora.store.DataStoreFactory;
import org.apache.hadoop.conf.Configuration;
import org.ljh.gora.demo.generated.Passwd; public class PasswdManager {     private DataStore<Long, Passwd> dataStore = null;     public PasswdManager() {
        try {
            init();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }     private void init() throws IOException {          dataStore = DataStoreFactory.getDataStore(Long.class, Passwd.class,
                new Configuration());
    }     private void parse(String input) throws IOException, ParseException,
            Exception {
        BufferedReader reader = new BufferedReader(new FileReader(input));
        long lineCount = 0;
        try {
            String line = reader.readLine();
            do {
                Passwd passwd = parseLine(line);
                if (passwd != null) {
                    dataStore.put(lineCount++, passwd);
                    dataStore.flush();
                }
                line = reader.readLine();
            } while (line != null);         } finally {
            reader.close();
            dataStore.close();
        }
    }     /** Parses a single log line in combined log format using StringTokenizers */
    private Passwd parseLine(String line) throws ParseException {         String[] tokens = line.split(":");
        System.out.println(tokens[0] + tokens[1] + "\n\n\n");         String loginname = tokens[0];
        String password = tokens[1];
        int uid = Integer.parseInt(tokens[2]);
        int gid = Integer.parseInt(tokens[3]);
        String username = tokens[4];
        String home = tokens[5];
        String shell = tokens[6];         Passwd passwd = new Passwd();
        passwd.setLoginname(loginname);
        passwd.setPasswd(password);
        passwd.setUid(uid);
        passwd.setGid(gid);
        passwd.setUsername(username);
        passwd.setHome(home);
        passwd.setShell(shell);         return passwd;
    }     public static void main(String[] args) throws IOException, ParseException,
            Exception {
        PasswdManager manager = new PasswdManager();
        manager.parse("passwd");
    }
}

程序中的关键步骤如下:

(1)获取DataSource

dataStore = DataStoreFactory.getDataStore(Long.class, Passwd.class,
                new Configuration());

(2)准备好写入数据库数据的key与value

long lineCount = 0;              
Passwd passwd = parseLine(line);

(3)将数据写入库表

                    dataStore.put(lineCount++, passwd);

7、从eclipsse导出程序,上传到服务器中,并运行程序

$ java -jar GoraDemo.jar

(1)导出的程序应为runnable jar file。

(2)运行程序的服务器器中需要运行着hbase。

8、查看结果

hbase(main):006:0> scan 'Passwd'
ROW COLUMN+CELL
\x00\x00\x00\x00\x00\x00\x00\x00 column=common:gid, timestamp=1422544581799, value=\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00 column=common:loginname, timestamp=1422544581799, value=root
\x00\x00\x00\x00\x00\x00\x00\x00 column=common:passwd, timestamp=1422544581799, value=x
\x00\x00\x00\x00\x00\x00\x00\x00 column=common:uid, timestamp=1422544581799, value=\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00 column=common:username, timestamp=1422544581799, value=root
………………………………

另外,关于读取数据库及删除数据的操作,请参考本文最前面的参考文档。

Gora快速入门的更多相关文章

  1. Gora快速入门 分类: C_OHTERS 2015-01-30 09:55 465人阅读 评论(0) 收藏

    概述 Gora是apache的一个开源项目. The Apache Gora open source framework provides an in-memory data model and pe ...

  2. Nutch 快速入门(Nutch 2.2.1+Hbase+Solr)

    http://www.tuicool.com/articles/VfEFjm Nutch 2.x 与 Nutch 1.x 相比,剥离出了存储层,放到了gora中,可以使用多种数据库,例如HBase, ...

  3. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  4. SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)

     SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...

  5. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)

    今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...

  6. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  7. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  8. Mybatis框架 的快速入门

    MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...

  9. grunt快速入门

    快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...

随机推荐

  1. vagrant 配置文件简析

    #自定义box名字 config.vm.box = "website" #以ip192.168.33.10访问 config.vm.network "private_ne ...

  2. JQuery this和$(this)的区别及获取$(this)子元素对象的方法

    1.JQuery this和$(this)的区别 相信很多刚接触JQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢? 首先来看看JQuery中的  $()  这 ...

  3. Chrome下的语音控制框架MyVoix.js使用篇(一)

    日前因工作需求,着手研究了语音识别技术,发现github上有网友发布了一款叫做voix.js的javascript框架.在拜读voix.js的源码后发现了不少问题,于是自己写了一款语音识别框架MyVo ...

  4. bindingredirect 没有效果

    在搞在线聊天室的时候用到了SignalR 1.1.4,依赖于Newtonsoft.Json 4.5.0.0. 而我另外的dll又依赖Newtonsoft.Json 6.0.0.0 我只引用6.0.0. ...

  5. KEIL中的变量相关

    例:sfr P0=0x80表示P0口地址为80H:(sfr是字节操作) sfr16 T2=0xCC表示T2口地址的低地址为TL2=0XCC,高地址为TH2=0XCD(sfr16是字操作) 头文件reg ...

  6. HTTP发送请求模拟

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...

  7. Linux企业级项目实践之网络爬虫(21)——扩展为多任务爬虫

    高效的网络爬虫是搜索引擎的重要基础.采用多任务并发执行,实现类似于CPU的流水线(pipeline)运行方式,可极大地提高网络和计算资源的利用率等性能. #include "threads. ...

  8. spring framework 4 源码阅读(1) --- 前期准备

    在开始看代码之前,需要做的第一件事是下载代码. 在这里:https://github.com/spring-projects/spring-framework 下载完成了发现使用gradle做的源代码 ...

  9. tyvj1185营业额统计

    描述 Description 营业额统计  Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况.  Tiger拿出了公司的账本,账本上记录了公司 ...

  10. hdu1521:排列组合---指数型母函数

    题意: n种元素,每种有 ni个,选出 m 个的排列有多少种 题解: 指数型母函数的裸题 x^n 项的系数为  an/n!.... 代码如下: #include <iostream> #i ...