root, and the password is the one you set when creating the EMR cluster. Once the correct information is entered, you can enter the EMR command line interface./usr/local/service/hbase:[root@172 ~]# su hadoop[hadoop@10root]$ cd /usr/local/service/hbase
[hadoop@10hbase]$ bin/hbase shell
help in HBase Shell to see basic usage information and command examples. Next, create a table by running the following command:hbase(main):001:0> create 'test', 'cf'
list command to see whether it exists.hbase(main):002:0> list 'test'TABLEtest1 row(s) in 0.0030 seconds=> ["test"]
put command to add elements to the table you created:hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'0 row(s) in 0.0850 secondshbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'0 row(s) in 0.0110 secondshbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'0 row(s) in 0.0100 seconds
scan command to traverse the entire table:hbase(main):006:0> scan 'test'ROW COLUMN+CELLrow1 column=cf:a, timestamp=1530276759697, value=value1row2 column=cf:b, timestamp=1530276777806, value=value2row3 column=cf:c, timestamp=1530276792839, value=value33 row(s) in 0.2110 seconds
get command to get the value of the specified row in the table:hbase(main):007:0> get 'test', 'row1'COLUMN CELLcf:a timestamp=1530276759697, value=value1 row(s) in 0.0790 seconds
drop command to delete a table, which should be disabled first before deletion by running the disable command:hbase(main):010:0> disable 'test'hbase(main):011:0> drop 'test'
quit command to close HBase Shell.D://mavenWorkplace, and create the project by running the following commands:mvn archetype:generate -DgroupId=$yourgroupID -DartifactId=$yourartifactID-DarchetypeArtifactId=maven-archetype-quickstart
$yourgroupID is your package name, $yourartifactID is your project name, and maven-archetype-quickstart indicates to create a Maven Java project. Some files need to be downloaded during the project creation, so please keep the network connected.$yourartifactID in the D://mavenWorkplace directory. The files included in the folder have the following structure:simple---pom.xml Core configuration, under the project root directory---src---main---java Java source code directory---resources Java configuration file directory---test---java Test source code directory---resources Test configuration directory
pom.xml file and the Java folder under the main directory. The pom.xml file is primarily used to create dependencies and package configurations; the Java folder is used to store your source code.pom.xml file:<dependencies><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.2.4</version></dependency></dependencies>
pom.xml file:<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>utf-8</encoding></configuration></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>
/usr/local/service/hbase/conf directory, and view the hbase.zookeeper.quorum configuration in the hbase-site.xml file for ZooKeeper's IP address $quorum and the hbase.zookeeper.property.clientPort configuration for the port number $clientPort.PutExample.java in the main>java folder and adding the following code to it:import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;import java.io.IOException;/*** Created by tencent on 2018/6/30.*/public class PutExample {public static void main(String[] args) throws IOException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum","$quorum");conf.set("hbase.zookeeper.property.clientPort","$clientPort");conf.set("zookeeper.znode.parent", "$znodePath");Connection connection = ConnectionFactory.createConnection(conf);Admin admin = connection.getAdmin();HTableDescriptor table = new HTableDescriptor(TableName.valueOf("test1"));table.addFamily(new HColumnDescriptor("cf").setCompressionType(Algorithm.NONE));System.out.print("Creating table. ");if (admin.tableExists(table.getTableName())) {admin.disableTable(table.getTableName());admin.deleteTable(table.getTableName());}admin.createTable(table);Table table1 = connection.getTable(TableName.valueOf("test1"));Put put1 = new Put(Bytes.toBytes("row1"));put1.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("a"),Bytes.toBytes("value1"));table1.put(put1);Put put2 = new Put(Bytes.toBytes("row2"));put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("b"),Bytes.toBytes("value2"));table1.put(put2);Put put3 = new Put(Bytes.toBytes("row3"));put3.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("c"),Bytes.toBytes("value3"));table1.put(put3);System.out.println(" Done.");}}
mvn package
scp $localfile root@public IP address:$remotefolder
$localfile is the path and the name of your local file; root is the CVM instance username. You can look up the public IP address in the node information in the EMR or CVM console. $remotefolder is the path where you want to store the file in the CVM instance. After the upload is completed, you can check whether the file is in the corresponding folder on the EMR command line.[hadoop@10 hadoop]$ java –jar $package.jar
list command to see whether the HBase table is successfully created with the API, and if yes, you can run the scan command to see the detailed content of the table.[hadoop@10hbase]$ bin/hbase shellhbase(main):002:0> list 'test1'TABLETest11 row(s) in 0.0030 seconds=> ["test1"]hbase(main):006:0> scan 'test1'ROW COLUMN+CELLrow1 column=cf:a, timestamp=1530276759697, value=value1row2 column=cf:b, timestamp=1530276777806, value=value2row3 column=cf:c, timestamp=1530276792839, value=value33 row(s) in 0.2110 seconds
Feedback