The source code is given first.
Package lekko.hbase;
/** * Created by Root in 2016/12/13.
* * Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.hbase.Cell;
Import Org.apache.hadoop.hbase.CellUtil;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.HColumnDescriptor;
Import Org.apache.hadoop.hbase.HTableDescriptor;
Import org.apache.hadoop.hbase.MasterNotRunningException;
Import Org.apache.hadoop.hbase.TableName;
Import org.apache.hadoop.hbase.ZooKeeperConnectionException;
Import Org.apache.hadoop.hbase.client.Admin;
Import org.apache.hadoop.hbase.client.Connection;
Import Org.apache.hadoop.hbase.client.ConnectionFactory;
Import Org.apache.hadoop.hbase.client.Delete;
Import Org.apache.hadoop.hbase.client.Get;
Import Org.apache.hadoop.hbase.client.Put;
Import Org.apache.hadoop.hbase.client.Result;
Import Org.apache.hadoop.hbase.client.ResultScanner; Import Org.apache.hadoop.hbase.client.sCan
Import org.apache.hadoop.hbase.client.Table;
Import org.apache.hadoop.hbase.util.Bytes;
public class Hbasedemo {public static Configuration conf;
static {conf = Hbaseconfiguration.create ();
Conf.set ("Hbase.zookeeper.quorum", "Master");
Conf.addresource ("Hbase-site.xml"); /** * CREATE TABLE * * @param tablename table name * @param columnfamily Column family * @throws MASTERNOTRUNNINGEXCEP tion * @throws zookeeperconnectionexception * @throws ioexception/public static void CreateTable (STR ING tablename, String columnfamily) throws Masternotrunningexception, IOException, Zookeeperconnectionexceptio
n {Connection conn = connectionfactory.createconnection (conf);
Admin admin = conn.getadmin (); try {if (Admin.tableexists (tablename.valueof (tablename))) {System.out.println ("Create table")
Data: "+ tablename +" already exists, and drop it successfully. "); else {tablename TableName = tablename.valueof (tablename);
Htabledescriptor Tabledesc = new Htabledescriptor (tablename);
Tabledesc.addfamily (New Hcolumndescriptor (columnfamily));
Admin.createtable (TABLEDESC); SYSTEM.OUT.PRINTLN ("Create table data:" + tablename + "created succeed.");}
finally {admin.close ();
Conn.close ();
/** * Inserts a new data into the table * * @param tablename table name * @param row key key * @param columnfamily column Family * @param column name * @param data to be inserted by * @throws IOException/public static void PutData (Strin G TableName, String row, String columnfamily, String column, String data) throws IOException {Connect
Ion conn = connectionfactory.createconnection (conf);
Table table = conn.gettable (tablename.valueof (tablename)); try {Put on = new put (Bytes.tobytes (row));
Put.addcolumn (Bytes.tobytes (columnfamily), bytes.tobytes (column), bytes.tobytes (data);
Table.put (Put);
System.out.println ("put" + row + "', '" + columnfamily + ":" + column + "', '" + Data + "'" + "successfully.");
finally {table.close ();
Conn.close (); }/** * Add a column family to a existing table * * @param tablename table name * @param c olumnfamily Column Family * @throws IOException */public static void Putfamily (String tablename, String col
umnfamily) throws IOException {Connection conn = connectionfactory.createconnection (conf);
Admin admin = conn.getadmin (); try {if (!admin.tableexists tablename.valueof (tablename)) {SYSTEM.OUT.PRINTLN (tablename +)
Not exists ");
else {admin.disabletable (tablename.valueof (tablename)); HcolumndescrIptor cf1 = new Hcolumndescriptor (columnfamily);
Admin.addcolumn (tablename.valueof (tablename), CF1);
Admin.enabletable (tablename.valueof (tablename));
System.out.println ("Put family data:" + tablename.valueof (tablename) + "," + columnfamily + "put succeed.");
finally {admin.close ();
Conn.close (); }/** * Reads a data according to key * * @param tablename table name * @param row key key * @param columnfamily column Family * @param column name * @throws IOException */public static void GetData (String tablename, String row, String columnfamily, String column) throws ioexception{Connection conn = connectionfactory.createconnection (conf)
;
Table table = conn.gettable (tablename.valueof (tablename));
try{Get get = new Get (bytes.tobytes (row));
Result result = Table.get (get); Byte[] RB = Result.getvalue (bytes.tobytES (columnfamily), bytes.tobytes (column));
String value = new String (RB, "UTF-8");
SYSTEM.OUT.PRINTLN ("Get Data" +value+ "successfully.");
finally {table.close ();
Conn.close ();
}/** * Get all data of a table * * @param tablename table name * @throws IOException */public static void Scanall (String tablename) throws IOException {Connection conn = Connectionfactory.creat
Econnection (conf);
Table table = conn.gettable (tablename.valueof (tablename));
try {Scan Scan = new Scan ();
Resultscanner Resultscanner = Table.getscanner (scan);
for (result result:resultscanner) {list<cell> cells = result.listcells ();
for (Cell cell:cells) {String row = new String (Result.getrow (), "UTF-8");
String family = new String (cellutil.clonefamily (cell), "UTF-8"); String qualifier = new String (Cellutil.clonequalifier (cell), "UTF-8");
String value = new String (Cellutil.clonevalue (cell), "UTF-8"); System.out.println ("Scan all data: [Row Kin row: +row+"],[cluster family: "+family+"],[column qualifier: "+qualifier+"],[value value: "+ Value+ "],[timestamp:" +cell.gettimestamp () + "] successfully.");}}
finally {table.close ();
Conn.close ();
}/** * Delete a data by row key * * @param tablename table name * @param rowkey Row key
* @throws IOException */public static void Deldata (String tablename, String rowkey) throws IOException {
Connection conn = connectionfactory.createconnection (conf);
Table table = conn.gettable (tablename.valueof (tablename));
try {list<delete> List = new arraylist<delete> ();
Delete del = new Delete (Rowkey.getbytes ()); List.aDD (DEL);
Table.delete (list); System.out.println ("Delete record" + Rowkey + "successfully.");}
finally {table.close ();
Conn.close (); }/** * Delete a column ' s value of a row * * @param tablename table name * @param rowkey RO W Key * @param familyname family name * @param columnName column name * @throws IOException/Pub
Lic static void DeleteColumn (String tablename, String Rowkey, String familyname, String columnName) throws IOException {
Connection conn = connectionfactory.createconnection (conf);
Table table = conn.gettable (tablename.valueof (tablename));
try{Delete del = new Delete (Bytes.tobytes (Rowkey));
Del.addcolumn (Bytes.tobytes (familyname), Bytes.tobytes (ColumnName));
list<delete> list = new arraylist<delete> (1);
List.add (DEL);
Table.delete (list); SysteM.out.println ("Delete column data: [Table:" +tablename+ "],row:" +rowkey+ "],[family:" +familyname+ "],[qualifier:" + Columnname+ "] successfully.");
finally {table.close ();
Conn.close ();
}/** * Delete a columnfamily ' s all columns value of a row * * @param tablename table name * @param rowkey Row key * @param familyname family name * @throws ioexception/public static void Del Etefamily (String tablename, String Rowkey, String familyname) throws IOException {Connection conn = Connectionfac
Tory.createconnection (conf);
Table table = conn.gettable (tablename.valueof (tablename));
try{Delete del = new Delete (Bytes.tobytes (Rowkey));
Del.addfamily (Bytes.tobytes (familyname));
list<delete> list = new arraylist<delete> (1);
List.add (DEL);
Table.delete (list); System.out.println ("Delete Family data: [table:"]+tablename+ "],row:" +rowkey+ "],[family:" +familyname+ "] successfully.");
finally {table.close ();
Conn.close (); }/** * Delete a table * * @param tablename table name * @throws IOException * @throws Masternotrunningexception * @throws zookeeperconnectionexception */public static void Deletetable (String t Ablename) throws IOException, Masternotrunningexception, zookeeperconnectionexception {Connection conn = Connecti
Onfactory.createconnection (conf);
Admin admin = conn.getadmin ();
try {admin.disabletable (tablename.valueof (tablename));
Admin.deletetable (tablename.valueof (tablename)); SYSTEM.OUT.PRINTLN ("Delete table" + tablename + "OK.");}
finally {admin.close ();
Conn.close ();
} public static void Main (string[] args) {System.err.println ("HBase Test start ..."); String TableName = "Student";
String tablename = "Hpwy.studentinfo";
try{deletetable (tablename);
CreateTable (tablename, "Stu");
CreateTable (tablename + "2", "Stu");
/* CreateTable (tablename, "load");
PutData (tablename, "Row_1", "Load", "No", "0001");
PutData (tablename, "Row_1", "Load", "Rec_date", "2016-06-03");
PutData (tablename, "Row_1", "Load", "Rec_time", "09:49:00"); PutData (tablename, "Row_1", "Load", "Power", "154.24"); * * PutData (tablename, "Row_1", "Stu", "stu_id", "001")
;
PutData (tablename, "Row_2", "Stu", "stu_id", "002");
PutData (tablename, "Row_3", "Stu", "stu_id", "003");
GetData (tablename, "Row_1", "Stu", "stu_id");
Deldata (tablename, "Row_3");
Scanall (tablename);
Deletetable (tablename + "2");
Putfamily (tablename, "score"); PutData (tablename, "Row_4", "Score","Chinese", "90");
PutData (tablename, "Row_5", "Score", "math", "91");
Scanall (tablename);
DeleteColumn (tablename, "Row_4", "score", "Chinese");
Deletefamily (tablename, "Row_5", "score");
Scanall (tablename);
catch (Exception ex) {ex.printstacktrace ();
} System.err.println ("HBase Test End ..."); }
}
Configure the configuration file for the XML here at the same time
<?xml version= "1.0"?> <?xml-stylesheet type= "text/xsl" href= "configuration.xsl"?> <!--/** * * Licensed t o The Apache Software Foundation (ASF) under one * or more contributor license agreements. The NOTICE file * Distributed with this work for additional information * regarding copyright. The ASF licenses this file * to you under the Apache License, Version 2.0 (The * "License"); You are not to use this file, except in compliance * with the License. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by APPL Icable or agreed to in writing, software * Distributed under the License was distributed on ' as is ' basis, * witho
UT warranties or CONDITIONS of any KIND, either express OR implied.
* The License for the specific language governing permissions and * limitations under the License. *--> <configuration> <property> <name>hbase.rootdir</name>
<value>hdfs://master:9000/hbase</value> </property> <property> <NAME>HBASE.CLUSER.D istributed</name> <value>true</value> </property> <property> <name>hbase .zookeeper.property.clientport</name> <value>2182</value> </property> <PR operty> <name>hbase.master</name> <value>master:60000</value> </property> <pro Perty> <name>base.zookeeper.quorum</name> <value>slave1,slave2,slave3</value> & lt;/property> <property> <name>hbase.security.authorization</name> <value>false </value> </property> <property> <name>zookeeper.session.timeout</name> <v alue>1200000</value> </property> <property> <NAME>HBASE.ZOOKEEPER.PROPERTY.DATADIR&L T;/name> <value>/home/zookeeper/datadir</value> </property> <property> <name>zookeeper.znode .parent</name> <value>/hbase</value> <description>root Znode for hbase in zookeeper.
All of HBase's zookeeper files that are configured with a relative path would go under this node. By default, all of the HBase ' s zookeeper file path are configured with a relative path, so they'll all go under this
directory unless changed.
</description> </property> <property> <name>zookeeper.znode.rootserver</name> <value>root-region-server</value> <description>path to Znode holding root region location. This is written by the master and read by clients and region servers. If A relative path is given, the parent folder would be ${zookeeper.znode.parent}. By default, this means the root location is stored at/hbase/root-region-sErver.
</description> </property> </configuration>
Finally share the results
"C:\Program Files\java\jdk1.8.0_74\bin\java"-didea.launcher.port=7534 "-didea.launcher.bin.path=d:\idea\intellij Idea 15.0.2\bin "-dfile.encoding=utf-8-classpath" C:\Program Files\java\jdk1.8.0_74\jre\lib\charsets.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\deploy.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\access-bridge-64.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\cldrdata.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\dnsns.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\jaccess.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\jfxrt.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\localedata.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\nashorn.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\sunec.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\sunjce_provider.jar; C:\Program Files\java\jdk1.8.0_74\jre\lib\ext\sunmscapi.jar; C:\Program FILES\JAVA\JDK1.8.0_74\JRE\LIB\EXT\SUNPKCS1