本文目錄一覽:
- 1、如何使用python腳本調用zk 命令
- 2、如何利用pykafka遠程消費 zookeeper+kafka集群 python腳本
- 3、python 多線程 訪問網站
- 4、python zookeeper 怎麼入參
- 5、python無法訪問手機存儲文件夾
- 6、python爬蟲,集群是如何實現節點的發現和管理
如何使用python腳本調用zk 命令
zk命令是你系統中的命令嗎
如果是的話
import os
os.system(zk)
這樣就可以調用啦
如何利用pykafka遠程消費 zookeeper+kafka集群 python腳本
#從kafka消費
#consumer_area = topic_area.get_simple_consumer(auto_offset_reset=OffsetType.LATEST)
#從ZOOKEEPER消費
consumer_area = topic_area.get_balanced_consumer(
consumer_group=b’zs_download_04′, # 自己命令
auto_offset_reset=OffsetType.LATEST,#在consumer_group存在的情況下,設置此變數,表示從最新的開始取
#auto_offset_reset=OffsetType.EARLIEST,
#reset_offset_on_start=True,
auto_commit_enable=True,
#auto_commit_interval_ms=1,
zookeeper_connect=ZK_LIST
)
python 多線程 訪問網站
#python2
#coding=utf-8
import os,re,requests,sys,time,threading
reload(sys)
sys.setdefaultencoding(‘utf-8’)
class Archives(object):
def __init__(self, url):
self.url = url
def save_html(self, text):
fn = ‘{}_{}’.format(int(time.time()), self.url.split(‘/’)[-1])
dirname = ‘htmls’
if not os.path.exists(dirname):
os.mkdir(dirname)
with open(os.path.join(dirname, fn), ‘w’) as f:
f.write(text)
def get_htmls(self):
try:
r = requests.get(self.url)
r.raise_for_status()
r.encoding = r.apparent_encoding
print ‘get html from ‘, url
self.save_html(r.text)
except Exception,e:
print ‘爬取失敗’,e
def main(self):
thread = threading.Thread(target=self.get_htmls())
thread.start()
thread.join()
if __name__==’__main__’:
start=time.time()
fn = sys.argv[1] if len(sys.argv)1 else ‘urls.txt’
with open(fn) as f:
s = f.readlines()
for url in set(s):
a=Archives(url.strip())
a.main()
end=time.time()
print end-start
python zookeeper 怎麼入參
前提是zookeeper安裝包已經在/usr/local/zookeeper下
cd /usr/local/zookeeper/src/c
./configure
make
make install
wget –no-check-certificate
tar -zxvf zkpython-0.4.tar.gz
cd zkpython-0.4
sudo python setup.py install
zkpython應用
下面是網上一個zkpython的類,用的時候只要import進去就行
vim zkclient.py
#!/usr/bin/env python2.7
# -*- coding: UTF-8 -*-
import zookeeper, time, threading
python無法訪問手機存儲文件夾
無法訪問的原因是某一個磁碟中保存了Python的編譯環境。在這個編譯的環境下,存在一個python.exe文件,然後又對文件換了一個地方存儲,所以就會找不到指定的程序文件。解決方法如下:
1、進入到pycharm中,找到菜單欄中的「file」選項,在下拉的列表中選擇「settings」,進入到設置對話框。
2、左側中找到「ProjectInterpreter」選項,在點擊它之後,在點擊右側的「設置」圖案,在選項中選擇「ShowAll」,這樣就將全部的項目進行呈現了。
3、打開「ProjectInterpreter」對話框,找到我們舊程序的安裝位置,也就是換了位置的文件並選擇,在點擊右側的「-」號進行刪除。
4、將舊的程序進行刪除之後,點右側的「+」號,這個時候會彈出一個「AddPython…」對話框,找到界面中的「Existiingenvironment」,在右側的「…」中,添加一個存在的Python環境。
5、添加了之後,點擊「OK」,此時在運行文件時,就不會報文件找不到的錯誤了。
python爬蟲,集群是如何實現節點的發現和管理
Ignite集群管理——基於Zookeeper的節點發現
Ignite支持基於組播,靜態IP,Zookeeper,JDBC等方式發現節點,本文主要介紹基於Zookeeper的節點發現。
環境準備,兩台筆記本電腦A,B。A筆記本上使用VMware虛擬機安裝了Ubuntu系統C。
1、 C安裝Zookeeper
由於主要測試Ignite,這裡僅僅簡單安裝一個zookeeper節點,下載zookeeper解壓後,直接執行zookeeper目錄下的bin/zkServer.sh start命令則成功啟動zookeeper。
查看Ubuntu系統C的IP地址為192.168.1.104,zookeeper默認埠為12181。
注意:這裡VMware虛擬機的網路適配器一定要選擇橋接模式,否則A機器無法訪問虛擬機。
2、 A系統運行Ignite節點1;
代碼中加粗的部分便是Ignite註冊代碼,十分簡單。
package com.coshaho.learn.ignite.cluster;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.Ignition;import org.apache.ignite.cache.CacheMode;import org.apache.ignite.configuration.CacheConfiguration;import org.apache.ignite.configuration.IgniteConfiguration;import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;import org.apache.ignite.spi.discovery.tcp.ipfinder.zk.TcpDiscoveryZookeeperIpFinder;/**
*
* IgniteCluster01.java Create on 2017年6月3日 下午10:52:38
*
* 類功能說明: 基於zookeeper集群
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho */public class IgniteCluster01
{ public static void main(String[] args)
{ TcpDiscoverySpi spi = new TcpDiscoverySpi();
TcpDiscoveryZookeeperIpFinder ipFinder = new TcpDiscoveryZookeeperIpFinder(); // Specify ZooKeeper connection string.
ipFinder.setZkConnectionString(“192.168.1.104:12181”);
spi.setIpFinder(ipFinder);
IgniteConfiguration cfg = new IgniteConfiguration(); // Override default discovery SPI. cfg.setDiscoverySpi(spi); // Start Ignite node.
Ignite ignite =Ignition.start(cfg);
System.out.println(“IgniteCluster1 start OK.”);
CacheConfigurationInteger, String cacheCfg = new CacheConfigurationInteger, String();
cacheCfg.setBackups(1);
cacheCfg.setCacheMode(CacheMode.PARTITIONED);
cacheCfg.setName(“myCache”);
IgniteCacheInteger, String cache = ignite.getOrCreateCache(cacheCfg);
cache.put(1, “ignite1”);
System.out.println(cache.get(1));
System.out.println(cache.get(2));
}
}
3、 B系統運行Ignite節點2;
package com.coshaho.learn.ignite.cluster;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.Ignition;import org.apache.ignite.cache.CacheMode;import org.apache.ignite.configuration.CacheConfiguration;import org.apache.ignite.configuration.IgniteConfiguration;import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;import org.apache.ignite.spi.discovery.tcp.ipfinder.zk.TcpDiscoveryZookeeperIpFinder;public class IgniteCluster02
{ public static void main(String[] args)
{
TcpDiscoverySpi spi = new TcpDiscoverySpi();
TcpDiscoveryZookeeperIpFinder ipFinder = new TcpDiscoveryZookeeperIpFinder(); // Specify ZooKeeper connection string.
ipFinder.setZkConnectionString(“192.168.1.104:12181”);
spi.setIpFinder(ipFinder);
IgniteConfiguration cfg = new IgniteConfiguration(); // Override default discovery SPI. cfg.setDiscoverySpi(spi); // Start Ignite node.
Ignite ignite =Ignition.start(cfg);
System.out.println(“IgniteCluster2 start OK.”);
CacheConfigurationInteger, String cacheCfg = new CacheConfigurationInteger, String();
cacheCfg.setBackups(1);
cacheCfg.setCacheMode(CacheMode.PARTITIONED);
cacheCfg.setName(“myCache”);
IgniteCacheInteger, String cache = ignite.getOrCreateCache(cacheCfg);
cache.put(2, “ignite2”);
System.out.println(cache.get(1));
System.out.println(cache.get(2));
}
}
可以看到,Ignite節點2可以成功訪問到Ignite節點1存入緩存的數據。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/236884.html