概述

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快速入门 分类: C_OHTERS 2015-01-30 09:55 465人阅读 评论(0) 收藏的更多相关文章

  1. 【solr专题之一】Solr快速入门 分类: H4_SOLR/LUCENCE 2014-07-02 14:59 2403人阅读 评论(0) 收藏

    一.Solr学习相关资料 1.官方材料 (1)快速入门:http://lucene.apache.org/solr/4_9_0/tutorial.html,以自带的example项目快速介绍发Solr ...

  2. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  3. iOS开源库--最全的整理 分类: ios相关 2015-04-08 09:20 486人阅读 评论(0) 收藏

    youtube下载神器:https://github.com/rg3/youtube-dl 我擦咧 vim插件:https://github.com/Valloric/YouCompleteMe vi ...

  4. Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏

    Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...

  5. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

  7. iOS 消息推送原理及实现总结 分类: ios技术 2015-03-01 09:22 70人阅读 评论(0) 收藏

    在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服 ...

  8. Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏

    效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  9. C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏

    using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...

随机推荐

  1. 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  2. Effective C++ 条款43

    学习处理模板化基类里的名称 本节作者编写的意图在我看来能够总结成一句话,就是"怎样定义并使用关于模板类的派生过程,怎样处理派生过程出现的编译不通过问题". 以下我们看一段说明性的代 ...

  3. PHP JSON的BUG

    将下面的数组进行 JSON 编码时出错,编码中丢掉了最后一维数组中的下标. Array ( [1] => Array ( [0] => Array ( [0] => Array ( ...

  4. 公众平台调整SSL安全策略,请开发者注意升级

    公众平台调整SSL安全策略,请开发者注意升级 近一段时间HTTPS加密协议SSL曝出高危漏洞,可能导致网络中传输的数据被黑客监听,对用户信息.网络账号密码等安全构成威胁.为保证用户信息以及通信安全,微 ...

  5. BZOJ2555: SubString(后缀自动机,LCT维护Parent树)

    Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...

  6. SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

  7. Spring学习总结(5)——IOC注入方式总结

    一.构造注入 在类被实例化的时候,它的构造方法被调用并且只能调用一次.所以它被用于类的初始化操作.<constructor-arg>是<bean>标签的子标签.通过其<v ...

  8. 设计模式六大原则(六): 开闭原则(Open Closed Principle)

    定义: 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 问题由来: 在软件的生命周期内,因为变化.升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不 ...

  9. [JWT] JWT Signature With RS256 - Learn The Advantages Compared to HS256

    The advantage of RS256 over HS256 is RS256 no longer need to share the secret key between client and ...

  10. 几个经常使用的cmd命令

    compmgmt.msc 计算机管理  devmgmt.msc 设备管理器  diskmgmt.msc 磁盘管理工具  dfrg.msc 磁盘碎片整理  eventvwr.msc 事件查看器  fsm ...