Skip to content

Instantly share code, notes, and snippets.

@balboah
Created May 20, 2011 13:36
Show Gist options
  • Save balboah/982896 to your computer and use it in GitHub Desktop.
Save balboah/982896 to your computer and use it in GitHub Desktop.
...
<?
protected function _getPathName($path)
{
$pathname = new Pathname();
$pathname->pathname = $path;
return $pathname;
}
public function getClient()
{
if (! $this->_client) {
$options = $this->getOptions();
list($server, $port) = explode(':', $options['thrift']);
$socket = new TSocket($server, $port);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocolAccelerated($transport);
$this->_client = new ThriftHadoopFileSystemClient($protocol);
try {
$transport->open();
$this->_client->setInactivityTimeoutPeriod(10);
} catch(TException $error) {
throw new Duego_Resource_Exception(
"Could not open a connection to the thrift server: {$options['thrift']}",
$error->getCode(),
$error->getPrevious()
);
}
}
return $this->_client;
}
public function create($path, $returnHandle=false)
{
try {
$status = $this->getClient()->create($this->_getPathName($path));
if (! $returnHandle) {
$this->getClient()->close($status);
}
} catch (TTransportException $error) {
throw new Duego_Resource_Exception(
"Could not create path: {$path}", $error->getCode(), $error->getPrevious()
);
}
}
public function putFile($localFile, $remoteFile)
{
$remoteHandle = $this->create($remoteFile, true);
$localHandle = fopen($localFile, 'r');
$localTest = fopen('test.jpg', 'w');
$localStat = fstat($localHandle);
$localSize = $localStat['size'];
$bytesWritten = 0;
while (! feof($localHandle)) {
$chunk = fread($localHandle, 4096);
$this->getClient()->write($remoteHandle, $chunk);
fwrite($localTest, $chunk, 4096);
$bytesWritten += strlen($chunk);
}
$this->getClient()->close($remoteHandle);
return $this;
}
?>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment