hbase(main):001:0> scan '-ROOT-'
ROW COLUMN+CELL
.META.,,1 column=info:regioninfo, timestamp=1340249081981, value={NAME => '.META.,,
1', STARTKEY => '', ENDKEY => '', ENCODED => 1028785192,}
.META.,,1 column=info:server, timestamp=1341304672637, value=Hadoop46:60020
.META.,,1 column=info:serverstartcode, timestamp=1341304672637, value=1341301228326
.META.,,1 column=info:v, timestamp=1340249081981, value=\x00\x00
1 row(s) in 1.3230 seconds
hbase(main):002:0> import java.util.Date
=> Java::JavaUtil::Date
hbase(main):003:0> Date.new(1341304672637).toString()
=> "Tue Jul 03 16:37:52 CST 2012"
hbase(main):004:0> Date.new(1341301228326).toString()
=> "Tue Jul 03 15:40:28 CST 2012"
在shell中,如果有可讀日期,能否轉成long類型呢?
hbase(main):005:0> import java.text.SimpleDateFormat
=> Java::JavaText::SimpleDateFormat
hbase(main):006:0> import java.text.ParsePosition
=> Java::JavaText::ParsePosition
hbase(main):015:0> SimpleDateFormat.new("yy/MM/dd").parse("12/07/03",ParsePosition.new(0)).getTime()
=> 1341244800000
參考
http://abloz.com/hbase/book.html
摘要: from:http://abloz.comauthor:ablozhoudate:2012.7.3在hbase的官方文檔里,講述了hbase的bin目錄下的ruby程序,可以采用如下的方式執行:如果要使用腳本,可以看Hbase的bin 目錄.在里面找到后綴為 *.rb的腳本.要想運行這個腳本,要這樣
$ ./bin/hbase org.jruby.Main PATH_TO_SCRIPT
如...
HBase 為用戶提供了一個非常方便的使用方式, 我們稱之為“HBase Shell”。
HBase Shell 提供了大多數的 HBase 命令, 通過 HBase Shell 用戶可以方便地創建、刪除及修改表, 還可以向表中添加數據、列出表中的相關信息等。
備注:寫錯 HBase Shell 命令時用鍵盤上的“Delete”進行刪除,“Backspace”不起作用。
在啟動 HBase 之后,用戶可以通過下面的命令進入 HBase Shell 之中,命令如下所示:
hadoop@ubuntu:~$ hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.3, r1408904, Wed Nov 14 19:55:11 UTC 2012
hbase(main):001:0>
具體的 HBase Shell 命令如下表 1.1-1 所示:

下面我們將以“一個學生成績表”的例子來詳細介紹常用的 HBase 命令及其使用方法。

這里 grad 對于表來說是一個列,course 對于表來說是一個列族,這個列族由三個列組成 china、math 和 english,當然我們可以根據我們的需要在 course 中建立更多的列族,如computer,physics 等相應的列添加入 course 列族。(備注:列族下面的列也是可以沒有名字的。)
1). create 命令
創建一個具有兩個列族“grad”和“course”的表“scores”。其中表名、行和列都要用單引號括起來,并以逗號隔開。
hbase(main):012:0> create 'scores', 'name', 'grad', 'course'
2). list 命令
查看當前 HBase 中具有哪些表。
hbase(main):012:0> list
3). describe 命令
查看表“scores”的構造。
hbase(main):012:0> describe 'scores'
4). put 命令
使用 put 命令向表中插入數據,參數分別為表名、行名、列名和值,其中列名前需要列族最為前綴,時間戳由系統自動生成。
格式: put 表名,行名,列名([列族:列名]),值
例子:
a. 加入一行數據,行名稱為“xiapi”,列族“grad”的列名為”(空字符串)”,值位 1。
hbase(main):012:0> put 'scores', 'xiapi', 'grad:', '1'
hbase(main):012:0> put 'scores', 'xiapi', 'grad:', '2' --修改操作(update)
b. 給“xiapi”這一行的數據的列族“course”添加一列“<china,97>”。
hbase(main):012:0> put 'scores', 'xiapi', 'course:china', '97'
hbase(main):012:0> put 'scores', 'xiapi', 'course:math', '128'
hbase(main):012:0> put 'scores', 'xiapi', 'course:english', '85'
5). get 命令
a.查看表“scores”中的行“xiapi”的相關數據。
hbase(main):012:0> get 'scores', 'xiapi'
b.查看表“scores”中行“xiapi”列“course :math”的值。
hbase(main):012:0> get 'scores', 'xiapi', 'course :math'
或者
hbase(main):012:0> get 'scores', 'xiapi', {COLUMN=>'course:math'}
hbase(main):012:0> get 'scores', 'xiapi', {COLUMNS=>'course:math'}
備注:COLUMN 和 COLUMNS 是不同的,scan 操作中的 COLUMNS 指定的是表的列族, get操作中的 COLUMN 指定的是特定的列,COLUMNS 的值實質上為“列族:列修飾符”。COLUMN 和 COLUMNS 必須為大寫。
6). scan 命令
a. 查看表“scores”中的所有數據。
hbase(main):012:0> scan 'scores'
注意:
scan 命令可以指定 startrow,stoprow 來 scan 多個 row。
例如:
scan 'user_test',{COLUMNS =>'info:username',LIMIT =>10, STARTROW => 'test', STOPROW=>'test2'}
b.查看表“scores”中列族“course”的所有數據。
hbase(main):012:0> scan 'scores', {COLUMN => 'grad'}
hbase(main):012:0> scan 'scores', {COLUMN=>'course:math'}
hbase(main):012:0> scan 'scores', {COLUMNS => 'course'}
hbase(main):012:0> scan 'scores', {COLUMNS => 'course'}
7). count 命令
hbase(main):068:0> count 'scores'
8). exists 命令
hbase(main):071:0> exists 'scores'
9). incr 命令(賦值)
10). delete 命令
刪除表“scores”中行為“xiaoxue”, 列族“course”中的“math”。
hbase(main):012:0> delete 'scores', 'xiapi', 'course:math'
11). truncate 命令
hbase(main):012:0> truncate 'scores'
12). disbale、drop 命令
通過“disable”和“drop”命令刪除“scores”表。
hbase(main):012:0> disable 'scores' --enable 'scores'
hbase(main):012:0> drop 'scores'
13). status命令
hbase(main):072:0> status
14). version命令
hbase(main):073:0> version
另外,在 shell 中,常量不需要用引號引起來,但二進制的值需要雙引號引起來,而其他值則用單引號引起來。HBase Shell 的常量可以通過在 shell 中輸入“Object.constants”。
最近在hadoop實際使用中有以下幾個小細節分享: i=m5M]Ef
1 中文問題
從url中解析出中文,但hadoop中打印出來仍是亂碼?我們曾經以為hadoop是不支持中文的,后來經過查看源代碼,發現hadoop僅僅是不支持以gbk格式輸出中文而己。
這是TextOutputFormat.class中的代碼,hadoop默認的輸出都是繼承自FileOutputFormat來的,FileOutputFormat的兩個子類一個是基于二進制流的輸出,一個就是基于文本的輸出TextOutputFormat。
public class TextOutputFormat<K, V> extends FileOutputFormat<K, V> {
protected static class LineRecordWriter<K, V> &E{CQ#k
implements RecordWriter<K, V> {
private static final String utf8 = “UTF-8″;//這里被寫死成了utf-8 2 kP0//
private static final byte[] newline; kTC'`xv
static { :htz]
try { 0 _!')+
newline = “/n”.getBytes(utf8); Ry$zF~[
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(”can’t find ” + utf8 + ” encoding”);
}
}
… k-:wM`C
public LineRecordWriter(DataOutputStream out, String keyValueSeparator) {
this.out = out;
try {
this.keyValueSeparator = keyValueSeparator.getBytes(utf8);
} catch (UnsupportedEncodingException uee) { @r.w+E=
throw new IllegalArgumentException(”can’t find ” + utf8 + ” encoding”);
}
} ab}Kt($
…
private void writeObject(Object o) throws IOException {
if (o instanceof Text) {
Text to = (Text) o;
out.write(to.getBytes(), 0, to.getLength());//這里也需要修改 q&DM*!Jq
} else { 5 O't-'
out.write(o.toString().getBytes(utf8));
}
}
… qxQuXF>:#
} |3bCq(ZR/P
可以看出hadoop默認的輸出寫死為utf-8,因此如果decode中文正確,那么將Linux客戶端的character設為utf-8是可以看到中文的。因為hadoop用utf-8的格式輸出了中文。
因為大多數數據庫是用gbk來定義字段的,如果想讓hadoop用gbk格式輸出中文以兼容數據庫怎么辦? _.{I1*6Y2
我們可以定義一個新的類: .c5)`
public class GbkOutputFormat<K, V> extends FileOutputFormat<K, V> { sTS Nu+
protected static class LineRecordWriter<K, V>
implements RecordWriter<K, V> {
//寫成gbk即可 F"ua`ercI
private static final String gbk = “gbk”;
private static final byte[] newline;
static {
try {
newline = “/n”.getBytes(gbk);
} catch (UnsupportedEncodingException uee) { @}<b42
throw new IllegalArgumentException(”can’t find ” + gbk + ” encoding”);
}
}
… SjL&/),
public LineRecordWriter(DataOutputStream out, String keyValueSeparator) { P?o|N<46
this.out = out; X-<l+WP
try { 0,]m.)ws
this.keyValueSeparator = keyValueSeparator.getBytes(gbk); Js'j}w
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(”can’t find ” + gbk + ” encoding”);
}
} J|aU}Z8m
… /(&UDG$
private void writeObject(Object o) throws IOException {
if (o instanceof Text) {
// Text to = (Text) o;
// out.write(to.getBytes(), 0, to.getLength()); +A-z>T(
// } else { @h,3"2W{Ev
out.write(o.toString().getBytes(gbk));
}
} isU4D
… eL_Il.:
}
然后在mapreduce代碼中加入conf1.setOutputFormat(GbkOutputFormat.class)
即可以gbk格式輸出中文。
2 關于計算過程中的壓縮和效率的對比問題 hf//2Vl
之前曾經介紹過對輸入文件采用壓縮可以提高部分計算效率。現在作更進一步的說明。
為什么壓縮會提高計算速度?這是因為mapreduce計算會將數據文件分散拷貝到所有datanode上,壓縮可以減少數據浪費在帶寬上的時間,當這些時間大于壓縮/解壓縮本身的時間時,計算速度就會提高了。
hadoop的壓縮除了將輸入文件進行壓縮外,hadoop本身還可以在計算過程中將map輸出以及將reduce輸出進行壓縮。這種計算當中的壓縮又有什么樣的效果呢?
測試環境:35臺節點的hadoop cluster,單機2 CPU,8 core,8G內存,redhat 2.6.9, 其中namenode和second namenode各一臺,namenode和second namenode不作datanode
輸入文件大小為2.5G不壓縮,records約為3600萬條。mapreduce程序分為兩個job: ;R]~9Aan
job1:map將record按user字段作key拆分,reduce中作外連接。這樣最后reduce輸出為87億records,大小540G
job2:map讀入這87億條數據并輸出,reduce進行簡單統計,最后的records為2.5億條,大小16G
計算耗時54min
僅對第二個階段的map作壓縮(第一個階段的map輸出并不大,沒有壓縮的必要),測試結果:計算耗時39min
可見時間上節約了15min,注意以下參數的不同。 U&W/Nj
不壓縮時:
Local bytes read=1923047905109 :3[;9xCHj
Local bytes written=1685607947227 "j8`)XXa(
壓縮時: /U>|^$4 #5
Local bytes read=770579526349 |RL/2j|
Local bytes written=245469534966
本地讀寫的的數量大大降低了
至于對reduce輸出的壓縮,很遺憾經過測試基本沒有提高速度的效果。可能是因為第一個job的輸出大多數是在本地機上進行map,不經過網絡傳輸的原因。
附:對map輸出進行壓縮,只需要添加 jobConf.setMapOutputCompressorClass(DefaultCodec.class)
3 關于reduce的數量設置問題
reduce數量究竟多少是適合的。目前測試認為reduce數量約等于cluster中datanode的總cores的一半比較合適,比如 cluster中有32臺datanode,每臺8 core,那么reduce設置為128速度最快。因為每臺機器8 core,4個作map,4個作reduce計算,正好合適。 u/(>a
附小測試:對同一個程序 j&[u$P*K
reduce num=32,reduce time = 6 min
reduce num=128, reduce time = 2 min
reduce num=320, reduce time = 5min
4某次正常運行mapreduce實例時,拋出錯誤
java.io.IOException: All datanodes xxx.xxx.xxx.xxx:xxx are bad. Aborting…
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.processDatanodeError(DFSClient.java:2158)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.access$1400(DFSClient.java:1735)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream$DataStreamer.run(DFSClient.java:1889)
java.io.IOException: Could not get block locations. Aborting…
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.processDatanodeError(DFSClient.java:2143)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.access$1400(DFSClient.java:1735)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream$DataStreamer.run(DFSClient.java:1889)
經查明,問題原因是linux機器打開了過多的文件導致。用命令ulimit -n可以發現linux默認的文件打開數目為1024,修改/ect/security/limit.conf,增加hadoop soft 65535
再重新運行程序(最好所有的datanode都修改),問題解決
P.S:據說hadoop dfs不能管理總數超過100M個文件,有待查證
5 運行一段時間后hadoop不能stop-all.sh的問題,顯示報錯
no tasktracker to stop ,no datanode to stop
問題的原因是hadoop在stop的時候依據的是datanode上的mapred和dfs進程號。而默認的進程號保存在/tmp下,linux 默認會每隔一段時間(一般是一個月或者7天左右)去刪除這個目錄下的文件。因此刪掉hadoop-hadoop-jobtracker.pid和 hadoop-hadoop-namenode.pid兩個文件后,namenode自然就找不到datanode上的這兩個進程了。
在配置文件中的export HADOOP_PID_DIR可以解決這個問題
Sybase 函數
Sybase字符串函數
長度和語法分析
datalength(char_expr)
在char_expr中返回字符的長度值,忽略尾空
substring(expression,start,length)
返回部分字符串
right(char_expr,int_expr)
返回char_expr右邊的int_expr字符
基本字符串運算
upper(char_expr)
把char_expr轉換成大寫形式
lower(char_expr)
把char_expr轉換成小寫形式
space(int_expr)
生成有int_expr個空格的字符串
replicate(char_expr,int_expr)
重復char_expr,int_expr次
stuff(expr1,start,length,expr2)
用expr2代替epxr1中start起始長為length的字符串
reverse(char_expr)
反寫char_expr中的文本
ltrim(char_expr)
刪除頭空
rtrim(char_expr)
刪除尾空
格式轉換
ascii(char_expr)
返回char_expr中第一個字符的ASCII值
char(int_expr)
把ASCII碼轉換為字符
str(float_expr[,length[,decimal]])
進行數值型到字符型轉換
soundex(char_expr)
返回char_expr的soundex值
difference(char_expr1,char_expr2)
返回表達式soundex值之差
串內搜索
charindex(char_expr,expression)
返回指定char_expr的開始位置,否則為0
patindex("%pattern%",expression)
返回指定樣式的開始位置,否則為0
datalength用于確定可變字符串的長度
soundex用于確定字符串是否發音相似
difference返回0-4之間的值,0表示最不相似,4表示最相似
通配符
% 匹配任何數量的字符或無字符
_ 匹配任何單個字符(空間占位符)
[] 規定有效范圍,或某個"OR"條件
[ABG] A,B,G
[A-C] A,B,C
[A-CE-G] A,B,C,E,F,G
[^ABG] 除了A,B,G
[^A-C] 除了A,B,C
escape子句
用某個轉義字符可在搜索字符串時將通配符作為文字來包含。
ANSI-89 SQL標準定義了escape子句指定某個轉義字符
缺省情況下,[]來轉義某個通配符,例:
select * from test_tab
where description like "%20[%]%"
語法:
like char_expression escape escape_character
例
select * from test_tab
where description like "%20#%%" escape "#"
+ 可用于串接字符
select au_laname+","+au_fname from authors
數學函數
abs(numeric_expr)
返回指定值的絕對值
ceiling(numeric_expr)
返回大于或等于指定值的最小整數
exp(float_expr)
給出指定值的指數值
floor(numeric_expr)
返回小于或等于指定值的最大整數
pi()
返回常數3.1415926
power(numeric_expr,power)
返回numeric_expr的值給power的冪
rand([int_expr])
返回0-1之間的隨機浮點數,可指定基值
round(numeric_expr,int_expr)
把數值表達式圓整到int_expr指定的精度
sign(int_expr)
返回正+1,零0或負-1
sqrt(float_expr)
返回指定值的平方根
SQL SERVER支持所有標準的三角函數和其他有用的函數
日期函數
getdate()
返回當前的系統日期和時間
datename(datepart,date_expr)
以字符串形式返回date_expr指定部分的值,轉換成合適的名字
datepart(datepart,date_expr)
作為整數返回date_expr值的指定部分
datediff(datepart,date_expr1,date_expr2)
返回date_expr2-date_expr1,通過指定的datepart度量
dateadd(datepart,number,date_expr)
返回日期,通過在date_expr上增加指定number的日期部件而產生的
datepart
日期部件 縮寫 值范圍
年 yy 1753-9999
季度 qq 1-4
月 mm 1-12
每年中的天 dy 1-366
天 dd 1-31
星期 wk 1-54
星期天 dw 1-7(1=sunday)
小時 hh 0-23
分鐘 mi 0-59
秒 ss 0-59
毫秒 ms 0-999
例:
select invoice_no,
datediff(dd,date_shipped,getdate())
from invoices
where balance_due>0
轉換函數convert
此函數把值從一種類型改變成另一種類型
convert(datetype [(length)],expression)
select "Advance="+convert(char(12),advance)
from titles
日期轉換
convert(datetype[(length)],expression,format)
format指定將日期轉換為什么格式,有以下值:
沒有世紀 有世紀 轉換字符串中日期格式
0 or 100 mon dd yyy hh:miAM(or PM)
1 101 mm/dd/yy
2 102 yy.mm.dd
3 103 dd/mm/yy
4 104 dd.mm.yy
5 105 dd-mm-yy
6 106 dd mon yy
7 107 mon dd,yy
8 108 hh:mm:ss
9 or 109 mon dd,yyyy hh:mi:ss:mmmAM(or PM)
10 110 mm-dd-yy
11 111 yy/mm/dd
12 112 yymmdd
系統函數
函數 定義
訪問和安全性信息
host_id() 客戶進程的當前主機進程ID號
host_name() 客戶進程的當前主計算機名
suser_id(["login_name"]) 用戶的SQL Server ID號
suser_name([server_user_id]) 用戶的SQL Server登錄名
user_id(["name_in_db"]) 用戶在數據庫中的ID號
user_name([user_id]) 用戶在數據庫中的名字
user 用戶在數據庫中的名字
show_role() 用戶的當前活動角色
數據庫和對象信息
db_id(["db_name"]) 數據庫ID號
db_name([db_id]) 數據庫名
object_id("objname") 數據庫對象ID號
object_name(obj_id]) 數據庫對象號
col_name(obj_id,col_id) 對象的欄名
col_length("objname","colname") 欄的長度
index_col("objname",index_id,key#) 已索引的欄名
valid_name(char_expr) 若char_expr不是有效標識符,則返回0
數據函數
datalength(expression) 按字節返回expression的長度
tsequal(timestamp1,timestamp2) 比較時戳值,若時戳值不匹配,則返回出錯消息
isnull()
isnull函數用指定的值代替查詢欄或合計中的空值
例:
select avg(isnull(total_order,$0))
from invoices
日期函數
getdate()
得到當前時間,可以設置得到各種時間格式.
datepart(日期部分,日期)
取指定時間的某一個部分,年月天時分秒.
datediff(日期部分,日期1,日期2)
計算指定的日期1和日期2的時間差多少.
dateadd(日期部分,數值表達式,日期)
計算指定時間,再加上表達式指定的時間長度.
--取時間的某一個部分
select datepart(yy,getdate()) --year
select datepart(mm,getdate()) --month
select datepart(dd,getdate()) --day
select datepart(hh,getdate()) --hour
select datepart(mi,getdate()) --min
select datepart(ss,getdate()) --sec
--取星期幾
set datefirst 1
select datepart(weekday,getdate()) --weekday
--字符串時間
select getdate() -- '03/11/12'
select convert(char,getdate(),101) -- '09/27/2003'
select convert(char,getdate(),102) -- '2003.11.12'
select convert(char,getdate(),103) -- '27/09/2003'
select convert(char,getdate(),104) -- '27.09.2003'
select convert(char,getdate(),105) -- '27-09-2003'
select convert(char,getdate(),106) -- '27 Sep 2003'
select convert(char,getdate(),107) --'Sep 27, 2003'
select convert(char,getdate(),108) --'11:16:06'
select convert(char,getdate(),109) --'Sep 27 2003 11:16:28:746AM'
select convert(char,getdate(),110) --'09-27-2003'
select convert(char,getdate(),111) --'2003/09/27'
select convert(char,getdate(),112) --'20030927'
select rtrim(convert(char,getdate(),102))+' '+(convert(char,getdate(),108)) -- '2003.11.12 11:03:41'
--整數時間
select convert(int,convert(char(10),getdate(),112)) -- 20031112
select datepart(hh,getdate())*10000 + datepart(mi,getdate())*100 + datepart(ss,getdate()) -- 110646
--時間格式 "YYYY.MM.DD HH:MI:SS" 轉換為 "YYYYMMDDHHMISS"
declare @a datetime,@tmp varchar(20),@tmp1 varchar(20)
select @a=convert(datetime,'2004.08.03 12:12:12')
select @tmp=convert(char(10),@a,112)
select @tmp
select @tmp1=convert(char(10),datepart(hh,@a)*10000 + datepart(mi,@a)*100 + datepart(ss,@a))
select @tmp1
select @tmp=@tmp+@tmp1
select @tmp
--當月最后一天
declare
@tmpstr varchar(10)
@mm int,
@premm int,
@curmmlastday varchar(10)
begin
select @mm=datepart(month,getdate())--當月
select @premm=datepart(month,dateadd(month,-1,getdate())) --上個月
if (@mm>=1 and @mm<=8)
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
else if (@mm>=9 and @mm<=11)
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.'+convert(char(2),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
else
select @tmpstr=convert(char(4),datepart(year,dateadd(year,1,getdate())))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
select @curmmlastday=convert(char(10),dateadd(day,-1,@tmpstr),102) --當月最后一天
end
源文檔 <http://hi.baidu.com/hwaspf/blog/item/a0ef87be66326e0d18d81f17.html>
摘要: 轉自:http://software.intel.com/zh-cn/articles/javascript-first-class-citizen-function/?cid=sw:prccsdn229032簡介在很多傳統語言(C/C++/Java/C#等)中,函數都是作為一個二等公民存在,你只能用語言的關鍵字聲明一個函數然后調用它,如果需要把函數作為參數傳給另一個函數,或是賦值給一個本地變量,...
摘要: Postgres 格式化函數提供一套有效的工具用于把各種數據類型(日期/時間,int,float,numeric)轉換成格式化的字符串以及反過來從格式化的字符串轉換成原始的數據類型。注意:所有格式化函數的第二個參數是用于轉換的模板。表 5-7. 格式化函數 函數返回描述例子to_char(timestamp, text)text把 timestamp 轉換成 str...