Gora快速入门
概述
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
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] + "
");
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 x00x00x00x00x00x00x00x00 column=common:gid, timestamp=1422544581799, value=x00x00x00x00 x00x00x00x00x00x00x00x00 column=common:loginname, timestamp=1422544581799, value=root x00x00x00x00x00x00x00x00 column=common:passwd, timestamp=1422544581799, value=x x00x00x00x00x00x00x00x00 column=common:uid, timestamp=1422544581799, value=x00x00x00x00 x00x00x00x00x00x00x00x00 column=common:username, timestamp=1422544581799, value=root ………………………………
另外,關于讀取數據庫及刪除數據的操作,請參考本文最前面的參考文檔。
總結
- 上一篇: 坦克世界账号被封永久了怎么不删号啊
- 下一篇: 电脑无法开机如何让电脑不开机