Gora快速入门 分类: C_OHTERS 2015-01-30 09:55 465人阅读 评论(0) 收藏
2015-01-30 09:55
393 查看
概述
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所使用的一些属性。
第二个属性指定了若表不存在,则自动创建一个。
因此在下面的java代码中没有显式调用
3、根据/etc/passwd的内容创建avro/passwd.json
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类中的属性与数据库中字段的对应关系。
6、编写类文件
程序中的关键步骤如下:
(1)获取DataSource
7、从eclipsse导出程序,上传到服务器中,并运行程序
$ java -jar GoraDemo.jar
(1)导出的程序应为runnable jar file。
(2)运行程序的服务器器中需要运行着hbase。
8、查看结果
另外,关于读取数据库及删除数据的操作,请参考本文最前面的参考文档。
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 ………………………………
另外,关于读取数据库及删除数据的操作,请参考本文最前面的参考文档。
相关文章推荐
- sqoop 1.4.4-cdh5.1.2快速入门 分类: C_OHTERS 2015-06-06 11:40 208人阅读 评论(0) 收藏
- mysql快速入门 分类: B6_MYSQL 2015-04-28 14:31 284人阅读 评论(0) 收藏
- RMAN之一:快速入门 分类: H2_ORACLE 2014-02-17 16:11 689人阅读 评论(0) 收藏
- Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏
- Gora官方范例 分类: C_OHTERS 2015-01-29 16:14 632人阅读 评论(0) 收藏
- JSON入门之二:org.json的基本用法 分类: C_OHTERS 2014-05-14 11:25 6001人阅读 评论(0) 收藏
- Json入门 分类: C_OHTERS 2014-04-23 16:20 601人阅读 评论(0) 收藏
- JDBC之一:JDBC快速入门 分类: B1_JAVA 2014-02-19 14:49 745人阅读 评论(0) 收藏
- Gora官方文档之二:Gora对Map-Reduce的支持 分类: C_OHTERS 2015-01-31 11:27 232人阅读 评论(0) 收藏
- 【solr专题之一】Solr快速入门 分类: H4_SOLR/LUCENCE 2014-07-02 14:59 2403人阅读 评论(0) 收藏
- HIVE快速入门 分类: B4_HIVE 2015-06-06 11:27 59人阅读 评论(0) 收藏
- HtmlParser基础教程 分类: C_OHTERS 2014-05-22 11:33 1649人阅读 评论(1) 收藏
- android 快速创建一个新的线… 分类: Android开发 2014-05-30 10:57 93人阅读 评论(0) 收藏
- HttpClient基础教程 分类: C_OHTERS 2014-05-18 23:23 2600人阅读 评论(0) 收藏
- Android快速搭建模拟器环境另类方… 分类: Android开发 2014-05-30 10:52 90人阅读 评论(0) 收藏
- 《大型网站技术架构》1:概述 分类: C_OHTERS 2014-05-07 20:40 664人阅读 评论(0) 收藏
- ffmpeg入门之 Tutorial02 分类: ffmpeg-SDL-VLC-Live555 2013-08-26 08:48 520人阅读 评论(0) 收藏
- GitHub项目协作基本步骤 分类: C_OHTERS 2013-09-23 21:31 690人阅读 评论(0) 收藏
- JSON之三:获取JSON文本并解释(以google的天气API为例) 分类: C_OHTERS 2014-05-14 16:35 2209人阅读 评论(0) 收藏
- GoldenGate系统二:快速搭建三方单向通信GoldenGate环境 分类: H2_ORACLE 2013-05-16 10:54 827人阅读 评论(0) 收藏