Tutorial 03_分布式数据库HBASE
(一)编程实现一下内容,并用Hadoop提供的Shell命令完成相同任务:
编程实现:
(1)列出HBase所有表的相关信息,例如表名;
package tutorial01;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class ListTables
{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void listTables() throws IOException
{
init();
HTableDescriptor[] hTableDescriptors = admin.listTables();
for (HTableDescriptor hTableDescriptor : hTableDescriptors)
{
System.out.println("表名:" + hTableDescriptor.getNameAsString());
}
close();
}
public static void init()
{
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }
catch (IOException e) { e.printStackTrace(); }
}
public static void close()
{
try { if (admin != null) { admin.close(); } if (connection != null) { connection.close(); } }
catch (IOException e) { e.printStackTrace(); }
}
public static void main(String[] args)
{
try { listTables(); }
catch (IOException e) { e.printStackTrace(); }
}
}
(2)在终端打印出所有表的相关数据;
package tutorial01;
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.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class ListTableData
{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void getData(String tableName) throws IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner)
{
printRecoder(result);
}
close();
}
public static void printRecoder(Result result) throws IOException
{
for (Cell cell : result.rawCells())
{
System.out.print("行健: " + new String(CellUtil.cloneRow(cell)));
System.out.print("列簇: " + new String(CellUtil.cloneFamily(cell)));
System.out.print(" 列: " + new String(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: " + new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳: " + cell.getTimestamp());
}
}
public static void init()
{
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }
catch (IOException e) { e.printStackTrace(); }
}
public static void close()
{
try { if (admin != null) { admin.close(); } if (null != connection) { connection.close(); } }
catch (IOException e) { e.printStackTrace(); }
}
public static void main(String[] args)
{
try { getData("student"); }
catch (IOException e) { e.printStackTrace(); }
}
}
(3)向已经创建好的表添加和删除指定的列和族;
package tutorial01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class InsertRow
{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val) throws IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
close();
}
public static void init()
{
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }
catch (IOException e) { e.printStackTrace(); }
}
public static void close()
{
try { if (admin != null) { admin.close(); } if (null != connection) { connection.close(); } }
catch (IOException e) { e.printStackTrace(); }
}
public static void main(String[] args)
{
try { insertRow("student", "s001", "course", "math", "99"); }
catch (IOException e) { e.printStackTrace(); }
}
}
(4)清空指定的表的所有记录和数据;
(5)统计表的行数。
Shell命令实现:
(1)列出HBase所有表的相关信息,例如表名;
(2)在终端打印出所有表的相关数据;
(3)向已经创建好的表添加和删除指定的列和族;
(4)清空指定的表的所有记录和数据;
(5)统计表的行数。