欢迎投稿

今日深度:

HBASE教程,

HBASE教程,


package com.popoaichuiniu.jacy;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
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.Table;
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.util.Bytes;

// Class that has nothing but a main.
// Does a Put, Get and a Scan against an hbase table.
// The API described here is since HBase 1.0.
public class HBaseExample {
  public static Configuration config=null;
  public static Connection connection=null; 
  public static void createTable(String tableName, String[] fields) throws IOException
  {
      Admin  admin=connection.getAdmin();
      // Instantiating table descriptor class
      //HTableDescriptor contains the details about an HBase table such as the descriptors of all the column families, 
      //is the table a catalog table, -ROOT- or hbase:meta , if the table is read only, the maximum size of the memstore, when the region split should occur, coprocessors associated with it etc...

      if(admin.tableExists(TableName.valueOf(tableName)))
      {
          System.out.println("table has existed!");
      }
      else
      { 
          HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
          for(int i=0;i<fields.length;i++)
          {
              HColumnDescriptor hColumnFamily=new HColumnDescriptor(fields[i].getBytes());
              tableDescriptor.addFamily(hColumnFamily);
          } 

          admin.createTable(tableDescriptor);
          System.out.println("create successfully!");

      }

  }

  public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException
  {
     Table table=connection.getTable(TableName.valueOf(tableName));
     Put put=new Put(row.getBytes());
     for(int i=0;i<fields.length;i++)
     put.add(fields[i].getBytes(),fields[i].getBytes() , values[i].getBytes());
     table.put(put);
  }

  public void scanColumn(String tableName, String column) throws IOException
  {   Table table=connection.getTable(TableName.valueOf(tableName));
      if(column.contains(":"))//only one column
      {String []temp=column.split(":");
       ResultScanner  rs=table.getScanner(temp[0].getBytes(),temp[1].getBytes());

       for(Iterator<Result>it= rs.iterator();it.hasNext();)
       {
           Result result=rs.next();
           byte []values=result.value();
           if(values==null)
               System.out.print("null"+" ");
           else
               System.out.print(Bytes.toString(values)+" ");
       }
       System.out.println("");
       rs.close(); 
      }
      else
      {       
           ResultScanner  rs=table.getScanner(column.getBytes());

           for(Iterator<Result>it= rs.iterator();it.hasNext();)//result  Single row result of a Get or Scan query.
           {
               Result result=rs.next();
               if(result!=null)
               System.out.println(result+" ");
               else
                  System.out.print("null"+" ");
           }
           System.out.println("");
           rs.close();
      }   

  }

  public void modifyData(String tableName, String row, String column,String value) throws IOException
  {Table table=connection.getTable(TableName.valueOf(tableName));
  Put put=new Put(row.getBytes());
  String []temp=column.split(":");
  put.addColumn(temp[0].getBytes(), temp[1].getBytes(), value.getBytes());
  table.put(put);
  }
 public void deleteRow(String tableName, String row) throws IOException
 {
     Table table=connection.getTable(TableName.valueOf(tableName));
     Delete delete=new Delete(row.getBytes());
     table.delete(delete);
 }
  public static void main(String[] args) throws IOException {
    // You need a configuration object to tell the client where to connect.
    // When you create a HBaseConfiguration, it reads in whatever you've set
    // into your hbase-site.xml and in hbase-default.xml, as long as these can
    // be found on the CLASSPATH
    config = HBaseConfiguration.create();

    // Next you need a Connection to the cluster. Create one. When done with it,
    // close it. A try/finally is a good way to ensure it gets closed or use
    // the jdk7 idiom, try-with-resources: see
    // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
    //
    // Connections are heavyweight.  Create one once and keep it around. From a Connection
    // you get a Table instance to access Tables, an Admin instance to administer the cluster,
    // and RegionLocator to find where regions are out on the cluster. As opposed to Connections,
    // Table, Admin and RegionLocator instances are lightweight; create as you need them and then
    // close when done.
    //
    String fields []=new String[]{"S_No","S_Name","S_Sex","S_Age"};
    String values []=new String[]{"SA16011096","zms","man","22"};
   connection = ConnectionFactory.createConnection(config);
   createTable("Student",fields);
   addRecord("Student","SA16011096",fields,values);

   fields=new String[]{"C_No","C_Name","C_credits"};//
   values=new String[]{"123","zuheshuxue","3.5"};
   createTable("Course",fields);
   addRecord("Course","123",fields,values);

   fields=new String[]{"SC_Sno","SC_Cno","SC_Score"};//
   values=new String[]{"SA16011096","123","85"};
   createTable("SC",fields);
   addRecord("SC","SA16011096",fields,values);

   connection.close();

}
}

www.htsjk.Com true http://www.htsjk.com/hbase/41888.html NewsArticle HBASE教程, package com.popoaichuiniu.jacy; import java.io.IOException; import java.util.Iterator; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDesc...
相关文章
    暂无相关文章
评论暂时关闭